Lucky7

题目链接:

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

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 p1p2…*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.

Source

2016 Multi-University Training Contest 4

##题意:

对于给出的区间[x, y]找出有多少个符合要求的数:
1. 能被7整除.
2. 给出不超过15组(pi, ai),其中pi为质数;
要求找出的数x满足 x % pi != ai;


##题解:

可以先找出能被7整除但不满足条件2的数:
就得到了一组同余模方程,这里用中国剩余定理来处理.
因为只要满足任一同余方程就需要被计数,所以需要用容斥原理来做.
由于n=15,所以最多只有2^15种方程组合,用状态压缩记录每个组合对应的方程,对于每种组合跑一遍中国剩余定理,找出在区间范围内的个数,再用容斥原理累加起来(奇数个元素就加,偶数个则减).

以上思路很好想,坑点在于:由于数据规模比较大
中国剩余定理中 ans = (ans+x*w*a[i])%M; 乘法的3个因子和M的规模都可能达到longlong上限,所以一乘就可能导致爆掉longlong.
这里的解决方案是:用快速乘法取模(类似快速幂)代替上述乘法.


##代码:
``` cpp
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define eps 1e-8
#define maxn 25
#define inf 0x3f3f3f3f
#define IN freopen("in.txt","r",stdin);
using namespace std;

LL _left, _right;

LL x,y,gcd;

void ex_gcd(LL a,LL b)

{

if(!b) {x=1;y=0;gcd=a;}

else {ex_gcd(b,a%b);LL temp=x;x=y;y=temp-a/b*y;}

}

int BitCount2(int n) {

int c =0 ;

for(; n; ++c) {

n &= (n -1) ;

}

return c ;

}

LL quickmul(LL a, LL b, LL mod) {

a %= mod;

LL ret = 0;

while(b) {

if(b & 1) ret = (ret + a) % mod;

b >>= 1;

a = (a + a) % mod;

}

return ret;

}

int n, m[maxn],a[maxn];

LL M;

LL China(int state)

{

LL w,ans=0; M=1;

for(int i=0;i<=n;i++)

if(!i || state&(1<<(i-1)))

M *= m[i];

for(int i=0;i<=n;i++) if(!i || state&(1<<(i-1))){

w=M/m[i];

ex_gcd(w,m[i]); while(x<0) {x+=m[i];y-=w;}

    //ans=(ans+x*w*a[i])%M;
//上式乘法会爆longlong,所以需要用快速乘法来防暴.
ans = (ans + quickmul(a[i] ,quickmul(x,w,M), M)) % M;
} LL cur = (ans+M)%M;
LL T = M;
cur = cur % T; LL l_ans, r_ans;
if(_left <= cur) l_ans = cur;
else l_ans = _left - (_left-cur) % T + T; if(_right < cur) return 0LL;
else if(_right == cur) return 1LL;
else r_ans = _right - (_right-cur) % T; if(l_ans > r_ans) return 0LL;
return (r_ans-l_ans) / T + 1LL;

}

int main(int argc, char const *argv[])

{

//IN;

int t; cin >> t; int ca = 1;
while(t--)
{
cin >> n >> _left >> _right;
m[0] = 7LL; a[0] = 0LL;
for(int i=1; i<=n; i++) {
scanf("%I64d %I64d", &m[i], &a[i]);
} LL ans = 0;
for(int i=1; i<(1<<n); i++) {
int flag = (BitCount2(i)%2? 1:0);
if(flag) ans += China(i);
else ans -= China(i);
} LL tmp = _right/7LL - _left/7LL;
if(_left%7LL==0) tmp++; printf("Case #%d: %I64d\n", ca++, tmp-ans);
} return 0;

}

HDU 5768 Lucky7 (中国剩余定理 + 容斥 + 快速乘法)的更多相关文章

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

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

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

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

  3. hdu_5768_Lucky7(中国剩余定理+容斥)

    题目链接:hdu_5768_Lucky7 题意: 给你一个区间,问你这个区间内是7的倍数,并且满足%a[i]不等于w[i]的数的个数 乍一看以为是数位DP,仔细看看条件,发现要用中国剩余定理,然后容斥 ...

  4. HDU5768Lucky7(中国剩余定理+容斥定理)(区间个数统计)

    When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortun ...

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

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

  6. hdu 5768 Lucky7 容斥

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

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

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

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

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

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

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

随机推荐

  1. px,dp,sp单位转换工具类

    在layout中使用dp 在代码中getWidth系列得到的是px 设置字体大小时使用的是sp /** * Android大小单位转换工具类 */ public class PxDpSpUtil { ...

  2. oracle视图总结(转)

    视图简介: 视图是基于一个表或多个表或视图的逻辑表,本身不包含数据,通过它可以对表里面的数据进行查询和修改.视图基于的表称为基表.视图是存储在数据字典里的一条select语句. 通过创建视图可以提取数 ...

  3. crontab无法调用java的问题解决

    本来想将写的代码挂在crontab下运行,谁知道无法运行,没有任何输出,试着用ls -al >> 1.log试了一下,确定crontab是正常运行的. 从网站上找了下问题,原因出在cron ...

  4. BZOJ1272: [BeiJingWc2008]Gate Of Babylon

    题解: 多重集合的组合数?还是0-m?有些元素有个数限制? 多重集合的组合数可以插板法,0-m直接利用组合数的公式一遍求出来,个数限制注意到只有15个,那我们就暴力容斥了 AC了真舒畅.. 注意开lo ...

  5. WebApp开发框架Ionic+AngularJS+Cordova

    目前的手机APP有三类:原生APP.WebAPP.HybridApp:HybridApp结合了前两类APP各自的优点,越来越流行. Ionic Ionic是一个新的.可以使用HTML5构建混合移动应用 ...

  6. poj 2553 The Bottom of a Graph

    求解的是有向图中满足“自己可达的顶点都能到达自己”的顶点个数如果强连通分量中某个顶点,还能到达分量外的顶点,则该连通分量不满足要求// 因此,本题要求的是将强连通分量缩点后所构造的新图中出度为0的顶点 ...

  7. Java—Map.Entry

    Map是java中的接口,Map.Entry是Map的一个内部接口. Map提供了一些常用方法,如keySet().entrySet()等方法. keySet()方法返回值是Map中key值的集合:e ...

  8. hdu 5407 CRB and Candies(组合数+最小公倍数+素数表+逆元)2015 Multi-University Training Contest 10

    题意: 输入n,求c(n,0)到c(n,n)的所有组合数的最小公倍数. 输入: 首行输入整数t,表示共有t组测试样例. 每组测试样例包含一个正整数n(1<=n<=1e6). 输出: 输出结 ...

  9. Map/Reduce之间的Partitioner接口

    一.Partitioner介绍 Partitioner的作用是对Mapper产生的中间结果进行分片,以便将同一分组的数据交给同一个Reduce处理,它直接影响Reduce阶段的负载均衡(个人理解:就是 ...

  10. hdu5073 简单枚举+精度处理

    其实这题还是挺简单的,因为移动k个星球后,这k个星球的权值就可以变为0,所以只有剩下的本来就是连着的才是最优解,也就是说要动也是动两端的,那么就O(N)枚举一遍动哪些就好了. 我是在杭电oj题目重现的 ...