2016多校联赛4E (hdu5768) Lucky7
来源:互联网 发布:hits算法计算三轮 编辑:程序博客网 时间:2024/06/10 01:29
Lucky7
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 318 Accepted Submission(s): 127
Problem Description
When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortunately fall into the sea. While it was dying, seven dolphins arched its body and sent it back to the shore. It is said that ?? used to surrounded by 7 candles when he faced a extremely difficult problem, and always solve it in seven minutes.
?? once wrote an autobiography, which mentioned something about himself. In his book, it said seven is his favorite number and he thinks that a number can be divisible by seven can bring him good luck. On the other hand, ?? abhors some other prime numbers and thinks a number x divided by pi which is one of these prime numbers with a given remainder ai will bring him bad luck. In this case, many of his lucky numbers are sullied because they can be divisible by 7 and also has a remainder of ai when it is divided by the prime number pi.
Now give you a pair of x and y, and N pairs of ai and pi, please find out how many numbers between x and y can bring ?? good luck.
?? once wrote an autobiography, which mentioned something about himself. In his book, it said seven is his favorite number and he thinks that a number can be divisible by seven can bring him good luck. On the other hand, ?? abhors some other prime numbers and thinks a number x divided by pi which is one of these prime numbers with a given remainder ai will bring him bad luck. In this case, many of his lucky numbers are sullied because they can be divisible by 7 and also has a remainder of ai when it is divided by the prime number pi.
Now give you a pair of x and y, and N pairs of ai and pi, please find out how many numbers between x and y can bring ?? good luck.
Input
On the first line there is an integer T(T≤20) representing the number of test cases.
Each test case starts with three integers three intergers n, x, y(0<=n<=15,0<x<y<1018 ) on a line where n is the number of pirmes.
Following on n lines each contains two integers pi, ai where pi is the pirme and ?? abhors the numbers have a remainder of ai when they are divided by pi.
It is guranteed that all the pi are distinct and pi!=7.
It is also guaranteed that p1*p2*…*pn<=1018 and 0<ai<pi<=105 for every i∈(1…n).
Each test case starts with three integers three intergers n, x, y(0<=n<=15,0<x<y<
Following on n lines each contains two integers pi, ai where pi is the pirme and ?? abhors the numbers have a remainder of ai when they are divided by pi.
It is guranteed that all the pi are distinct and pi!=7.
It is also guaranteed that p1*p2*…*pn<=
Output
For each test case, first output "Case #x: ",x=1,2,3...., then output the correct answer on a line.
Sample Input
22 1 1003 25 30 1 100
Sample Output
Case #1: 7Case #2: 14HintFor Case 1: 7,21,42,49,70,84,91 are the seven numbers.For Case2: 7,14,21,28,35,42,49,56,63,70,77,84,91,98 are the fourteen numbers.
思路:
所需知识,容斥定理+中国剩余定理+模乘法
很明显是容斥定理+中国剩余定理。。。
枚举数去套中国定理可以用二进制状态压缩。
具体看代码你就懂了
#include<cstdio>#include<string>#include<cstring>#include<iostream> #include<algorithm>using namespace std;const int maxn = 20;typedef long long LL;LL A[maxn],MOD[maxn];LL N;LL multimod(LL x,LL y,LL m)//模乘法 {LL ans=0;while(y){if(y&1){ans+=x;ans%=m;}x+=x;x%=m;y>>=1;}ans=(ans+m)%m;return ans;}void extend_Euclid(LL a, LL b, LL &x, LL &y) //扩展欧几里得算法 { if(!b) {x = 1;y = 0;return; } extend_Euclid(b, a % b, x, y); LL tmp = x; x = y; y = tmp - (a / b) * y; } LL count(LL last) //中国剩余定理+容斥 M[]存放余数 A[]存放两两互质的数 { LL totalsum=0;for(int i=0;i<(1<<N);i++){LL M=7,ans=0,count=0,x=0,y;for(int j=0;j<N;j++)if(i&(1<<j)){M*=A[j];count++;}for(int j=0;j<N;j++)if(i&(1<<j)){LL Mi=M/A[j]; extend_Euclid(Mi,A[j],x,y); ans=(ans+multimod(multimod(x,Mi,M),MOD[j],M))%M; } ans=(ans+M)%M; LL res=last/M+(last%M>=ans); if(count%2)totalsum-=res;//容斥原理的体现 else totalsum+=res;}return totalsum;}int main(){int T;scanf("%d",&T);for(int tcase=1;tcase<=T;tcase++){LL x,y;scanf("%lld%lld%lld",&N,&x,&y);for(int i=0;i<N;i++){scanf("%lld%lld",&A[i],&MOD[i]);}printf("Case #%d: %lld\n",tcase,count(y)-count(x-1));}}
1 0
- 2016多校联赛4E (hdu5768) Lucky7
- 2016多校训练Contest4: 1005 Lucky7 hdu5768
- HDU5768 Lucky7
- 2016多校联合第四场 HDU5768
- 【数论】hdu5768 Lucky7(中国剩余定理)
- HDU5768 Lucky7[中国剩余定理+容斥定理]
- hdu5768 Lucky7 中国剩余定理 + 容斥原理
- HDU5768:Lucky7(中国剩余定理 & 容斥)
- 2016多校联赛4L (hdu5775) Bubble Sort
- 2016多校4 hdu 5768 Lucky7 数论+容斥原理
- 2016 Multi-University Training Contest 4 1005Lucky7
- 2016多校联赛 hdu 5724 Chess
- 2016多校联赛 hdu 5728 PowMod
- 2016暑假多校联赛 HDU5738
- 2016多校联赛5题解
- 2016多校联赛 hdu5826 physics 数学题
- hdu 5823 2016多校联赛8
- hdu_5536_多校联赛4_1010
- 深入浅出数据分析-读书笔记
- 自定义滑动开关控件的实现与使用
- 详解垃圾回收机制
- Android 四大图片缓存原理,特性对比
- 处理方法中的数据绑定
- 2016多校联赛4E (hdu5768) Lucky7
- 第三十二章 字符串匹配
- lucene性能优化
- 十六、迭代器模式Iterator(行为型)
- 全平台自动化测试软件的未来王者--上帝之眼( Sikuli )的简介与安装
- 同步容器&并发下的容器
- Linux目录结构和常用命令
- 如何使用CSDN-markdown编辑器
- Python(Dict和Set类型)