【数论】 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); } }
相关文章推荐
- HDOJ-1788 Chinese remainder theorem again(数论)
- 读控制台HDU 1788 Chinese remainder theorem again 数论读控制台
- hdoj-1788-Chinese remainder theorem again
- HDU 1788——Chinese remainder theorem again
- Chinese remainder theorem again(hdu 1788)两种解法:线性同余方程或者简单的最小公倍数
- HDU 1788 Chinese remainder theorem again
- hdu 1788 Chinese remainder theorem again(最小公倍数)
- hdu 1788 Chinese remainder theorem again(最小公倍数)
- HDU1788 Chinese remainder theorem again【中国剩余定理】
- HDU 1788 Chinese remainder theorem again
- 【数论】【中国剩余定理】【LCM】hdu1788 Chinese remainder theorem again
- HDU1788 Chinese remainder theorem again 中国剩余定理
- HDU 1788 Chinese remainder theorem again 中国剩余定理转换
- 【HDU】 1788 Chinese remainder theorem again
- hdu 1788 Chinese remainder theorem again((数学:简单题)
- Hdu 1788 Chinese remainder theorem again
- HDU——1788 Chinese remainder theorem again
- hdu 1788 Chinese remainder theorem again
- Chinese remainder theorem again + 数论
- hdu - 1788 - Chinese remainder theorem again-(gcd,不互质的中国剩余定理)