51NOD - 1079 中国剩余定理
2018-02-06 13:28
369 查看
1079 中国剩余定理
数论
修改 隐藏话题
1079 中国剩余定理
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
一个正整数K,给出K Mod 一些质数的结果,求符合条件的最小的K。例如,K % 2 = 1, K % 3 = 2, K % 5 = 3。符合条件的最小的K = 23。
Input
Output
Input示例
Output示例
题解:中国剩余定理模板题,注意会爆int。
AC代码:
数论
修改 隐藏话题
1079 中国剩余定理
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
一个正整数K,给出K Mod 一些质数的结果,求符合条件的最小的K。例如,K % 2 = 1, K % 3 = 2, K % 5 = 3。符合条件的最小的K = 23。
Input
第1行:1个数N表示后面输入的质数及模的数量。(2 <= N <= 10) 第2 - N + 1行,每行2个数P和M,中间用空格分隔,P是质数,M是K % P的结果。(2 <= P <= 100, 0 <= K < P)
Output
输出符合条件的最小的K。数据中所有K均小于10^9。
Input示例
3 2 1 3 2 5 3
Output示例
23
题解:中国剩余定理模板题,注意会爆int。
AC代码:
#include<cstdio> using namespace std; const int maxn = 107; int a[maxn],f[maxn],n; int extgcd(int a,long long b,int &x,int &y) { if(b==0) { x=1; y=0; return a; } int d = extgcd(b,a%b,x,y); int t = x; x = y; y = t - a/b*y; return d; } int china(int b[], int w[], int k) { int i, d, x, y, a = 0; long long m,n = 1; for (i = 1; i <= k; i++) { n *= w[i]; } for (i = 1; i <= k; i++) { m = n / w[i]; d = extgcd(w[i], m, x, y); a = (a + y * m * b[i]) % n; } if (a > 0) { return a; } else { return (a + n); } } int main() { scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d%d",&f[i],&a[i]); int ans = china(a,f,n); printf("%d\n",ans); return 0; }
相关文章推荐
- 51nod 1079 中国剩余定理
- 51nod:1079 中国剩余定理
- 51nod 1079 中国剩余定理
- [51NOD - 1079] 中国剩余定理
- 51nod-1079 中国剩余定理
- 51nod 1079 中国剩余定理
- 51nod 1079 中国剩余定理
- 51nod 1079 中国剩余定理
- 51nod 1079 中国剩余定理
- 51nod - -1079 中国剩余定理
- 51nod-【1079 中国剩余定理】
- 51Nod-1079-【数学】中国剩余定理
- 51nod 1079 中国剩余定理
- 51nod-1079中国剩余定理
- 51nod--1079 中国剩余定理
- 51nod 1079 中国剩余定理
- 51nod 1079 中国剩余定理
- 51nod:1079 中国剩余定理(数学)
- 51Nod 1079中国剩余定理(孙子定理)
- 51Nod-1079-中国剩余定理