Codevs 3990 中国余数定理 2
来源:互联网 发布:origin8.6绘图软件 编辑:程序博客网 时间:2024/05/09 13:08
3990 中国余数定理 2
时间限制: 1 s
空间限制: 1000 KB
题目等级 : 白银 Silver
传送门
题目描述 Description
Skytree神犇最近在研究中国博大精深的数学。
这时,Sci蒟蒻前来拜访,于是Skytree给Sci蒟蒻出了一道数学题:
给定n个质数,以及k模这些质数的余数。问:在闭区间[a,b]中,有多少个k?最小的k是多少?
Sci蒟蒻数学能力差了Skytree三条街,所以他只好寻求计算机的帮助。他发邮件给同为oier的你,你能帮他解决这个问题吗?
输入描述 Input Description
输入第一行为三个正整数n、a、b。
第2到n+1行,每行有两个整数,分别代表第n个质数和k模第n个质数的余数。
输出描述 Output Description
输出为两个整数,代表闭区间[a,b]中k的个数和闭区间[a,b]中最小的k。如果k不存在,则输出两个0。
样例输入 Sample Input
样例1:
3 2 28
3 2
5 3
7 2
样例2:
3 24 31
3 2
5 3
7 2
样例输出 Sample Output
样例1:
1
23
样例2:
0
0
数据范围及提示 Data Size & Hint
1<=a<=b<=10^14
n<=10
输入保证所有n个质数的乘积<=10^14
每个质数<=1.5*10^9
请无视通过率(被人黑了。。。)
数据保证不会溢出64bit整数
分类标签 Tags
数论 欧几里得算法 逆元
/*中国剩余定理(exgcd+逆元)(互质版).数学很重要.cin很重要.*/#include<iostream>#include<cstdio>#define MAXN 11#define LL long long using namespace std;LL l,r,m[MAXN],M1[MAXN],x,ans,sum,y,n,tot=1,b[MAXN],M[MAXN];void exgcd(LL a,LL b){ if(!b) { x=1;y=0;return ; } exgcd(b,a%b); LL s=x;x=y;y=s-a/b*y;}int main(){ cin>>n>>l>>r; //scanf("%I64d%I64d%I64d",&n,&l,&r); for(int i=1;i<=n;i++) cin>>m[i]>>b[i],tot*=m[i];//① for(int i=1;i<=n;i++) M[i]=tot/m[i];//② for(int i=1;i<=n;i++) { x=y=0; exgcd(M[i],m[i]); M1[i]=(x+m[i])%m[i];//③ } for(int i=1;i<=n;i++) { ans=(ans+M[i]*M1[i]%tot*b[i])%tot;//④//随时mod以防爆掉. } if(ans<l||ans>r) ans=sum=0; else sum=(r-ans)/tot+1; cout<<sum<<endl<<ans;; //printf("%lld\n%lld",sum,ans); return 0;}
- Codevs 3990 中国余数定理 2
- 余数 中国余数定理
- CodeVS1990 中国余数定理2
- 【codevs3990】中国余数定理2
- code vs 3990 中国余数定理 2 (中国剩余定理)
- [CODEVS3990]中国余数定理 2(中国剩余定理)
- poj1006中国余数定理
- 中国余数定理
- 中国余数定理
- 中国余数定理:HDU1370
- 中国余数定理
- 中国余数定理
- 中国余数定理
- 中国余数定理
- 中国余数定理
- POJ 1006 中国余数定理
- 中国余数定理解题步骤
- CodeVS3040 中国余数定理 1
- HackRank Two Two
- 20. 让 express 可以进行父子模板包含 express-ejs-layouts
- AsyncTask异步任务与LruCache缓存策略实现图片加载(一)
- POJ3984 迷宫问题
- vagrant
- Codevs 3990 中国余数定理 2
- Java学习笔记(基本数据类型和变量命名规则)
- 【编程马拉松】【025-数字三角形 】
- Application windows are expected to have a root view controller at the end of application launch'
- VMware12 安装MAC-OS-X 10.11
- 【编程马拉松】【026-是男人就下100层】
- 371. Sum of Two Integers
- Go语言学习笔记9
- Java中set集合