题目链接:

Lucky7

Time Limit: 2000/1000 MS (Java/Others)  

  Memory Limit: 65536/65536 K (Java/Others)

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

题意:

问[l,r]中有多少个数%7==0且%pi!=ai;

思路:

范围太大,用容斥原理求出%7==0&&%pi==ai,的这些再加加减减;

CRT用的白书的板子;还不太会,明天来好好学学;代码参考了http://blog.csdn.net/danliwoo/article/details/52058069

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=20071027;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=(1<<8)+100;
const int maxn=(1<<8);
const double eps=1e-8; int n,flag[20];
LL L,R,m[20],a[20];
LL cal(LL x,LL y,LL mod)
{
LL s=0,base=x;
while(y)
{
if(y&1)s=(s+base)%mod;
base=(base+base)%mod;
y>>=1;
}
return s;
}
LL gao(LL x, LL r, LL p){
return (x-r)/p;
} void exgcd(LL fa,LL fb,LL &d,LL &x,LL &y)
{
if(fb==0){d=fa;x=1;y=0;}
else
{
exgcd(fb,fa%fb,d,y,x);
y-=x*(fa/fb);
}
}
inline LL CRT()
{
LL M=1,d,y,x=0;
For(i,0,n)if(flag[i])M=M*m[i];
For(i,0,n)
{
if(!flag[i])continue;
LL w=M/m[i];
exgcd(m[i],w,d,d,y);
y=(y%M+M)%M;
x=(x+cal(cal(y,w,M),a[i],M))%M;
}
x=(x+M)%M;
LL ans=gao(R+M,x,M)-gao(M+L-1,x,M);
return ans;
} int main()
{
int t,Case=0;
read(t);
while(t--)
{
read(n);read(L);read(R);
For(i,0,n-1)
{
read(m[i]);read(a[i]);
}
m[n]=7;a[n]=0;flag[n]=1;
int sum=(1<<n);
LL ans=0;
For(i,0,sum-1)
{
int num=0;
for(int j=0;j<n;j++)
{
if(i&(1<<j))flag[j]=1;
else flag[j]=0;
num+=flag[j];
}
if(num&1)num=-1;
else num=1;
ans=ans+num*CRT();
}
printf("Case #%d: %lld\n",++Case,ans);
} return 0;
}

  

最新文章

  1. 鼠标滚动div固定浮动-加锚点
  2. PHP组件化开发
  3. linux下使用localhost和127.0.0.1都不能连接的解决思路
  4. 基本概率分布Basic Concept of Probability Distributions 1: Binomial Distribution
  5. 初探接口测试框架--python系列4
  6. Swift概览
  7. TCP UDP 协议的选择
  8. MFC 堆栈溢出 test dword ptr [eax],eax ; probe page.
  9. Function.prototype.bind
  10. gpg XXX NO_PUBKEY XXX
  11. SpringMVC环境搭建---xml版及注解版
  12. Python Web-第三周-Networks and Sockets(Using Python to Access Web Data)
  13. Microsoft Soft SQL Server 大数据----分区表性能测试
  14. 一键脚本清理DEBIAN系统无用组件 减少系统资源
  15. win10个人助理conrtana软件能否支持用户反馈、后续优化
  16. unigui 在单据中,某输入为必填项的 JS代码
  17. Avro实现RPC
  18. 【376】COMP 9021 相关笔记(二)
  19. .NET 之 垃圾回收机制GC
  20. csharp: DataTable结构相同的比较

热门文章

  1. 使用spring声明式事务,spring使用AOP来支持声明式事务,会根据事务属性,自动在方法调用之前决定是否开启一个事务,并在方法执行之后决定事务提交或回滚事务。
  2. Pixhawk之姿态解算篇(1)_入门篇(DCM Nomalize)
  3. ShadowMap渲染阴影方法及问题 【转】
  4. python 常见细节知识点
  5. Python下opencv使用笔记(七)(图像梯度与边缘检測)
  6. java设计模式之-建造者模式
  7. Django的安装使用,以及建立本地网站
  8. 小白学phoneGap《构建跨平台APP:phoneGap移动应用实战》连载一(PhoneGap中的API)
  9. POJ 2249-Binomial Showdown(排列组合计数)
  10. match excel test search replace 用法