题目链接:
题目大意:C语言循环语句,初试i赋值A,每次加C,并且模2^k,当i == B时终止,问终止循环次数,或者无法终止.
思路: 思路比较简单的一道题,解方程CX + A = B (mod 2^k)即可,变形一下:CX = B - A (mod 2^k)
#include #include using namespace std;long long gcd(long long a, long long b){ return b ? gcd(b, a%b) : a;}void ext_gcd(long long a, long long b, long long &x, long long &y){ if (b == 0){ x = 1; y = 0; return ; } ext_gcd(b, a%b, x, y); long long tmp = x; x = y; y = tmp - a / b * y; return ;}bool equalation(long long a, long long b, long long c, long long &x, long long &y){ long long g = gcd(a, b); if (c % g != 0){ return false; } a /= g; b /= g; c /= g; ext_gcd(a, b, x, y); x *= c; long long tmp = abs(double(b)); x = (x % tmp + tmp) % tmp; return true;}int main(){ long long a, b, c, k, m; while(cin >> a >> b >> c >> k){ if (a + b + c + k == 0){ return 0; } m = 1LL << k; long long x, y; if (equalation(c, m, b-a, x, y)){ cout << x << endl; } else{ cout << "FOREVERn"; } } return 0;}