模数循环节——cf547A
campjls讲过模数循环节的问题,今天做cf才做到这类题
h1->a1的长度为len1,a1->a1的长度为cir1
h2->a2的长度为len2,a2->a2的长度为cir2
要注意特判,再用exgcd求
len1+cir1*t1 = len2+cir2*t2的一组整数解,把t1回代就是答案
//#pragma comment(linker,"/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include <stack>
#include <list>
using namespace std;
const int SZ=,INF=0x7FFFFFFF;
typedef long long lon; lon exgcd(lon a,lon b,lon &x,lon &y,lon &d)
{
if(b==)
{
x=;
y=;
d=a;
return a;
}
lon gcd=exgcd(b,a%b,x,y,d);
lon x2=x,y2=y;
x=y2;
y=x2-(a/b)*y2;
return gcd;
} int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","r",stdin);
lon m;
cin>>m;
lon h1,a1,x1,y1,h2,a2,x2,y2;
cin>>h1>>a1>>x1>>y1>>h2>>a2>>x2>>y2;
set<pair<lon,lon>> st;
st.insert(make_pair(h1,h2));
lon cnt1=,cnt2=,pri1=,pri2=,t1=,t2=;
for(lon time=;;++time)
{
lon newa1=(x1*h1+y1)%m,newa2=(x2*h2+y2)%m;
if(newa1==a1)
{
if(cnt1==)pri1=time;
else if(cnt1==) t1=time-pri1;
++cnt1;
}
if(newa2==a2)
{
if(cnt2==)pri2=time;
else if(cnt2==)t2=time-pri2;
++cnt2;
}
if(pri1&&pri2&&t1&&t2)break; if(newa1==a1&&newa2==a2)
{
cout<<time<<endl;
return ;
}
if(time>1e7)
{
cout<<-<<endl;
return ;
}
h1=newa1,h2=newa2;
}
lon x,y,d;
exgcd(abs(t1),abs(t2),x,y,d);
if(abs(pri1-pri2)%d==)
{
x*=(pri2-pri1)/d;
y*=(pri2-pri1)/d;
y*=-;
if(x<)
{
int beishu=abs((x)/(t2/d));
x=x+beishu*abs(t2/d);
y=y+beishu*abs(t1/d);
}
if(y<)
{
int num=;
num=abs((y)/(t1/d));
y=y+num*abs(t1/d);
if(y<)
{
++num;
y+=abs(t1/d);
}
x+=num*abs(t2/d);
}
if(x<)x+=abs(t2/d);
if(x>&&y>)
{
int num1=abs((x)/(t2/d));
int num2=abs((y)/(t1/d));
int miner=min(num1,num2);
x-=miner*abs(t2/d);
y-=miner*abs(t1/d);
}
cout<<pri1+x*t1<<endl;
}
else cout<<-<<endl;
return ;
}
模数循环节——cf547A的更多相关文章
- HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)
传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...
- 特征根法求通项+广义Fibonacci数列找循环节 - HDU 5451 Best Solver
Best Solver Problem's Link Mean: 给出x和M,求:(5+2√6)^(1+2x)的值.x<2^32,M<=46337. analyse: 这题需要用到高中的数 ...
- 2019牛客暑期多校训练营(第九场)The power of Fibonacci——循环节&&CRT
题意 求 $\displaystyle \sum_{i=1}^n F_i^m $,($1 \leq n\leq 10^9,1 \leq m\leq 10^3$),答案对 $10^9$ 取模. 分析 ...
- hdu 2837 Calculation 指数循环节套路题
Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 3746 (KMP求最小循环节) Cyclic Nacklace
题意: 给出一个字符串,要求在后面添加最少的字符是的新串是循环的,且至少有两个循环节.输出最少需要添加字符的个数. 分析: 假设所给字符串为p[0...l-1],其长度为l 有这样一个结论: 这个串的 ...
- Period(KMP,循环节问题)
题意: 求给你个串,前i位子串由某个字符串重复k次得到,求所有的i和k 分析: i-next[i]恰好是一个循环节 #include <map> #include <set> ...
- uva202:循环小数(循环节+抽屉原理)
题意: 给出两个数n,m,0<=n,m<=3000,输出n/m的循环小数表示以及循环节长度. 思路: 设立一个r[]数组记录循环小数,u[]记录每次的count,用于标记,小数计算可用 r ...
- poj 1961 Period【求前缀的长度,以及其中最小循环节的循环次数】
Period Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 14653 Accepted: 6965 Descripti ...
- [KMP求最小循环节][HDU3746][Cyclic Nacklace]
题意 给你个字符串,问在字符串末尾还要添加几个字符,使得字符串循环2次以上. 解法 无论这个串是不是循环串 i-next[i] 都能求出它的最小循环节 代码: /* 思路:kmp+字符串的最小循环节问 ...
随机推荐
- boost pointer container
1. boost::ptr_vector #include <boost/ptr_container/ptr_vector.hpp> #include <iostream> i ...
- linux su su-的区别
su只是切换用户. su - 切换用户并切换shell环境. su another pwd后为/home/current su - another pwd后为/home/another
- PHP curl_reset函数
curl_reset— 重置libcurl会话句柄的所有选项. 说明 void curl_reset ( resource $ch ) 该函数将重新初始化cURL的所有选项值(默认值). 注意:cur ...
- vue开发环境及vue相关
一.安装node.js Vue项目通常通过webpack工具来构建,而webpack命令的执行是依赖node.js的环境的,所以首先要安装node.js. 二.安装cnpm cnpm是淘宝对npm的镜 ...
- 【LeetCode 35】搜索插入位置
题目链接 [题解] 还是那句话,想知道l或者r所在的数字的含义 就想想它最后一次执行的时候在干什么就行. [代码] class Solution { public: int searchInsert( ...
- 2019牛客多校第五场G-subsequence 1 DP
G-subsequence 1 题意 给你两个字符串\(s.t\),问\(s\)中有多少个子序列能大于\(t\). 思路 令\(len1\)为\(s\)的子序列的长度,\(lent\)为\(t\)的长 ...
- The First Scrum Meeting!
第六周会议 情况简述 会议概要:明确需求,确定目标 参与人员:詹晓宇 谢赛金 熊紫仁 徐翠萍 周娟 孙尚煜 讨论时间:2019-10-24 会议地点:六区研讨性教室 具体内容 根据之前做的P ...
- 存储-docker volume 生命周期管理(14)
volume 生命周期管理 - 每天5分钟玩转 Docker 容器技术(44) Data Volume 中存放的是重要的应用数据,如何管理 volume 对应用至关重要.前面我们主要关注的是 volu ...
- SQL必知必会——创建和操纵表(十七)
1.创建表 一般有两种创建表的方法: 多数DBMS都具有交互式创建和管理数据库表的工具表也可以直接用SQL语句操纵1.1.表创建基础 CREATE TABLE products( prod_id,CH ...
- 力扣算法题—143ReorderList
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You may not mod ...