hdu-5768 Lucky7(容斥定理+中国剩余定理)
题目链接:
Lucky7
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
?? 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.
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).
题意:
问[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(容斥定理+中国剩余定理)的更多相关文章
- hdu 5768 Lucky7 容斥
Lucky7 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...
- HDU - 4135 Co-prime 容斥定理
题意:给定区间和n,求区间中与n互素的数的个数, . 思路:利用容斥定理求得先求得区间与n互素的数的个数,设表示区间中与n互素的数的个数, 那么区间中与n互素的数的个数等于.详细分析见求指定区间内与n ...
- HDU 5514 Frogs 容斥定理
Frogs Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5514 De ...
- HDU5768Lucky7(中国剩余定理+容斥定理)(区间个数统计)
When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortun ...
- HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 1796How many integers can you find(简单容斥定理)
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
- HDU 4135 Co-prime 欧拉+容斥定理
Co-prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 1695 GCD(容斥定理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- 题解报告:hdu 4135 Co-prime(容斥定理入门)
Problem Description Given a number N, you are asked to count the number of integers between A and B ...
随机推荐
- 跟开涛学SpringMVC(4.1):Controller接口控制器详解(1)
http://www.importnew.com/19397.html http://blog.csdn.net/u014607184/article/details/52074530 https:/ ...
- yii框架:CDbConnection failed to open the DB connection: could not find driver的解决的方法
这个问题是由于php中缺少pdo mysql造成的. 解决方法是为php加入此扩展.前往你最早的php安装文件,进入ext/pdo_mysql/文件夹下,然后./configure --with-ph ...
- 【Spark】RDD操作具体解释4——Action算子
本质上在Actions算子中通过SparkContext运行提交作业的runJob操作,触发了RDD DAG的运行. 依据Action算子的输出空间将Action算子进行分类:无输出. HDFS. S ...
- sql的一些知识_函数_汇总数据
汇总数据 avg()---------求平均数 值得注意的是:avg()只能用于一个列的平均值查询,多个列的平均值请使用多个avg() avg()忽略null值 count()-------计数(指定 ...
- 百科知识 .tar.xz文件如何打开
7-ZIP可以打开,右击提取到当前目录即可 发现这个压缩比例还是相当不一般的,都快十倍了.
- [LeetCode][Java] Best Time to Buy and Sell Stock IV
题目: Say you have an array for which the ith element is the price of a given stock on day i. Design a ...
- meta标签多种用法
<meta name=”google” content=”notranslate” /> <!-- 有时,Google在结果页面会提供一个翻译链接,但有时候你不希望出现这个链接,你可 ...
- python(20)- 列表生成式和生成器表达式练习Ⅱ
题目一: 有两个列表,分别存放来老男孩报名学习linux和python课程的学生名字linux=['钢弹','小壁虎','小虎比','alex','wupeiqi','yuanhao']python= ...
- 数据迁移实战:基于Kettle的Mysql到DB2的数据迁移
From:https://my.oschina.net/simpleton/blog/525675 一.什么是ETL ETL,是英文 Extract-Transform-Load 的缩写,用来描述将数 ...
- 【转载】ASP.NET应用程序与页面生命周期
在本文中,我们将了解不同的事件,ASP.NET 应用程序的生命周期以浏览器向 Web 服务器(对于 ASP.NET 应用程序,通常为 IIS)发送请求为起点,直至将请求结果返回至浏览器结束.在这个过程 ...