分析:

因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题。当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的数的个数,可以看成若干个同余方程联立成的一次同余方程组。然后我们就可以很自然而然的想到了中国剩余定理。需要注意的是,在处理中国剩余定理的过程中,可能会发生超出LongLong的情况,需要写个类似于快速幂的快速乘法来处理。

吐槽:赛场上不会快速乘,导致疯狂WA,唉,还是太年轻

代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long LL;
const int N = ;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9+;
void exgcd(LL a,LL b,LL &d,LL& x,LL& y)
{
if(!b)
{
d=a;
x=;
y=;
}
else
{
exgcd(b,a%b,d,y,x);
y-=x*(a/b);
}
}
LL a[],m[];
/*对应的模线性方程组,余数放在a数组,模数放在m数组*/
LL qpow(LL a,LL b,LL mod){
a%=mod;
LL ret=;
while(b){
if(b&)ret=(ret+a)%mod;
b>>=;
a=(a+a)%mod;
}
return ret;
}
LL china(int n,LL* a,LL *m)
{
LL M=,d,y,x=;
for(int i=; i<n; ++i)M*=m[i];
for(int i=; i<n; ++i)
{
LL w=M/m[i];
exgcd(m[i],w,d,d,y);
x=(x+qpow(qpow(y,w,M),a[i],M))%M;
}
return (x+M)%M;
}
LL p[N],yu[N];
int main(){
int kase=,n,T;
scanf("%d",&T);
while(T--){
LL l,r;
scanf("%d",&n);
cin>>l>>r;
for(int i=;i<n;++i)
cin>>p[i]>>yu[i];
int len=(<<n);
LL ret=r/-(l-)/;
for(int i=;i<len;++i){
int cnt=;
LL cur=;
for(int j=;j<n;++j){
if(i&(<<j)){
m[cnt]=p[j];
a[cnt]=yu[j];
cnt++;
cur*=p[j];
}
}
m[cnt]=;a[cnt]=;
cur*=;
cnt++;
LL tmp=china(cnt,a,m);
LL sub=;
if(tmp>=l&&tmp<=r){
LL cha=r-tmp;
sub=cha/cur+;
}
else if(tmp<l){
LL cha=l-tmp;
tmp+=cha/cur*cur;
if(tmp<l)tmp+=cur;
if(tmp>=l&&tmp<=r){
cha=r-tmp;
sub=cha/cur+;
}
}
if(cnt&)ret+=sub;
else ret-=sub;
}
printf("Case #%d: %I64d\n",++kase,ret);
}
return ;
}

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

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

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

  2. 模板-->中国剩余定理[互质版本]

    如果有相应的OJ题目,欢迎同学们提供相应的链接 相关链接 所有模板的快速链接 扩展欧几里得extend_gcd模板 poj_1006_Biorhythms,my_ac_code 简单的测试 None ...

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

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

  4. HDU 5768:Lucky7(中国剩余定理 + 容斥原理)

    http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Problem Description   When ?? was born, seven ...

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

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

  6. hdu X问题 (中国剩余定理不互质)

    http://acm.hdu.edu.cn/showproblem.php?pid=1573 X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory ...

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

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

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

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

  9. hdu 5768 Lucky7 容斥

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

随机推荐

  1. POJ2996Help Me with the Game

    http://poj.org/problem?id=2996 又是一个大模拟题,表示做模拟题做的恶心,这个题主要是对数据的处理,从表格中将数据取出来再进行处理即可. 主要注意的点就是:1.KQRBN五 ...

  2. LeeCode 1-Two Sum

    Two Sum Total Accepted: 125096 Total Submissions: 705262 Question Solution Given an array of integer ...

  3. Filter高级开发

    孤傲苍狼 只为成功找方法,不为失败找借口! javaweb学习总结(四十三)——Filter高级开发 在filter中可以得到代表用户请求和响应的request.response对象,因此在编程中可以 ...

  4. DOS命令 Net config server Net config workstation

    DOS命令 Net config 作用:显示当前运行的可配置服务,或显示并修改某项服务的设置. 格式:net conifg service options 参数:(1)键入不带参数的net conif ...

  5. 62. Unique Paths

    题目: A robot is located at the top-left corner of a m x ngrid (marked 'Start' in the diagram below). ...

  6. 52. N-Queens II

    题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...

  7. 48. Rotate Image

    题目: You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees (clockwis ...

  8. C++:派生类的构造函数和析构函数

    4.2 派生类的构造函数和析构函数4.2.1 派生类构造函数和析构函数的执行顺序 通常情况下,当创建派生类对象时,首先执行基类的构造函数,随后再执行派生类的构造函数:当撤销派生类对象时,则先执行派生类 ...

  9. jps

    jps(Java Virtual Machine Process Status Tool)是JDK 1.5提供的一个显示当前所有java进程pid的命令,简单实用,非常适合在linux/unix平台上 ...

  10. linux PATH环境变量

    $PATH:决定了shell将到哪些目录中寻找命令或程序,PATH的值是一系列目录,当您运行一个程序时,Linux在这些目录下进行搜寻编译链接 shell下输出path值: echo $PATH: w ...