题目链接:

  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 ;
}
/*
// //
*/

最新文章

  1. 发一则自己创作的Lae程序员小漫画,仅供一乐
  2. POJ 1226 后缀数组
  3. 【C语言学习】-08 指针
  4. C#照片批量压缩小工具
  5. C++ 之 常量成员函数
  6. centos 6.4下的postgresql 9.2使用
  7. java实现Composite(组合)模式
  8. C++ STL之list容器的基本操作
  9. 本地计算机上的OracleOraDb11g_home2TNSListener服务启动又停止了。
  10. 学习面向对象编程OOP 第二天
  11. Android IntentService使用介绍以及源码解析
  12. Navicat的使用
  13. vue菜鸟从业记:没准备好的面试,那叫尬聊
  14. [luogu P2205] [USACO13JAN]画栅栏Painting the Fence
  15. iOS 技术篇: 如何利用dsym文件分析苹果被拒日志
  16. css权重计算规则
  17. Sqrt Bo (水题)
  18. JVM调优总结 -Xms -Xmx -Xmn -Xss(转自:iteye unixboy)
  19. CodeForces - 1003D
  20. php如何实现定时任务,php定时任务方法,最佳解决方案,php自动任务处理

热门文章

  1. Reso | liunx下longeneQQ和搜狗拼音
  2. 使用CSS、JS的几种方法
  3. 当ViewPager嵌套在ScrollView/ListView里时,手势冲突如何处理?
  4. Linq101-Projection
  5. 【转】UITextView的使用详解
  6. iPhone真机测试Crash信息分析
  7. javascript权威指南学习笔记2
  8. java-二分查找法
  9. jQuery获取JSON格式数据方法
  10. 希尔排序(Shell&#39;s Sort)的C语言实现