博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 5768:Lucky7(中国剩余定理 + 容斥原理)
阅读量:5275 次
发布时间:2019-06-14

本文共 3764 字,大约阅读时间需要 12 分钟。

 

Lucky7

 

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.
 
 
题意:找出[l, r]里面可以被 7 整除的并且不满足任意一个同余式的数的个数。
1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 using namespace std; 8 typedef long long LL; 9 #define N 2010 11 LL p[N], a[N];12 int bit[N];13 int n;14 15 LL mul(LL a, LL b, LL m)16 {17 //快速乘法18 LL ans = 0;19 while(b) {20 if(b & 1) ans = (ans + a) % m;21 a <<= 1;22 a %= m;23 b >>= 1;24 }25 return ans;26 }27 28 LL exgcd(LL a, LL b, LL &x, LL &y)29 {30 if(b == 0) {31 x = 1;32 y = 0;33 return a;34 }35 LL r = exgcd(b, a%b, x, y);36 int t = x;37 x = y;38 y = t - a / b * y;39 return r;40 }41 42 LL CRT(LL x, LL y)43 {44 //中国剩余定理: 找同时满足多个同余式的解45 LL M = 1, ans = 0;46 for(int i = 0; i <= n; i++) {47 if(bit[i]) M *= p[i];48 }49 for(int i = 0; i <= n; i++) {50 if(bit[i]) {51 LL x, y, Mi;52 Mi = M / p[i];53 exgcd(Mi, p[i], x, y);54 x = (x % p[i] + p[i]) % p[i];55 ans = (ans + mul(Mi * a[i] % M, x, M) % M + M) % M;56 //ans找出来的是在 M 以内的特解即最小正整数解57 }58 }59 //每过 M 可以有一个解60 LL res = (y - ans + M) / M - (x - 1 - ans + M) / M;61 return res;62 }63 64 void solve(LL x, LL y)65 {66 bit[n] = 1;67 LL ans = 0;68 int all = 1 << n;69 for(int i = 0; i < all; i++) {70 int tmp = i, k = 0;71 for(int j = 0; j < n; j++) {72 bit[j] = tmp & 1;73 tmp >>= 1;74 k += bit[j];75 }76 k = k & 1 ? -1 : 1;77 //k是计算包含多少个同余式78 //容斥原理: 奇数减,偶数加,具体可以看《组合数学》P10879 //计算出不具有性质(满足任意一个同余式)的数的数量80 ans += CRT(x, y) * k;81 }82 printf("%I64d\n", ans);83 }84 85 int main()86 {87 int t;88 scanf("%d", &t);89 for(int cas = 1; cas <= t; cas++) {90 LL x, y;91 scanf("%d%I64d%I64d", &n, &x, &y);92 for(int i = 0; i < n; i++)93 scanf("%I64d%I64d", &p[i], &a[i]);94 p[n] = 7, a[n] = 0;95 printf("Case #%d: ", cas);96 solve(x, y);97 }98 return 0;99 }

 

转载于:https://www.cnblogs.com/fightfordream/p/5773905.html

你可能感兴趣的文章
React Native 入门 调试项目
查看>>
C# 通过 Quartz .NET 实现 schedule job 的处理
查看>>
关于java之socket输入流输出流可否放在不同的线程里进行处理
查看>>
目前为止用过的最好的Json互转工具类ConvertJson
查看>>
Day13
查看>>
tensorflow saver简介+Demo with linear-model
查看>>
Luogu_4103 [HEOI2014]大工程
查看>>
Oracle——SQL基础
查看>>
项目置顶随笔
查看>>
Redis的安装与使用
查看>>
P1970 花匠
查看>>
java语言与java技术
查看>>
NOIP2016提高A组五校联考2总结
查看>>
iOS 项目的编译速度提高
查看>>
table中checkbox选择多行
查看>>
Magento开发文档(三):Magento控制器
查看>>
性能调优攻略
查看>>
ie6解决png图片透明问题
查看>>
瞬间的永恒
查看>>
2019-8-5 考试总结
查看>>