模数循环节——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+字符串的最小循环节问 ...
随机推荐
- 获取浏览器IP
public static string GetLoginIP(HttpRequestBase request) { string loginip = " ...
- 硬币问题 (dp,多重背包的二分优化)
题目描述 给你n种硬币,知道每种的面值Ai和每种的数量Ci.问能凑出多少种不大于m的面值. 输入 有多组数据,每一组第一行有两个整数 n(1≤n≤100)和m(m≤100000),第二行有2n个整数, ...
- <自动化测试>之<selenium API 用法2>
不知道之前的selenium API 用法1,有没有去练习, 个人认为线性代码还是要靠敲的, 后面的模块化除了多敲还需要一定的编程思想去理解, 今天下午不是很忙就给来这儿补充点selenium api ...
- ICO和区块链区别
区块链项目众筹(ICO)浅析 2017-07-25 原创 Fintech科普大使 ICO是区块链初创公司项目融资的重要方式类似于Kickstarter众筹,但有不同之处(具体在下一节详述),可以避开传 ...
- 基于nodejs的一个实时markdown转html工具小程序
1.版本一 - 1.1`npm install marked --save` 安装markdwon转html的包.- 1.2 使用watchFile监视 markdown文件 /** * Create ...
- Eclipes 安装windowbuilding
一.找到对应版本的windowbuilder 打开这个链接:http://www.eclipse.org/windowbuilder/download.php eclipse的版本号可以在eclips ...
- POJ 2001 Shortest Prefixes (Trie)
题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...
- upc组队赛3 Chaarshanbegaan at Cafebazaar
Chaarshanbegaan at Cafebazaar 题目链接 http://icpc.upc.edu.cn/problem.php?cid=1618&pid=1 题目描述 Chaars ...
- vue父组件与子组件之间的数据传递
父组件向子组件传递数据 父组件用数据绑定:子组件用props接收 <!-- test-vue-model父组件 --> <template> <div> <m ...
- Linux(四)—— 项目部署与ansible自动化部署
目录 项目部署与ansible自动化部署 一.项目部署 二.ansible自动化部署(python自动化运维) 1.安装ansible 2.ansible例子 3.ansible自动化部署nginx ...