地址:http://acm.hdu.edu.cn/showproblem.php?pid=5768

Lucky7

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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.

 
题意:
  就是问有多少个幸运数,然后幸运数呢,就是能被7整除的数,但是这些数可能被污染,哪些数会被污染呢,就是有n种方法会被污染,每种方法给出一个ai,一个
  pi,如果幸运数%pi等于ai就会被污染,然后给你一个区间,问这个区间有多少个没被污染的幸运数。
 
题解:
  这题一开始就知道是个中国剩余定理,然后看了多篇题解,发现要用容斥+中国剩余定理,然后发现要爆long long,必须要加上一个快速乘法=。=,然后求逆元的时候
  不能用费马小定理=。=,因为快速幂要爆long long,用快速乘法就会T(I good vegetable a),最后只有学了一波欧几里德求逆元TAT。
  先对着标程看了半天他在写啥,然后发现标程是先状态压缩,然后再容斥=。=,看标程就看了好久,我是不是没救了QAQ。
 
代码:
 #include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>
#include<queue>
#include<set>
#include<string>
#include<map>
#define inf 9223372036854775807
#define INF 9e7+5
#define PI acos(-1)
using namespace std;
typedef long long ll;
typedef double db;
typedef long long LL;
const int maxn = 1e2 + ;
const int mod = 1e9 + ;
const db eps = 1e-;
ll n, l, r, ai[maxn], pi[maxn]; int Bitcount(ll x) {
return x ? Bitcount(x >> ) + (x&1LL) : ;
} //求当前状态选择的方程的个数 ll q_mul(ll a, ll b, ll mod) {
ll ret = ;
while (b) {
if (b & ) ret = (ret + a) % mod;
b >>= ;
a = (a + a) % mod;
}
return ret;
} // 快速乘法 void Ex_Gcd(ll a, ll b, ll &x, ll &y){
ll d;
if(b==){
x=,y=;
return;
}
Ex_Gcd(b,a%b,y,x);
y-=a/b*x;
} // 这里直接抄的标程的函数。。 ll CRT(ll p[], ll a[], ll cnt) {
ll M = ;
ll res = ;
for (int i = ; i <= cnt; i++) M *= p[i];
for (int i = ; i <= cnt; i++) {
ll m = M / p[i], x, y;
Ex_Gcd(m, p[i], x, y);
res = (res + q_mul(q_mul(x, m, M), a[i], M)) % M;
}
return (res + M) % M;
} ll solve(ll x) {
ll p[maxn], m[maxn], ans = ;
p[] = , m[] = ;
if (!x) return ;
for (ll i = ; i < (1LL << n); i++) {
ll num = Bitcount(i), t = , cnt = ;
for (ll j = ; j < n; j++) {
if (i & (1LL << j)) {
p[++cnt] = pi[j];
m[cnt] = ai[j];
t *= pi[j];
}
}
ll res = CRT(p, m, cnt);
if (res > x) continue;
if (num & ) ans -= (x - res) / t + ; //容斥一下
else ans += (x - res) / t + ;
}
return ans + x / ;
} int main() {
//cin.sync_with_stdio(false);
//freopen("tt.txt", "r", stdin);
//freopen("isharp.out", "w", stdout);
int t, cas = ; cin >> t;
while (t--) {
cin >> n >> l >> r;
for (int i = ; i < n; i++)
cin >> pi[i] >> ai[i];
printf("Case #%d: %I64d\n", cas++, solve(r) - solve(l-));
}
return ;
}

最新文章

  1. 微信小程序开发初体验
  2. spark streaming 与 kafka 结合使用的一些概念理解
  3. GJM : Taurus.MVC 2.0 开源发布:WebAPI开发教程 [转载]
  4. 传递消息--第三方开源--EventBus的简单使用
  5. 56. 2种方法判断二叉树是不是平衡二叉树[is balanced tree]
  6. BootStrap入门教程 (二) :BASE CSS(排版(Typography),表格(Table),表单(Forms),按钮(Buttons))
  7. VS中无法加入断点进行调试解决方案
  8. php实现文件夹下的文件读取功能
  9. python课程第一天作业1-模拟登录
  10. php缓存技术常用函数
  11. 50个php程序性能优化集锦
  12. HDU [P1533]
  13. 利用Python脚本悄无声息的遥控室友电脑开机密码!
  14. linux mysql 定时备份 使用crontab
  15. Linux的is not in the sudoers file 解决
  16. 永无BUG
  17. UNIX环境编程学习笔记(5)——文件I/O之fcntl函数访问已打开文件的性质
  18. Java Tools &amp;Tools APIs
  19. Web 2.0应用客户端性能问题十大根源《转载》
  20. 初始Dubbo

热门文章

  1. SPOJ:Collecting Candies(不错的DP)
  2. 【POJ 2752】 Seek the Name, Seek the Fame
  3. POI2013 Bytecomputer
  4. SimpleDateFormat并发隐患及其解决
  5. 洛谷 - P1141 - 01迷宫 - dfs
  6. Codeforces 快速竞技#4
  7. hdoj1176【DP】
  8. poj 2492 A Bug&#39;s Life【带权并查集】
  9. python 合集set,交集,并集,差集,对称差集别搞混
  10. git 保存文件目录