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. VMWare ESXi 5.5安装及配置

    VMWare ESXi 5.5安装大概过程如下:制作虚拟化ESXi系统的USB启动盘,安装ESXi系统到USB,用USB启动ESXi系统.比较难理解,下面图解过程. 下载UNetbootin   (下 ...

  2. [HDF]hdf-4.2.6类库的使用

    HDF文件包括科学数据和VData部分.读取HDF格式的calipso数据,用GDAL可以方便的读取其中的子数据集,但是没有发现GDAL中提供读取Vdata的方法.所以只好考虑借助hdf-4.2.6类 ...

  3. redmine一键安装

    参加项目 bitnami项目  https://bitnami.com/stack/redmine/installer 百度网盘地址为:http://pan.baidu.com/s/1jESnO

  4. c#异步调用

    首先来看一个简单的例子: 小明在烧水,等水烧开以后,将开水灌入热水瓶,然后开始整理家务 小文在烧水,在烧水的过程中整理家务,等水烧开以后,放下手中的家务活,将开水灌入热水瓶,然后继续整理家务 这也是日 ...

  5. jQuery uploadify上传文件404,500错误

    1.如果部署到了IIS7的话,IIS7默认的大小为3000000.修改方法如下: 找到网站双击“请求筛选”——右边找到“编辑功能设置”——将“允许的最大内容长度”改成你想要的就行了. 2.当上传大文件 ...

  6. <s:iterator> 对list操作的一种方法

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA6IAAAH3CAIAAAAuRnW9AAAgAElEQVR4nOzdf4gk1333+wLDDffhPp

  7. powershell 参数 [String]Service

    此种情况,去掉前面的[String] 在里面操作的时候就会认为是string,并可以自动操作了,否则限定为String类型时,就无法将输入的a,b当作String了, 或者需要添加'a,b'单引号来变 ...

  8. Ruby界面开发--wxRuby库TextCtrl相关问题

    界面库官方教程:(1) 总的各种库函数讲解http://wxruby.rubyforge.org/doc/index.html (2)TextCtrl讲解 http://wxruby.rubyforg ...

  9. MyEclipse 下 Tomcat启动变慢如何解决

    MyEclipse   下  Tomcat启动变慢如何解决 项目使用debug启动有时候会突然变得非常慢.不但启动慢,启动之后连打开项目页面也很慢,是日常的4,5倍.可以有下面的几种解决方法: 1. ...

  10. 封装page分页类

    类: <?php //分页工具类 class Page{ /*         * 获取分页字符串         * @param1 string $uri,分页要请求的脚本url       ...