hdu_5768_Lucky7(中国剩余定理+容斥)
题目链接:hdu_5768_Lucky7
题意:
给你一个区间,问你这个区间内是7的倍数,并且满足%a[i]不等于w[i]的数的个数
乍一看以为是数位DP,仔细看看条件,发现要用中国剩余定理,然后容斥一下就出答案了,不过这里在中国剩余定理里面的乘法会有数据爆long long ,所有要写一个高精度乘法,这里卡死很多人、
#include <bits/stdc++.h>
#define F(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef long long ll; ll a[],w[],l,r;
int t,n,vis[],ic=; ll mut(ll a,ll b,ll mod)//高精度求乘法
{
ll an=;
while(b){if(b&)an=(an+a)%mod;b>>=,a=(a<<)%mod;}
return an;
} ll Extended_Euclid(ll a, ll b, ll &x, ll &y)//扩展欧几里得算法
{
if(b==){x=,y=;return a;}
ll d=Extended_Euclid(b,a%b,y,x);
y-=a/b*x;
return d;
} inline ll cal(ll r,ll l,ll m){return (r-l)/m;} ll Chinese_Remainder(int len=n+)//中国剩余定理,a[]存放余数,w[]存放两两互质的数
{
ll x,y,m,N=,ret=;
F(i,,len)if(vis[i])N*=w[i];//从1开始
F(i,,len)if(vis[i])m=N/w[i],Extended_Euclid(w[i],m,x,y),
ret=(ret+mut(mut(y,m,N),a[i],N))%N;//注意是否爆ll
ret=(N+ret%N)%N;
return cal(r+N,ret,N)-cal(l-+N,ret,N);
} int main()
{
scanf("%d",&t);
w[]=,a[]=,vis[]=;
while(t--)
{
scanf("%d%lld%lld",&n,&l,&r);
F(i,,n+)scanf("%d%d",w+i,a+i);
F(i,,n+)vis[i]=;
ll ans=;
int end=<<n;
for(int i=;i<end;i++)
{
int tp=i,cnt=;
F(j,,n+)vis[j]=tp&,tp>>=,cnt+=vis[j];
cnt=cnt&?-:;
ans+=1ll*cnt*Chinese_Remainder();
}
printf("Case #%d: %lld\n",ic++,ans);
}
return ;
}
hdu_5768_Lucky7(中国剩余定理+容斥)的更多相关文章
- HDU 5768 Lucky7 (中国剩余定理+容斥)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5768 给你n个同余方程组,然后给你l,r,问你l,r中有多少数%7=0且%ai != bi. 比较明显 ...
- hdu 5768 Lucky7 中国剩余定理+容斥+快速乘
Lucky7 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem D ...
- HDU 5768 Lucky7 (中国剩余定理 + 容斥 + 快速乘法)
Lucky7 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...
- HDU5768Lucky7(中国剩余定理+容斥定理)(区间个数统计)
When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortun ...
- [BZOJ 3129] [Sdoi2013] 方程 【容斥+组合数取模+中国剩余定理】
题目链接:BZOJ - 3129 题目分析 使用隔板法的思想,如果没有任何限制条件,那么方案数就是 C(m - 1, n - 1). 如果有一个限制条件是 xi >= Ai ,那么我们就可以将 ...
- bzoj 3782 上学路线 卢卡斯定理 容斥 中国剩余定理 dp
LINK:上学路线 从(0,0)走到(n,m)每次只能向上或者向右走 有K个点不能走求方案数,对P取模. \(1\leq N,M\leq 10^10 0\leq T\leq 200\) p=10000 ...
- HDU 5768Lucky7(多校第四场)容斥+中国剩余定理(扩展欧几里德求逆元的)+快速乘法
地址:http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Time Limit: 2000/1000 MS (Java/Others) M ...
- ACM/ICPC 之 中国剩余定理+容斥原理(HDU5768)
二进制枚举+容斥原理+中国剩余定理 #include<iostream> #include<cstring> #include<cstdio> #include&l ...
- HDU 5768 Lucky7 容斥原理+中国剩余定理(互质)
分析: 因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题.当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的 ...
随机推荐
- 关于jQuery表单校验
<style> .red{border: 1px solid red;} .wrong-tip{color: red;} </style> <form action=&q ...
- freemarker + spring mvc + spring + mybatis + mysql + maven项目搭建
今天说说搭建项目,使用freemarker + spring mvc + spring + mybatis + mysql + maven搭建web项目. 先假设您已经配置好eclipse的maven ...
- 找工作---操作系统常考知识点总结【PB】
1.进程是并发过程中程序的执行过程 2.进程的特征:结构特征.动态性.并发性.独立性.异步性 3.临界区指在每个进程中访问临界资源的那段代码 4,现在操作系统中申请资源的基本单位是进程,在CPU得到执 ...
- EasyUI 日期选择插件封装成选择到月份的插件
将普通的日期选择插件封装成选择到月份的插件: var nowMonth = new Date(); var month = ...
- hdu1008
//c++// #includeusing namespace std;int main(){int n,j,t,start;while (cin >> n,n){start =0;t = ...
- em,strong,b,i的区别
文章来源: http://www.zhihu.com/question/19551271 默认样式: strong=b=粗体 em=i=斜体 HTML4.01: strong,em代表语义,从语义 ...
- Java中的DateFormatter
字母 日期或时间元素 表示 示例 G Era 标志符 Text AD y 年 Year 1996; 96 M 年中的月份 Month July; Jul;07 w 年中的周数 Number 27 W ...
- Spring Security(09)——Filter
目录 1.1 Filter顺序 1.2 添加Filter到FilterChain 1.3 DelegatingFilterProxy 1.4 FilterChainPr ...
- MATLAB制作符合IEEE标准的图插入Latex
1.MATLAB最好保存为eps格式,虽然IEEE也支持png等其他格式,但是MATLAB在保存为png格式时,很容易在后期插图时,出现分辨率不足等问题. 2. MATLAB在save as图片的时候 ...
- 浅析const标识符在C++函数的功能
范例: class matrix { public: matrix(){}; const double getvalue(const unsigned row, const unsigned colu ...