Lucky7 HDU
来源:互联网 发布:java制表符怎么用 编辑:程序博客网 时间:2024/06/11 18:32
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.
Input
On the first line there is an integer
Each test case starts with three integers three intergers
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
Output
For each test case, first output “Case #x: “,x=1,2,3…., then output the correct answer on a line.
Sample Input
2
2 1 100
3 2
5 3
0 1 100
Sample Output
Case #1: 7
Case #2: 14
Hint
For 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.
这题,诶,读完题就知道是什么思路了,除数都是质数,而且两两不等,明显的中国剩余定理的普通形式,而且才15个,明显的互斥原理,二进制枚举所有情况就行。。。但是当时训练心态已经崩了,被一到简单题搞死。。。这题坑点还不少,还有超long long的情况要处理,今天静下心来,写完了它。
分析:
假设根本没有同余式,那么答案就是:
但是,因为有了同余式,会有某些即使7的倍数有满足这些同余式的数,那这些数应该剔除,很显然的容斥。
我们现在来求假设在n组同余式下,范围在
也就是:
根据中国定理我们有:
那么其实可以写成
现在我们要看的就是有多少的x满足这个式子,这个显然是用扩展欧几里得来求一个特解,设:
因为:
所以:
代码实现上要注意longlong的溢出
#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#define N 50005using namespace std;typedef long long ll;void gcd(ll a,ll b,ll& d,ll& x,ll& y){ if(!b) { d=a; x=1; y=0; } else { gcd(b,a%b,d,y,x); y-=x*(a/b); }}//扩展欧几里得ll quickmul(ll a, ll b, ll mod) { a %= mod; ll ret = 0; while(b) { if(b & 1) ret = (ret + a) % mod; b >>= 1; a = (a + a) % mod; } return ret;}//防爆long longll getInv(ll M,ll m)//求逆元{ ll d,x,y; gcd(M,m,d,x,y); while(x<0) x+=m; return x;}int m[20];int a[20];int n;int main(){ int t; long long x,y; int ca=1; scanf("%d",&t); while(t--) { scanf("%d%lld%lld",&n,&x,&y); for(int i=0;i<n;i++) scanf("%d%d",m+i,a+i); ll ans1=y/7; ll num1=ans1; ll ans2=(x-1)/7; ll num2=ans2;//x,y相当于求两次而已 for(int s=1;s<(1<<n);s++) { int countt=0;//偶数则加,由容斥得 long long M=1; for(int j=0;j<n;j++) if((1<<j)&s) { countt++; M*=m[j]; } //cout<<"haha"<<endl; ll x0=0; for(int j=0;j<n;j++) if((1<<j)&s) x0=(x0+quickmul(M/m[j],getInv(M/m[j],m[j])*a[j]%M,M))%M; //cout<<x0<<" "<<M<<endl; ll x,y,d; gcd(7,M,d,x,y); while(x<0) x+=M; x=quickmul(x,x0,M); //cout<<"x:"<<x<<" "<<(num1-x)/M<<endl; x-=M;//x为负数的好处就不需要加1,也不需要判断大小 if(countt&1) { ans1-=(num1-x)/M; ans2-=(num2-x)/M; } else { ans1+=(num1-x)/M; ans2+=(num2-x)/M; } } printf("Case #%d: %lld\n",ca++,ans1-ans2); } return 0;}
- Lucky7 HDU
- Hdu 5768 Lucky7
- HDU 5768 Lucky7
- 【HDU】 5768 Lucky7
- HDU 5768 Lucky7
- HDU 5768 Lucky7
- HDU 5768 Lucky7
- hdu-5768 Lucky7 容斥
- HDU 5768 Lucky7 数论 中国剩余定理
- HDU 5768 Lucky7(CRT+容斥)
- HDU 5768 Lucky7 中国剩余定理
- 【HDU 5768】Lucky7(CRT+容斥)
- 2016多校4 hdu 5768 Lucky7 数论+容斥原理
- HDU 5768 Lucky7(容斥+同余方程组)
- HDU 5768 (Lucky7 中国剩余定理+容斥原理)
- HDU-5768-Lucky7(中国剩余定理+容斥)
- HDU-5768-Lucky7-中国剩余定律+容斥
- hdu 5768 多校4 Lucky7【crt+容斥】
- 关于“WireX Botnet”事件Android样本分析报告
- 2017 智慧树英语口语直通车答案单元测试答案
- javaweb之Servlet
- linux的文件及目录的默认权限umask值和chown改变用户属主及组和删除用户及-bash-4.1错误
- MongoDB Native Node.js Driver
- Lucky7 HDU
- 机器学习--SVM
- linux中进行时间同步
- scala如何从文件读取数据并转换成矩阵
- 30.github 搭建 blog
- neo4j使用教程(2)初步使用py2neo
- javaweb之HttpRequest与HttpResponse
- 小波分析与去噪
- 网页编程--JavaScript之变量