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. [ArcEngine]IFeatureBuffer使用

    public static void LoadOnlyModeInsert(IFeatureClass featureClass, List < IGeometry > geometryL ...

  2. Vue.2.0.5-混合

    基础 混合是一种灵活的分布式复用 Vue 组件的方式.混合对象可以包含任意组件选项.以组件使用混合对象时,所有混合对象的选项将被混入该组件本身的选项. 例子: // 定义一个混合对象 var myMi ...

  3. 关于IOS框架的解释

  4. iOS 从手机相册里选取图片

    #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> @pr ...

  5. maven 一好用的仓库镜像

    <mirror> <id>ibiblio.org</id> <name>ibiblio Mirror of http://repo1.maven.org ...

  6. c# yyyyMMdd,dd/MM/yyyy 类型字符串转换为datetime 类型

    DateTime ConvertDate = DateTime.ParseExact("20140504", "yyyyMMdd", null, System. ...

  7. ObjectMonitor,ObjectWaiter 实现wait(),notify()

    0.java对象锁监视器 在JVM的规范中,有这么一些话:“在JVM中,每个对象和类在逻辑上都是和一个监视器相关联的”“为了实现监视器的排他性监视能力,JVM为每一个对象和类都关联一个锁”“锁住了一个 ...

  8. Oracle-数据库

    Oracle 1.特点 关系型数据库 采用二维表的行使管理数据库 具有行和列  表间存在关联关系 2.安装 数据库(11g) 版本类型 32位    64位  安装类型 桌面类 本机开发 服务器类 生 ...

  9. 使用gradle创建java程序

    创建一个Java项目 我们可以使用Java插件来创建一个Java项目,为了做到这点,我们需要把下面这段语句加入到build.gradle文件中: 1 apply plugin: 'java' 就是这样 ...

  10. UISearchController的使用

    - (void)addSearchController { _searchController = [[UISearchController alloc] initWithSearchResultsC ...