2016 Multi-University Training Contest 4 1005Lucky7
来源:互联网 发布:supreme淘宝哪家正 编辑:程序博客网 时间:2024/06/05 06:23
Lucky7
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 637 Accepted Submission(s): 236
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.
Author
FZU
Source
2016 Multi-University Training Contest 4
【题意】在给你的(x,y)的范围中,能被7整除的是幸运的数字,不过有些幸运的数字被污染了,给出n对p,a 能被p除然后余数是a的,是被污染的。
问在给定范围内有多少个数是幸运的。
【分析】通过给定的条件 我们共可以列出n+1个模线性方程 ,那么只要容斥一下:枚举几个在一起,利用解方程求解,奇的加偶的减就行了。
即:假使n=2 那么ans=(总的7的倍数的个数)+(条件1)+(条件2)-条件(1,2)。代码
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<map>#include<set>#include<vector>#define F first#define S second#define mp make_pairusing namespace std;typedef long long LL;LL extend_gcd(LL a,LL b,LL &x,LL &y){ if(a==0 && b==0) return -1; if(b==0 ) { x=1; y=0; return a; } LL d=extend_gcd(b,a%b,y,x); y-= a/b*x; return d;}LL m[20],a[20];bool solve(LL &m0,LL &a0,LL m,LL a){ LL y,x; LL g=extend_gcd(m0,m,x,y); if(abs(a-a0)%g) return false; x *= (a-a0)/g; x %= m/g; a0 = (x*m0 + a0); m0 *= m/g; a0 %= m0; if(a0 < 0) a0+=m0; return true ;}bool MLES(LL &m0,LL &a0,LL n,int x){ bool flag=true; m0=1; a0=0; for(int i=0; i<=n; i++) { if(i!=0&&(x&(1<<(i-1)))==0) continue; if(!solve(m0,a0,m[i],a[i])) { flag=false; break; } } return flag;}bool judge(int x){ int sum=0; while(x) { if(x&1) sum++; x>>=1; } if(sum%2) return true; return false;}int main(){ LL T,n,l,r; int u=1; scanf("%lld",&T); while(T--) { scanf("%lld%lld%lld",&n,&l,&r); for(int i=1; i<=n; i++) { scanf("%lld%lld",&m[i],&a[i]); } m[0]=7; a[0]=0; LL x,y ; LL ans=0,sum; for(int i=1; i<(1<<n); i++) { sum=0; if(MLES(x,y,n,i)) { if(r>=y) { sum++; sum+=(r-y)/x; } if(l>=y) { sum--; sum-=(l-y)/x; } if(l%x==y) sum++; if(judge(i)) { ans+=sum; } else ans-=sum; } } sum =0; if(MLES(x,y,n,0)) { if(r>=y) { sum++; sum+=(r-y)/x; } if(l>=y) { sum--; sum-=(l-y)/x; } if(l%x==y) sum++; ans=sum-ans; } printf("Case #%d: %lld\n",u++,ans); } return 0;}
0 0
- 2016 Multi-University Training Contest 4 1005Lucky7
- HDU 5768 Lucky7(2016 Multi-University Training Contest 4 -1005)——中国剩余定理 + 容斥原理
- 2016 Multi-University Training Contest 4
- 2016 Multi-University Training Contest 4
- 2016 Multi-University Training Contest 4 题解
- 2016 Multi-University Training Contest 1 1005 Necklace
- 2016 Multi-University Training Contest 2-1005---HDU 5738 Eureka
- 2016 Multi-University Training Contest 2 1005 Eureka
- 2016 Multi-University Training Contest 2-1005(hdu5738) Eureka
- 2017 Multi-University Training Contest 3 1005
- 2016 Multi-University Training Contest 4题解报告
- hdu5763Another Meaning 2016 Multi-University Training Contest 4(kmp+dp)
- 2016 Multi-University Training Contest 4 欢乐战记
- 2016 Multi-University Training Contest 4 题解(待续)
- 2016 Multi-University Training Contest 4 1001 Another Meaning
- 2016 Multi-University Training Contest 4 1006 Substring
- 2016 Multi-University Training Contest 4 1007 (hdu 5770)
- HDU 5769-->Substring-->2016 Multi-University Training Contest 4
- drawable文件夹下的文件类型
- 题目845 无主之地1
- 初学者的福利6:杨辉三角
- UIL的使用
- 单例模式
- 2016 Multi-University Training Contest 4 1005Lucky7
- EBCDIC、ASCII与EBCDIC的关系及相互转化
- ORACLE函数之日期时间运算函数
- 深入理解Android之Gradle
- CocoaPods的安装和使用
- Access程序从txt文件导入数据至数据库的DoCmd.TransferText方法
- percona
- 设计模式(一)单例模式
- Jmeter 的使用(完成一次简单的测试计划)