campjls讲过模数循环节的问题,今天做cf才做到这类题

h1->a1的长度为len1,a1->a1的长度为cir1

h2->a2的长度为len2,a2->a2的长度为cir2

要注意特判,再用exgcd求

len1+cir1*t1 = len2+cir2*t2的一组整数解,把t1回代就是答案

  1. //#pragma comment(linker,"/STACK:1024000000,1024000000")
  2. #include<iostream>
  3. #include<cstdio>
  4. #include<string>
  5. #include<cstring>
  6. #include<vector>
  7. #include<cmath>
  8. #include<queue>
  9. #include<stack>
  10. #include<map>
  11. #include<set>
  12. #include<algorithm>
  13. #include <stack>
  14. #include <list>
  15. using namespace std;
  16. const int SZ=,INF=0x7FFFFFFF;
  17. typedef long long lon;
  18.  
  19. lon exgcd(lon a,lon b,lon &x,lon &y,lon &d)
  20. {
  21. if(b==)
  22. {
  23. x=;
  24. y=;
  25. d=a;
  26. return a;
  27. }
  28. lon gcd=exgcd(b,a%b,x,y,d);
  29. lon x2=x,y2=y;
  30. x=y2;
  31. y=x2-(a/b)*y2;
  32. return gcd;
  33. }
  34.  
  35. int main()
  36. {
  37. std::ios::sync_with_stdio();
  38. //freopen("d:\\1.txt","r",stdin);
  39. lon m;
  40. cin>>m;
  41. lon h1,a1,x1,y1,h2,a2,x2,y2;
  42. cin>>h1>>a1>>x1>>y1>>h2>>a2>>x2>>y2;
  43. set<pair<lon,lon>> st;
  44. st.insert(make_pair(h1,h2));
  45. lon cnt1=,cnt2=,pri1=,pri2=,t1=,t2=;
  46. for(lon time=;;++time)
  47. {
  48. lon newa1=(x1*h1+y1)%m,newa2=(x2*h2+y2)%m;
  49. if(newa1==a1)
  50. {
  51. if(cnt1==)pri1=time;
  52. else if(cnt1==) t1=time-pri1;
  53. ++cnt1;
  54. }
  55. if(newa2==a2)
  56. {
  57. if(cnt2==)pri2=time;
  58. else if(cnt2==)t2=time-pri2;
  59. ++cnt2;
  60. }
  61. if(pri1&&pri2&&t1&&t2)break;
  62.  
  63. if(newa1==a1&&newa2==a2)
  64. {
  65. cout<<time<<endl;
  66. return ;
  67. }
  68. if(time>1e7)
  69. {
  70. cout<<-<<endl;
  71. return ;
  72. }
  73. h1=newa1,h2=newa2;
  74. }
  75. lon x,y,d;
  76. exgcd(abs(t1),abs(t2),x,y,d);
  77. if(abs(pri1-pri2)%d==)
  78. {
  79. x*=(pri2-pri1)/d;
  80. y*=(pri2-pri1)/d;
  81. y*=-;
  82. if(x<)
  83. {
  84. int beishu=abs((x)/(t2/d));
  85. x=x+beishu*abs(t2/d);
  86. y=y+beishu*abs(t1/d);
  87. }
  88. if(y<)
  89. {
  90. int num=;
  91. num=abs((y)/(t1/d));
  92. y=y+num*abs(t1/d);
  93. if(y<)
  94. {
  95. ++num;
  96. y+=abs(t1/d);
  97. }
  98. x+=num*abs(t2/d);
  99. }
  100. if(x<)x+=abs(t2/d);
  101. if(x>&&y>)
  102. {
  103. int num1=abs((x)/(t2/d));
  104. int num2=abs((y)/(t1/d));
  105. int miner=min(num1,num2);
  106. x-=miner*abs(t2/d);
  107. y-=miner*abs(t1/d);
  108. }
  109. cout<<pri1+x*t1<<endl;
  110. }
  111. else cout<<-<<endl;
  112. return ;
  113. }

模数循环节——cf547A的更多相关文章

  1. HDU 5895 Mathematician QSC(矩阵乘法+循环节降幂+除法取模小技巧+快速幂)

    传送门:HDU 5895 Mathematician QSC 这是一篇很好的题解,我想讲的他基本都讲了http://blog.csdn.net/queuelovestack/article/detai ...

  2. 特征根法求通项+广义Fibonacci数列找循环节 - HDU 5451 Best Solver

    Best Solver Problem's Link Mean: 给出x和M,求:(5+2√6)^(1+2x)的值.x<2^32,M<=46337. analyse: 这题需要用到高中的数 ...

  3. 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$ 取模. 分析 ...

  4. hdu 2837 Calculation 指数循环节套路题

    Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  5. HDU 3746 (KMP求最小循环节) Cyclic Nacklace

    题意: 给出一个字符串,要求在后面添加最少的字符是的新串是循环的,且至少有两个循环节.输出最少需要添加字符的个数. 分析: 假设所给字符串为p[0...l-1],其长度为l 有这样一个结论: 这个串的 ...

  6. Period(KMP,循环节问题)

    题意: 求给你个串,前i位子串由某个字符串重复k次得到,求所有的i和k 分析: i-next[i]恰好是一个循环节 #include <map> #include <set> ...

  7. uva202:循环小数(循环节+抽屉原理)

    题意: 给出两个数n,m,0<=n,m<=3000,输出n/m的循环小数表示以及循环节长度. 思路: 设立一个r[]数组记录循环小数,u[]记录每次的count,用于标记,小数计算可用 r ...

  8. poj 1961 Period【求前缀的长度,以及其中最小循环节的循环次数】

    Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 14653   Accepted: 6965 Descripti ...

  9. [KMP求最小循环节][HDU3746][Cyclic Nacklace]

    题意 给你个字符串,问在字符串末尾还要添加几个字符,使得字符串循环2次以上. 解法 无论这个串是不是循环串 i-next[i] 都能求出它的最小循环节 代码: /* 思路:kmp+字符串的最小循环节问 ...

随机推荐

  1. 获取浏览器IP

    public static string GetLoginIP(HttpRequestBase request)        {            string loginip = " ...

  2. 硬币问题 (dp,多重背包的二分优化)

    题目描述 给你n种硬币,知道每种的面值Ai和每种的数量Ci.问能凑出多少种不大于m的面值. 输入 有多组数据,每一组第一行有两个整数 n(1≤n≤100)和m(m≤100000),第二行有2n个整数, ...

  3. <自动化测试>之<selenium API 用法2>

    不知道之前的selenium API 用法1,有没有去练习, 个人认为线性代码还是要靠敲的, 后面的模块化除了多敲还需要一定的编程思想去理解, 今天下午不是很忙就给来这儿补充点selenium api ...

  4. ICO和区块链区别

    区块链项目众筹(ICO)浅析 2017-07-25 原创 Fintech科普大使 ICO是区块链初创公司项目融资的重要方式类似于Kickstarter众筹,但有不同之处(具体在下一节详述),可以避开传 ...

  5. 基于nodejs的一个实时markdown转html工具小程序

    1.版本一 - 1.1`npm install marked --save` 安装markdwon转html的包.- 1.2 使用watchFile监视 markdown文件 /** * Create ...

  6. Eclipes 安装windowbuilding

    一.找到对应版本的windowbuilder 打开这个链接:http://www.eclipse.org/windowbuilder/download.php eclipse的版本号可以在eclips ...

  7. POJ 2001 Shortest Prefixes (Trie)

    题目链接:POJ 2001 Description A prefix of a string is a substring starting at the beginning of the given ...

  8. upc组队赛3 Chaarshanbegaan at Cafebazaar

    Chaarshanbegaan at Cafebazaar 题目链接 http://icpc.upc.edu.cn/problem.php?cid=1618&pid=1 题目描述 Chaars ...

  9. vue父组件与子组件之间的数据传递

    父组件向子组件传递数据 父组件用数据绑定:子组件用props接收 <!-- test-vue-model父组件 --> <template> <div> <m ...

  10. Linux(四)—— 项目部署与ansible自动化部署

    目录 项目部署与ansible自动化部署 一.项目部署 二.ansible自动化部署(python自动化运维) 1.安装ansible 2.ansible例子 3.ansible自动化部署nginx ...