hdu 5768

来源:互联网 发布:base64 json 图片 编辑:程序博客网 时间:2024/06/05 20:53

传送门

Lucky7

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1877    Accepted Submission(s): 694


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.
 

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<=105for every i∈(1…n).
 

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: 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.
 

Author
FZU
 

Source
2016 Multi-University Training Contest 4
 

Recommend
wange2014   |   We have carefully selected several similar problems for you:  6079 6078 6077 6076 6075 

求[l,r]范围内是7的倍数,同时不满足任意一个给定的同余式的数的个数。如范围为[1,100],不满足模3余2或模5余3的7的倍数有7,21,42,49,70,84,91 ,故答案为7. 
其中除数都为非7的素数(≤105),除数的乘积小于1018.同余式最多有15个。
一看这道题就是孙子定理,多个同余式相乘,然后呢你会发现如果满足其中一些式子就可能出现容斥,所以呢所以 状态压缩呗。另外就是孙子定理求的是一个满足同余式的最小值。所以还要用左右区间去减一下除因子得到这个区间能含有多少个这个X的倍数。也就是求有多少个kX在这个区间里。另外就没有另外了。得用快速乘 不然有可能爆LL
//china no.1#pragma comment(linker, "/STACK:1024000000,1024000000")#include <vector>#include <iostream>#include <string>#include <map>#include <stack>#include <cstring>#include <queue>#include <list>#include <stdio.h>#include <set>#include <algorithm>#include <cstdlib>#include <cmath>#include <iomanip>#include <cctype>#include <sstream>#include <functional>#include <stdlib.h>#include <time.h>#include <bitset>using namespace std;typedef long long LL;const int N = 100000 + 5;inline LL Scan(){LL Res=0,ch,Flag=0;if((ch=getchar())=='-')Flag=1;else if(ch>='0' && ch<='9')Res=ch-'0';while((ch=getchar())>='0'&&ch<='9')Res=Res*10+ch-'0';return Flag ? -Res : Res;}LL cnt=0;int s[20],n;LL a[20], m[20];void ex_gcd(LL a, LL b, LL &x, LL &y, LL &d)    {        if (!b)        {            d = a, x = 1, y = 0;        }        else        {            ex_gcd(b, a % b, y, x, d);            y -= x * (a / b);        }    }    LL calc(LL x, LL r, LL p){return (x-r)/p;}LL inv(LL t, LL p) //如果不存在,返回-1    {        LL d, x, y;        ex_gcd(t, p, x, y, d);        return d == 1 ? (x % p + p) % p : -1;    }    LL mult(LL a, LL k, LL m)  {      LL res = 0;      while(k)      {          if(k & 1LL)              res = (res + a) % m;          k >>= 1;          a = (a << 1) % m;      }      return res;  }  LL China(LL l, LL r) //中国剩余定理    {        LL M = 1, ret = 0;        for(int i = 0; i <=n; i ++) if(s[i])M *= m[i];        for(int i = 0; i <=n; i ++) if(s[i])       {            LL w = M / m[i];            ret = (ret + mult(w,inv(w, m[i]) * a[i],M)) % M;        }        LL res = calc(r+M, ret, M) - calc(l-1+M, ret, M);    return res;    }  int main(){//freopen( "in.txt" , "r" , stdin );int T;scanf("%d",&T);for(int cas=1; cas<=T; cas++){cnt=0;LL l,r;n=Scan();l=Scan();r=Scan();memset(s,0,sizeof(s));m[n]=7;a[n]=0;s[n]=1;for(int i=0; i<n; i++)m[i]=Scan(),a[i]=Scan();int st=(1<<n);for(int i=0; i<st; i++){int t = i, k = 0;for(int j = 0; j < n; j++){s[j] = t & 1;t >>= 1;k += s[j];}k = k & 1 ? -1 : 1;cnt+=(LL) k*China(l,r);}printf("Case #%d: %lld\n",cas,cnt);}}


原创粉丝点击