【POJ】2115 C Looooops(扩欧)】的更多相关文章

题目地址: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…
辗转相除法(欧几里得算法) 时间复杂度:在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:http://poj.org/problem?id=2115 思路 设循环T次 则要满足A≡(B+CT)(mod 2k) 可得 A=B+CT+m*2k 移项得C*T+2k*m=B-A (因为要满足B大于A)即是Exgcd的标准式子了 代码 #include<iostream> #include<cstdio> using namespace std; #define ll long long ll A,B,C,T,k; int gcd(ll a,ll b) { i…
链接:传送门 题意:题目中给出一个循环 for (variable = A; variable != B; variable += C) ,这个东东还需要 mod 2^k 问至少多次能退出,如果进入死循环输出输出"FOREVER" 思路:简单拓欧嘛,简单分析一下 A + C * x = B + 2^k * y,如果方程有解,那么最小整数解就是最少次数,否则就是死循环,写了一下快速幂,不清楚普通求 2^k 也并不会T,还是快一点好 /***************************…
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 starts by setting variable to value A and <= x < 2k) modulo 2k. Input The input consists…
http://poj.org/problem?id=2115 题意: 给你一个变量,变量初始值a,终止值b,每循环一遍加c,问一共循环几遍终止,结果mod2^k.如果无法终止则输出FOREVER. 思路: 根据题意原题可化成c * x = b - a mod (2 ^ k),然后解这个模线性方程. #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #includ…
[题目链接] http://poj.org/problem?id=2115 [题目大意] 求for (variable = A; variable != B; variable += C)的循环次数, 其中变量为k比特无符号整数. [题解] 题目等价于求解Cx=(B–A)(mod 2^k),利用扩展欧几里得算法可以求解该问题 [代码] #include <algorithm> #include <cstring> #include <cstdio> using name…
题目:http://poj.org/problem?id=2115 exgcd裸题.注意最后各种%b.注意打出正确的exgcd板子.就是别忘了/=g. #include<iostream> #include<cstdio> #include<cstring> #define ll long long using namespace std; ll a,b,x,y,r,A,B,C,k,g; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}…
扩展GCD...一定要(1L<<k),不然k=31是会出错的 ....                        C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15444   Accepted: 3941 Description A Compiler Mystery: We are given a C-language style for loop of type for (variable…