您的位置:首页 > 大数据 > 人工智能

【数论】 HDOJ 1788 Chinese remainder theorem again

2014-07-17 14:59 549 查看
简单的中国剩余定理。。。数论一顿套模板。。。

#include <iostream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <climits>
#define maxn 800005
#define eps 1e-6
#define mod 10007
#define INF 99999999
#define lowbit(x) (x&(-x))
//#define lson o<<1, L, mid
//#define rson o<<1 | 1, mid+1, R
typedef long long LL;
using namespace std;

LL a[maxn], b[maxn], n;
void extend_gcd(LL a, LL b, LL &d, LL &x, LL &y)
{
if(b == 0) { d = a, x = 1, y = 0; }
else { extend_gcd(b, a%b, d, y, x), y -= x*(a/b); }
}
void extend_chinese_reminder(LL &a1, LL &b1)
{
LL x, y, g, tmp, i, a2, b2;
for(i = 1; i < n; i++) {
a2 = a[i], b2 = b[i];
extend_gcd(a1, a2, g, x, y);
tmp = a2/g;
x = x*(b2-b1)/g;
x = (x%tmp+tmp)%tmp;
b1 = a1*x+b1;
a1 = (a1*a2)/g;
b1 = (b1%a1+a1)%a1;
}
}
int main(void)
{
LL a1, b1, aa, i;
while(scanf("%I64d%I64d", &n, &aa), n!=0 || aa!=0) {
for(i = 0; i < n; i++) scanf("%I64d", &a[i]);
for(i = 0; i < n; i++) b[i] = a[i] - aa;
a1 = a[0], b1 = b[0];
extend_chinese_reminder(a1, b1);
printf("%I64d\n", b1);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: