Hdu 1788 Chinese remainder theorem again

来源:互联网 发布:橙光立绘制作软件 编辑:程序博客网 时间:2024/06/05 11:45

大意不再赘述。

思路:其实是最小公倍数。

N+a ≡ 0 (mod Mi),求得lcm之后减去a即可。

#include <iostream>#include <cstdlib>#include <cstdio>#include <cstring>using namespace std;typedef __int64 LL; //long long WAconst int MAXN = 110;int n;LL lcm, a;LL gcd(LL a, LL b){return b == 0 ? a:gcd(b, a%b);}int read_case(){scanf("%d%I64d", &n, &a);if(!n && !a) return 0;lcm = 1;for(int i = 1; i <= n; i++){LL m;scanf("%I64d", &m);lcm = lcm / gcd(lcm, m) * m;}return 1;}void solve(){LL ans = lcm-a;printf("%I64d\n", ans);}int main(){while(read_case()){solve();}return 0;}


原创粉丝点击