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. window.location.href和window.open的几种用法和区别

    使用js的同学一定知道js的location.href的作用是什么,但是在js中关于location.href的用法究竟有哪几种,究竟有哪些区别,估计很多人都不知道了. 一.location.href ...

  2. 作业一直"执行"

    背景:一个作业有7个步骤,前面的步骤成功/失败都转到下一步,直至最后退出,作业计划是每天早上8点执行.步骤中的语句是例行检查脚本,之前的历史记录都是一分钟内完成.此次重启数据库服务器后,检查发现此作业 ...

  3. 用仿ActionScript的语法来编写html5——终篇,LegendForHtml5Programming1.0开源库件

    一,LegendForHtml5Programming1.0库件是什么?它是一个javascript库,它模仿了ActionScript的语法,用于html5的开发,目前实现的功能相对较少,还不能称之 ...

  4. 收集一些java相关的文章

    有些文章看了,以后想再看已经忘了在哪里了.故在此一一记录下那些值得看的文章. 1:如何确定Java对象的大小 中文版本 :http://article.yeeyan.org/view/104091/6 ...

  5. WebService 的一些基本概念

    一. 1.Endpoint http://www.ttdev.com/SimpleService 这个webservice全名就是所谓的"endpoint" 2.RPC type ...

  6. redhat 6.7 安装nvidia显卡驱动时出现的问题

    一.给Redhat装Nvidia驱动时,出现类似ERROR: The Nouveau kernel driver is currently in use by your system. 的错误,这是应 ...

  7. Swift游戏实战-跑酷熊猫 13 二段跳的实现

    这节内容我们来实现熊猫的二段跳. 要点: 二段跳的逻辑: 逻辑一,第一次点击屏幕,status就会变成jump. 逻辑二,第二次点击屏幕,status就会变成jump2. 逻辑三,当status变成j ...

  8. 当As3遇见Swift(三)

    类 As3 Swift中似乎没有包,包路径的概念.因而显得简洁的多. package { public class ShuaiGe { } } Swift类 class ShuaiGe{ } 类的构造 ...

  9. acm算法模板(5)

    STL 中 sort 函数用法简介 做 ACM 题的时候,排序是一种经常要用到的操作.如果每次都自己写个冒泡之类的 O(n^2) 排序,不但程序容易超时,而且浪费宝贵的比赛时间,还很有可能写错. ST ...

  10. [摘录]quarts:Quartz Quick Start Guide

    (Primarily authored by Dafydd James) Welcome to the QuickStart guide for Quartz. As you read this gu ...