Lucky7

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

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

Author
FZU
思路:中国剩余定理+容斥+扩展欧几里得;
比赛时打了将近2个小时,最后还因为快速幂中的一个模没写而超时 GG啊。
其实题解的思路和我的思路有些不同,题解是容斥的时候将7加入一起用中国剩余定理求解,这时求出来的通解直接是7 的倍数,因为%7=0,加入求同余方程组的解;
因为只要符合模这些数中的一条就可以了,如果直接求解会重复,所以用容斥原理。加入其中求得一个通解,记为t+kmod;
然后y<=t+kmod<=x;移项可以求出k的解的个数,然后根据容斥这里是奇减偶加。
然后我的思路是没有加入7求同余。
而是用求出那些个的数的同余方程组的解然后t+kmod=7r;再用扩展欧几里得,求k的通解,但在这之前我先求x<=t+kmod<=y;的k的范围。
扩欧求得的b+7p,代入前面的不等式解p的范围,求得p有多少个,然后容斥奇减偶加p;
  1 #include<stdio.h>
  2 #include<algorithm>
  3 #include<iostream>
  4 #include<string.h>
  5 #include<queue>
  6 #include<stdlib.h>
  7 #include<iostream>
  8 #include<vector>
  9 #include<map>
 10 #include<set>
 11 #include<math.h>
 12 using namespace std;
 13 typedef long long LL;
 14 typedef struct pp
 15 {
 16         LL x;
 17         LL y;
 18 } ss;
 19 ss ans[20];
 20 LL quick(LL n,LL m,LL mod);
 21 LL mul(LL n, LL m,LL p);
 22 pair<LL,LL>P(LL n,LL m);
 23 LL gcd(LL n, LL m);
 24 LL mm[20];
 25 int main(void)
 26 {
 27         int i,j,k;
 28         scanf("%d",&k);
 29         int __ca=0;
 30         LL n,x,y;
 31         while(k--)
 32         {
 33                 __ca++;
 34                 scanf("%lld %lld %lld",&n,&x,&y);
 35                 LL sum;
 36                 sum=y/7-(x-1)/7;
 37                  printf("Case #%d: ",__ca);
 38                 if(n==0)
 39                 {
 40                         printf("%lld\n",sum);
 41                 }
 42                 else
 43                 {
 44                         LL mod=1;
 45                         for(i=0; i<n; i++)
 46                         {
 47                                 scanf("%lld %lld",&ans[i].x,&ans[i].y);
 48                         }
 49                         LL anw=0;
 50                         int s;
 51                         for(j=1; j<(1<<n); j++)
 52                         {
 53                                 int cr=0;
 54                                 for(s=0; s<n; s++)
 55                                 {
 56                                         if(j&(1<<s))
 57                                         {
 58                                                 mm[cr++]=s;
 59                                         }
 60                                 }
 61                                 LL mod=1;
 62                                 LL acm=0;
 63                                 for(i=0; i<cr; i++)
 64                                 {
 65                                         mod*=ans[mm[i]].x;
 66                                 }
 67                                 for(i=0; i<cr; i++)
 68                                 {
 69                                         LL mod1=mod/ans[mm[i]].x;
 70                                         LL ni=quick(mod1,ans[mm[i]].x-2,ans[mm[i]].x);
 71                                   acm=(acm+mul(mul(mod1,ni,mod),ans[mm[i]].y,mod))%mod;
 72                                 }
 73                                 anw=acm;
 74                                 LL ctx=0;
 75                                 if(anw>y)
 76                                 {
 77                                         continue;
 78                                 }
 79                                 else
 80                                 {
 81                                         LL  cha=x-anw;
 82                                         LL nx,ny;
 83                                         LL cha1=y-anw;
 84                                         nx=cha/mod;
 85                                         while(anw+mod*nx<x)
 86                                         {
 87                                                 nx++;
 88                                         } if(cha<0)nx=0;
 89                                         ny=cha1/mod;
 90                                         {
 91                                                 pair<LL ,LL>AK=P(mod,7);
 92                                                 LL nxx=(AK.first*acm%7+7)%7;
 93                                                 nxx=((-nxx)%7+7)%7;
 94                                                 if(ny>=nxx)
 95                                                 {
 96                                                         LL cx=max(nx-nxx,(LL)0);
 97                                                         LL cy=ny-nxx;
 98                                               if(cx==0)ctx=cy/7+1;else {ctx=cy/7-(cx-1)/7;}
 99                                                 }
100                                         }
101                                 }
102                                 if(cr%2)
103                                 {
104                                         sum-=ctx;
105                                 }
106                                 else sum+=ctx;
107                         } printf("%lld\n",sum);
108                 }
109         }
110         return 0;
111 }
112 LL gcd(LL n, LL m)
113 {
114         if(m==0)
115         {
116                 return n;
117         }
118         else if(n%m==0)
119         {
120                 return m;
121         }
122         else
123         {
124                 return gcd(m,n%m);
125         }
126 }
127 LL quick(LL n,LL m,LL mod)
128 {
129         LL cnt=1;n%=mod;
130         while(m)
131         {
132                 if(m&1)
133                 {
134                         cnt=cnt*n%mod;
135                 }
136                 n=n*n%mod;
137                 m/=2;
138         }
139         return cnt;
140 }
141 LL mul(LL n, LL m,LL p)
142 {
143         n%=p;
144         m%=p;
145         LL ret=0;
146         while(m)
147         {
148                 if(m&1)
149                 {
150                         ret=ret+n;
151                         ret%=p;
152                 }
153                 m>>=1;
154                 n<<=1;
155                 n%=p;
156         }
157         return ret;
158 }
159 pair<LL,LL>P(LL n,LL m)
160 {
161         if(m==0)
162         {
163                 pair<LL,LL>ak;
164                 ak=make_pair(1,0);
165                 return ak;
166         }
167         else
168         {
169                 pair<LL,LL>A=P(m,n%m);
170                 LL nx=A.second;
171                 LL ny=A.first;
172                 ny=ny-(n/m)*nx;
173                 A.first=nx;
174                 A.second=ny;
175                 return A;
176         }
177 }

  1 #include<stdio.h>
  2 #include<algorithm>
  3 #include<iostream>
  4 #include<string.h>
  5 #include<queue>
  6 #include<stdlib.h>
  7 #include<iostream>
  8 #include<vector>
  9 #include<map>
 10 #include<set>
 11 #include<math.h>
 12 using namespace std;
 13 typedef long long LL;
 14 typedef struct pp
 15 {
 16         LL x;
 17         LL y;
 18 } ss;
 19 ss ans[20];
 20 LL quick(LL n,LL m,LL mod);
 21 LL mul(LL n, LL m,LL p);
 22 pair<LL,LL>P(LL n,LL m);
 23 LL gcd(LL n, LL m);
 24 LL mm[20];
 25 int main(void)
 26 {
 27         int i,j,k;
 28         scanf("%d",&k);
 29         int __ca=0;
 30         LL n,x,y;
 31         while(k--)
 32         {
 33                 __ca++;
 34                 scanf("%lld %lld %lld",&n,&x,&y);
 35                 LL sum;
 36                 sum=y/7-(x-1)/7;
 37                 if(n==0)
 38                 {
 39                         printf("Case #%d: %lld\n",__ca,sum);
 40                 }
 41                 else
 42                 {
 43                         LL mod=1;
 44                         for(i=0; i<n; i++)
 45                         {
 46                                 scanf("%lld %lld",&ans[i].x,&ans[i].y);
 47                         }
 48                         LL anw=0;
 49                         LL s;
 50                         for(j=1; j<(1<<n); j++)
 51                         {  LL mod=1;
 52                                 int cr=0;
 53                                 for(s=0; s<n; s++)
 54                                 {
 55                                         if(j&(1<<s))
 56                                         {
 57                                                 mm[cr++]=s;
 58                                                 mod*=ans[s].x;
 59                                         }
 60                                 }mod*=7;
 61                                 LL acm=0;
 62                                 for(i=0; i<cr; i++)
 63                                 {
 64                                         LL mod1=mod/ans[mm[i]].x;
 65                                         LL ni=quick(mod1,ans[mm[i]].x-2,ans[mm[i]].x);
 66                                         acm=(acm+mul(mul(mod1,ni,mod),ans[mm[i]].y,mod))%mod;
 67                                 }
 68                                 acm%=mod;
 69                                 acm+=mod;
 70                                 acm%=mod;
 71                                 anw=acm;
 72                                 LL ctx=0;
 73                                 if(anw>y)
 74                                 {
 75                                         continue;
 76                                 }
 77                                 else
 78                                 {
 79                                        if(anw<x)
 80                                        {
 81                                            LL ax=x-anw-1;
 82                                            LL ay=y-anw;
 83                                            ctx+=ay/mod-ax/mod;
 84                                        }
 85                                        else if(anw>=x)
 86                                        {  LL ay=y-anw;
 87                                            ctx+=ay/mod+1;
 88                                        }
 89                                 }
 90                                 if(cr%2)
 91                                 {
 92                                         sum-=ctx;
 93                                 }
 94                                 else sum+=ctx;
 95                         } printf("Case #%d: %lld\n",__ca,sum);
 96                 }
 97         }
 98         return 0;
 99 }
100 LL gcd(LL n, LL m)
101 {
102         if(m==0)
103         {
104                 return n;
105         }
106         else if(n%m==0)
107         {
108                 return m;
109         }
110         else
111         {
112                 return gcd(m,n%m);
113         }
114 }
115 LL quick(LL n,LL m,LL mod)
116 {
117         LL cnt=1;n%=mod;
118         while(m>0)
119         {
120                 if(m&1)
121                 {
122                         cnt=cnt*n%mod;
123                 }
124                 n=n*n%mod;
125                 m/=2;
126         }
127         return cnt;
128 }
129 LL mul(LL n, LL m,LL p)
130 {
131         n%=p;
132         m%=p;
133         LL ret=0;
134         while(m)
135         {
136                 if(m&1)
137                 {
138                         ret=ret+n;
139                         ret%=p;
140                 }
141                 m>>=1;
142                 n<<=1;
143                 n%=p;
144         }
145         return ret;
146 }
147 pair<LL,LL>P(LL n,LL m)
148 {
149         if(m==0)
150         {
151                 pair<LL,LL>ak;
152                 ak=make_pair(1,0);
153                 return ak;
154         }
155         else
156         {
157                 pair<LL,LL>A=P(m,n%m);
158                 LL nx=A.second;
159                 LL ny=A.first;
160                 ny=ny-(n/m)*nx;
161                 A.first=nx;
162                 A.second=ny;
163                 return A;
164         }
165 }

转载于:https://www.cnblogs.com/zzuli2sjy/p/5716795.html

Lucky7(hdu5768)相关推荐

  1. HDU 5768 Lucky7 (中国剩余定理 + 容斥 + 快速乘法)

    Lucky7 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...

  2. HDU 5768 Lucky7(2016 Multi-University Training Contest 4 -1005)——中国剩余定理 + 容斥原理

    [传送门](http://acm.hdu.edu.cn/showproblem.php?pid=5768) Lucky7 Time Limit: 2000/1000 MS (Java/Others)  ...

  3. 信息学竞赛中的数学知识 --- 容斥原理

    C++基础数论-----容斥原理 C++基础数论-----容斥原理_C2020lax的博客-CSDN博客_容斥原理c++ C++数论容斥原理----无关的元素 C++数论容斥原理----无关的元素 - ...

  4. 软考之路-网络攻击:主动攻击和被动攻击

    被动攻击(针对路上的东西下手) 概念:就是网络窃听,窃取数据包并进行分析,从中窃取重要的敏感信息 措施:防止被动攻击的主要手段是数据加密传输 主动攻击(针对计算机下手) 概念:包括窃取.篡改.假冒和破 ...

  5. 机房收费系统-- MDI子窗体显示技巧(续vb.net版)

    在vb6.0版的机房收费系统中说道过这个问题,回顾请点击:http://blog.csdn.net/chenjinge7/article/details/8231546这次在vb.net版本中想再次利 ...

  6. 菜鸟学UML--概述

    转载于:https://www.cnblogs.com/lucky7/archive/2013/03/11/3768657.html

  7. 机房收费系统-- MDI子窗体显示技巧

    我们所做的机房收费系统,是运用的MDI多窗体方式,对多类数据进行存取和查询!在窗体显示设置上主要会遇到这样两个问题: 一. 程序运行后,为达到使用效率高的目的,父窗体显示一个" 学生刷卡上下 ...

  8. oracle+函数怎么写if,Oracle NULLIF函数

    Oracle NULLIF函数 Oracle NULLIF函数语法为NULLIF(表达式1,表达式2),如果表达式1和表达式2相等则返回空值,如果表达式1和表达式2不相等则返回表达式1的结果. 注意: ...

  9. 东北大学程序设计夏令营数学

    数学专题 本次训练包含题目 Problem A Mysterious Bacteria Problem B Harmonic Number Problem C Trailing Zeroes (III ...

最新文章

  1. Linux最佳聊天软件:Skype 4.3轻体验
  2. Apple Catching POJ - 2385(基础的动态规划算法)
  3. Mybatis映射文件SQL语句模糊查询,#和$的区别和注意事项
  4. linux tar order
  5. Linux下查看CPU个数(逻辑个数和物理个数)
  6. 软件架构-里氏替换原则
  7. Redis系列之key操作命令与Redis中的事务详解(六)
  8. C#中splitContainer用法
  9. p6spy监测mysql_Spring使用p6spy监控sql
  10. flex布局及flex实现常见的前端布局
  11. android刷机工具mac版,刷机精灵mac版
  12. 【合集】MATLAB常见图形格式调整问题
  13. 2019牛客暑期多校训练营(第三场)----C-Guessing ETT
  14. 如何基于深度学习实现商品识别技术|图普科技
  15. SSM源码分析之Spring05-DI实现原理(基于Annotation 注入)
  16. pinyin4j:拼音与汉字的转换实例
  17. pytorch安装 镜像网站
  18. 几何校正(image to map)
  19. 音频相概念扫盲——声音处理的过程
  20. Excel取消工作簿密码【忘记工作簿密码的解决方法】

热门文章

  1. [YTU]_2921( Shape系列-7)
  2. OpenCv:Mat矩阵的初始化
  3. mse函数(均方误差函数)
  4. 某些列满足某些条件就被改是什么值,不满足就被改为另外的一些值(python pandas)
  5. 如何正确使用迁移学习
  6. Java之二分法查找
  7. 13.3Runtime 类中的主要方法
  8. vue循环渲染变量类样式
  9. vue-router(2)
  10. MFC CTreeCtrl运用