题目大意&&分析: for (variable = A; variable != B; variable += C) statement;这个循环式子表示a+c*n(n为整数)==b是停止循环,题目中要求(a+c*n)%2^k=b时停止循环:所以我们可以得到一个形如ax+by=c的方程式:a+c*n=b+2^k*m:通过移项:c*x-2^k*m=b-a:可以直接套exgcd模板了: 代码: #include <iostream> using namespace std; typ…
题目:http://poj.org/problem?id=2115 就是扩展欧几里得呗: 然而忘记除公约数... 代码如下: #include<iostream> #include<cstdio> #include<cstring> using namespace std; typedef long long ll; ll A,B,C,k,a,b,x,y,g,s; ll gcd(ll a,ll b){return a%b?gcd(b,a%b):b;} void exgc…
http://poj.org/problem?id=2115 题意:给出A,B,C和k(k表示变量是在k位机下的无符号整数),判断循环次数,不能终止输出"FOREVER". 即转化成 c*x = b-a mod (2^k), 解这个模线性方程的最小正整数解. 模板题,代码很短,但是很难理解的样子...转载了一些有关的资料... #include <stdio.h> #define LL long long LL Extend_Euclid(LL a,LL b,LL &…
辗转相除法(欧几里得算法) 时间复杂度:在O(logmax(a, b))以内 int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } 扩展欧几里得算法 时间复杂度和欧几里得算法相同 int extgcd(int a, int b, int& x, int& y) { int d = a; if (b != 0) { d = extgcd(b, a % b, y, x); y -= (a / b) * x;…
题目地址:POJ 2115 水题. . 公式非常好推.最直接的公式就是a+n*c==b+m*2^k.然后能够变形为模线性方程的样子,就是 n*c+m*2^k==b-a.即求n*c==(b-a)mod(2^k)的最小解.(真搞不懂为什么训练的时候好多人把青蛙的约会都给做出来了,这题却一直做不出来.. . . . 这两道不都是推公式然后变形吗. .... ) 代码例如以下: #include <iostream> #include <cstdio> #include <strin…
题意不难理解,看了后就能得出下列式子: (A+C*x-B)mod(2^k)=0 即(C*x)mod(2^k)=(B-A)mod(2^k) 利用模线性方程(线性同余方程)即可求解 模板直达车 #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; typedef long l…
题目链接:https://cn.vjudge.net/contest/276376#problem/B 题目大意:for( int  i= A ; i != B; i+ = c ),然后给你A,B,C,K,前三个是for循环里面的变量,K指的是每一次ABC三个数都对2^k进行取余,然后问你要使得这个循环停止,最少的循环次数,如果是一个死循环,就输出"FOREVER". 具体思路:和我之前写的那一篇博客思路差不多,证明过程直接上图. 一开始没有注意到B,等号右边应该是B-A,而不是B,以…
C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24355   Accepted: 6788 Description A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; variable != B; variable += C) statement; I.e., a loop w…
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20128 Accepted: 5405 Description A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; variable != B; variable += C) statement; I.e., a loop which…
题目链接:https://cn.vjudge.net/contest/276376#problem/C 题目大意:中文题目. 具体思路:扩展gcd,具体证明过程看图片(就这麽个题我搞了一天,,,). AC代码: #include<iostream> #include<stack> #include<stdio.h> #include<cmath> #include<queue> #include<algorithm> using na…