题目链接:

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

  

hdu-5768 Lucky7(容斥定理+中国剩余定理)的更多相关文章

  1. hdu 5768 Lucky7 容斥

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

  2. HDU - 4135 Co-prime 容斥定理

    题意:给定区间和n,求区间中与n互素的数的个数, . 思路:利用容斥定理求得先求得区间与n互素的数的个数,设表示区间中与n互素的数的个数, 那么区间中与n互素的数的个数等于.详细分析见求指定区间内与n ...

  3. HDU 5514 Frogs 容斥定理

    Frogs Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5514 De ...

  4. HDU5768Lucky7(中国剩余定理+容斥定理)(区间个数统计)

    When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortun ...

  5. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. HDU 1796How many integers can you find(简单容斥定理)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  7. HDU 4135 Co-prime 欧拉+容斥定理

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  8. HDU 1695 GCD(容斥定理)

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  9. 题解报告:hdu 4135 Co-prime(容斥定理入门)

    Problem Description Given a number N, you are asked to count the number of integers between A and B ...

随机推荐

  1. angular http ajax header

    myAppModule.config(['$httpProvider', function($httpProvider) { $httpProvider.defaults.headers.common ...

  2. ubuntu 配置 django

    安装 安装Apache sudo apt-get install apache2 安装Django 下载Django 安装mod_wsgi sudo apt-get install libapache ...

  3. 一天时间用OpenFire打造自己的IM聊天工具

    Openfire采用Java开发,开源的实时协作(RTC)服务器基于XMPP(Jabber)协议.Openfire安装和使用都非常简单,并利用Web进行管理.单台服务器可支持上万并发用户. 好友界面 ...

  4. intent 支持的action 动作

    String ACTION_AIRPLANE_MODE_CHANGED Broadcast Action: The user has switched the phone into or out of ...

  5. [Guava源代码阅读笔记]-Basic Utilities篇-1

    欢迎訪问:个人博客 写该系列文章的目的是记录Guava源代码中个人感觉不错且值得借鉴的内容. 一.MoreObjects类 //MoreObjects.ToStringHelper类的toString ...

  6. java开始到熟悉103-104

    本次内容:linkedlist() 此次是承接上次arraylist(),自己实现linkedlist()(内容较少) package list; /** * 自定义linkedlist类 * @au ...

  7. windows ffmpeg 推送摄像头数据到rtmp服务

    文本主要讲述windows系统下如何利用ffmpeg获取摄像机流并推送到rtmp服务,命令的用法前文 中有讲到过,这次是通过代码来实现.实现该项功能的基本流程如下: 图1 ffmpeg推流流程图 较前 ...

  8. 服务器文档下载zip格式 SQL Server SQL分页查询 C#过滤html标签 EF 延时加载与死锁 在JS方法中返回多个值的三种方法(转载) IEnumerable,ICollection,IList接口问题 不吹不擂,你想要的Python面试都在这里了【315+道题】 基于mvc三层架构和ajax技术实现最简单的文件上传 事件管理

    服务器文档下载zip格式   刚好这次项目中遇到了这个东西,就来弄一下,挺简单的,但是前台调用的时候弄错了,浪费了大半天的时间,本人也是菜鸟一枚.开始吧.(MVC的) @using Rattan.Co ...

  9. AsyncTask源代码解析

    快要毕业了.近期在阿里巴巴校园招聘面试,一面过了,感觉挺轻松,可能是运气好.面试官感觉比我腼腆一些.我俩从android绕到了spring mvc 到数据库悲观锁 到linux 然后又会到了andro ...

  10. SVProgressHUD 用法一

    SVProgressHUD 用法一  SVProgressHUD 是一个第三方的控件,是一个弹出提示层,用来提示 网络加载 或 提示对错,看下面图,你就明白了:     那么,SVProgressHU ...