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

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 整除的并且不满足任意一个同余式的数的个数。
 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
typedef long long LL;
#define N 20 LL p[N], a[N];
int bit[N];
int n; LL mul(LL a, LL b, LL m)
{
//快速乘法
LL ans = ;
while(b) {
if(b & ) ans = (ans + a) % m;
a <<= ;
a %= m;
b >>= ;
}
return ans;
} LL exgcd(LL a, LL b, LL &x, LL &y)
{
if(b == ) {
x = ;
y = ;
return a;
}
LL r = exgcd(b, a%b, x, y);
int t = x;
x = y;
y = t - a / b * y;
return r;
} LL CRT(LL x, LL y)
{
//中国剩余定理: 找同时满足多个同余式的解
LL M = , ans = ;
for(int i = ; i <= n; i++) {
if(bit[i]) M *= p[i];
}
for(int i = ; i <= n; i++) {
if(bit[i]) {
LL x, y, Mi;
Mi = M / p[i];
exgcd(Mi, p[i], x, y);
x = (x % p[i] + p[i]) % p[i];
ans = (ans + mul(Mi * a[i] % M, x, M) % M + M) % M;
//ans找出来的是在 M 以内的特解即最小正整数解
}
}
//每过 M 可以有一个解
LL res = (y - ans + M) / M - (x - - ans + M) / M;
return res;
} void solve(LL x, LL y)
{
bit[n] = ;
LL ans = ;
int all = << n;
for(int i = ; i < all; i++) {
int tmp = i, k = ;
for(int j = ; j < n; j++) {
bit[j] = tmp & ;
tmp >>= ;
k += bit[j];
}
k = k & ? - : ;
//k是计算包含多少个同余式
//容斥原理: 奇数减,偶数加,具体可以看《组合数学》P108
//计算出不具有性质(满足任意一个同余式)的数的数量
ans += CRT(x, y) * k;
}
printf("%I64d\n", ans);
} int main()
{
int t;
scanf("%d", &t);
for(int cas = ; cas <= t; cas++) {
LL x, y;
scanf("%d%I64d%I64d", &n, &x, &y);
for(int i = ; i < n; i++)
scanf("%I64d%I64d", &p[i], &a[i]);
p[n] = , a[n] = ;
printf("Case #%d: ", cas);
solve(x, y);
}
return ;
}

HDU 5768:Lucky7(中国剩余定理 + 容斥原理)的更多相关文章

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5768 给你n个同余方程组,然后给你l,r,问你l,r中有多少数%7=0且%ai != bi. 比较明显 ...

  2. hdu 5768 Lucky7 中国剩余定理+容斥+快速乘

    Lucky7 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem D ...

  3. HDU 5768 Lucky7(CRT+容斥原理)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5768 [题目大意] 求出一个区间内7的倍数中,对于每个ai取模不等于bi的数的个数. [题解] 首 ...

  4. 【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 题目大意: T组数据,求L~R中满足:1.是7的倍数,2.对n个素数有 %pi!=ai  的数 ...

  5. HDU 5768 Lucky7 (容斥原理 + 中国剩余定理 + 状态压缩 + 带膜乘法)

    题意:……应该不用我说了,看起来就很容斥原理,很中国剩余定理…… 方法:因为题目中的n最大是15,使用状态压缩可以将所有的组合都举出来,然后再拆开成数组,进行中国剩余定理的运算,中国剩余定理能够求出同 ...

  6. HDU 5768 Lucky7 容斥原理+中国剩余定理(互质)

    分析: 因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题.当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的 ...

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

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

  8. hdu 5768 Lucky7 容斥

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

  9. ACM/ICPC 之 中国剩余定理+容斥原理(HDU5768)

    二进制枚举+容斥原理+中国剩余定理 #include<iostream> #include<cstring> #include<cstdio> #include&l ...

随机推荐

  1. box2d.js

    https://github.com/kripken/box2d.js/ Demo: http://kripken.github.io/box2d.js/webgl_demo/box2d.html 演 ...

  2. python os用法笔记

    获取文件所在路径 import os os.path.dirname(__file__)  获取当前文件的所在路径 print (os.path.dirname(os.path.dirname(__f ...

  3. C语言中静态库和动态库笔记

    库 库,故名思议,是存放东西的地方,其中存放的东西可以被多个人公用. 程序中借用库的概念,描述将代码进行抽取,这种代码被大多数程序使用, 其过程具有一定的模块化.封装.抽象的特征. 按照库的使用方式, ...

  4. 基于jdk proxy的动态代理模式

    代理模式 是spring AOP机制的实现基础,有必要学习一下. 有两种,一种是目标类有接口的, 采用JDK动态代理,一种是目标类没接口的,采用CGLIB动态代理. 先看一组代码, package c ...

  5. JQuery Delay Hover效果

    CSS代码 .tbui_aside_float_bar { position: fixed; left: 50%; bottom: 120px; margin-left: 608px; border- ...

  6. Leetcode: Convert a Number to Hexadecimal

    Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two's compl ...

  7. 如何抠PSD素材中的图片

    在网上经常可以找到一些好看呢的PSD素材,如何才能将这些素材抠出来 存成一张张的png图片呢? 在PhotoShop中 1·隐藏无用的图层,然后窗口中仅剩需要看到的那个素材图 2·用工具选择该区域(注 ...

  8. eclipse JAVA 类元素 快速添加set和get方法

    鼠标在代码窗口,鼠标右键 然后选择如下图 然后在下面图中,选择需要个类的元素添加的set和get 选择好了点击OK,这样eclipse就自动生成get和set方法

  9. 纪念我sgu第一个10题!

    哎,等下次再做20题纪念一下!尼玛,根本做不出来,还要到处翻别人的555555555555

  10. Python之urllib2

    urllib2 - extensible library for opening URLs Note The urllib2 module has been split across several ...