【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5768
题目大意:
T组数据,求L~R中满足:1.是7的倍数,2.对n个素数有 %pi!=ai 的数的个数。
题目思路:
【中国剩余定理】【容斥原理】【快速乘法】【数论】
因为都是素数所以两两互素,满足中国剩余定理的条件。
把7加到素数中,a=0,这样就变成解n+1个同余方程的通解(最小解)。之后算L~R中有多少解。
但是由于中国剩余定理的条件是同时成立的,而题目是或的关系,所以要用容斥原理叠加删减。
中间过程中可能会爆long long,所以要用快速乘法(和快速幂类似,只是乘改成加)
//
//by coolxxx
//
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define eps (1e-8)
#define J 10000000
#define MAX 0x7f7f7f7f
#define PI 3.1415926535897
#define N 24
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll;
LL l,r,ans;
LL p[N],a[N];
bool u[N];
LL cheng(LL a,LL b,LL mod)
{
LL sum=;
for(;b;b>>=)
{
if(b&1LL)sum=(sum+a)%mod;
a=(a+a)%mod;
}
return sum;
}
LL exgcd(LL a,LL b,LL &x,LL &y)
{
if(!b){x=,y=;return a;}
LL d=exgcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
LL china(int nn)
{
LL sum=,tot=,tott,x,y;
int i;
for(i=;i<=nn;i++)if(u[i])tot*=p[i];
for(i=;i<=nn;i++)
{
if(!u[i])continue;
tott=tot/p[i];
exgcd(tott,p[i],x,y);
x=(x%p[i]+p[i])%p[i];
sum=((sum+cheng(a[i]*tott%tot,x,tot))%tot+tot)%tot;
}
sum=(r+tot-sum)/tot-(l-+tot-sum)/tot;
return sum;
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k,ii;
// for(scanf("%d",&cas);cas;cas--)
for(scanf("%d",&cas),cass=;cass<=cas;cass++)
// while(~scanf("%s",s))
// while(~scanf("%d",&n))
{
ans=;
printf("Case #%d: ",cass);
scanf("%d%lld%lld",&n,&l,&r);
for(i=;i<=n;i++)
scanf("%lld%lld",&p[i],&a[i]);
lll=<<n;
n++;
u[n]=;p[n]=;a[n]=;
for(i=;i<lll;i++)
{
for(j=i,k=,ii=;ii<n;j>>=,ii++)
{
u[ii]=j&;
k+=u[ii];
}
k=k&?-:;
ans+=1LL*k*china(n);
}
printf("%lld\n",ans);
}
return ;
}
/*
// //
*/
【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7的更多相关文章
- HDU 5768 Lucky7 (中国剩余定理 + 容斥 + 快速乘法)
Lucky7 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...
- HDU 5768 Lucky7 (容斥原理 + 中国剩余定理 + 状态压缩 + 带膜乘法)
题意:……应该不用我说了,看起来就很容斥原理,很中国剩余定理…… 方法:因为题目中的n最大是15,使用状态压缩可以将所有的组合都举出来,然后再拆开成数组,进行中国剩余定理的运算,中国剩余定理能够求出同 ...
- HDU 5768:Lucky7(中国剩余定理 + 容斥原理)
http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Problem Description When ?? was born, seven ...
- HDU 5768 Lucky7 容斥原理+中国剩余定理(互质)
分析: 因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题.当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的 ...
- hdu 5768 Lucky7 中国剩余定理+容斥+快速乘
Lucky7 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem D ...
- HDU 5768 Lucky7 (中国剩余定理+容斥)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5768 给你n个同余方程组,然后给你l,r,问你l,r中有多少数%7=0且%ai != bi. 比较明显 ...
- ACM/ICPC 之 中国剩余定理+容斥原理(HDU5768)
二进制枚举+容斥原理+中国剩余定理 #include<iostream> #include<cstring> #include<cstdio> #include&l ...
- 中国剩余定理&Lucas定理&按位与——hdu 5446
链接: hdu 5446 http://acm.hdu.edu.cn/showproblem.php?pid=5446 题意: 给你三个数$n, m, k$ 第二行是$k$个数,$p_1,p_2,p_ ...
- hdu 5768 Lucky7 容斥
Lucky7 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...
随机推荐
- 给方法传递参数:ref参数和out参数
/*--------------------------------------------------- 给方法传递参数:ref参数和out参数 (P106) ------------------- ...
- jQuery中在当前页面弹出一个新的界面
W.$.dialog({ content:'url:wswgrkbillController.do?snh&id='+b+'&bh='+c+'&ck='+d+'&sl= ...
- MySQL存储过程详解 mysql 存储过程(二)
mysql存储过程详解 1. 存储过程简介 我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL ...
- 纯css+js水平时间轴
自定义,并自动加载时间节点 当前时间节点居中,突出显示 时间动态无痕添加 效果图: 初始状态 时间左走到一定2016.1月后 html: <!-- 水平时间轴 --> <div id ...
- 获取html页面所有的img标签
#region 获取html中所有Img Regex r = new Regex(@"<img[\s\S]*?>", RegexOptions.IgnoreCase); ...
- 在Cognos报表中使用钻取特性,参数传递
转载至:http://blog.sina.com.cn/s/blog_6eda1c4e0100mu3t.html Cognos的钻取方式大致可以分为三种: 1.模型固有的->由CUBE和DMR支 ...
- Objective-C学习篇01—类的声明与实现
Objective-C,是美国人布莱德·确斯(Brad Cox)于 1980 年年初发明的一 种程序设计语言,其与同时代的 C++ 一样,都是在 C 的基础上加入面向对象特性扩充而成的.Objecti ...
- Static Class (静态类)
一般情况下是不可以用static修饰类的.如果一定要用static修饰类的话,通常static修饰的是匿名内部类. 在一个类中创建另外一个类,叫做成员内部类.这个成员内部类可以静态的(利用static ...
- 浮点与整形在GUI下的相关思考
平时不接触绘图,似乎感觉不到浮点和整形所带来的区别,这次项目中意外的碰到了浮点与整形进行迁移的工作.因此写点心得,让自己以后也可以看看. 用浮点作图有个最大的弊端就是边界情况,比如你需要在点(20,2 ...
- Python md5 sha1 的使用
版本: Python 2.7 说明: Python 内置的 hashlib 模块中有 md5 和 sha1 加密方法,可以直接使用. md5加密 import hashlib data = 'This ...