大意不再赘述。
思路:简单的一元线性同余方程,有点KD,移位运算符强制转换行不通,必须用1LL<<K才可以。
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
using namespace std;
typedef __int64 LL;
LL A, B, C;
int K;
void ex_gcd(LL a, LL b, LL &d, LL &x, LL &y)
{
if(!b)
{
d = a; x = 1; y = 0;
}
else
{
ex_gcd(b, a%b, d, y, x);
y -= x*(a/b);
}
}
void solve()
{
LL d, x, y;
ex_gcd(C, 1LL<<K, d, x, y);
if((B-A) % d) { printf("FOREVER\n"); return ;}
LL b1 = (1LL<<K) / d;
x *= (B-A) / d;
LL ans = (x % b1 + b1) % b1;
printf("%I64d\n", ans);
}
int main()
{
while(scanf("%I64d%I64d%I64d%d", &A, &B, &C, &K) && (A+B+C+K))
{
solve();
}
return 0;
}
本文介绍了一种解决一元线性同余方程的具体算法,并通过C++实现了解题过程。该算法利用了扩展欧几里得算法来寻找方程的特解,进而得出所有可能的解。特别注意的是,在处理移位运算时,需要使用1LL<<K以确保正确性。
275

被折叠的 条评论
为什么被折叠?



