hdu 5768 Lucky7 中国剩余定理+容斥+快速乘
Lucky7
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
?? 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.
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).
2 1 100
3 2
5 3
0 1 100
Case #2: 14
For Case 1: 7,21,42,49,70,84,91 are the seven numbers.
For Case2: 7,14,21,28,35,42,49,56,63,70,77,84,91,98 are the fourteen numbers.
2016 Multi-University Training Contest 4
思路:套一个中国剩余定理两两不互质的模版;
容斥一发。。。由于犯了一个sb错,wa一天,不想说什么了;
1005 Lucky7
因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题。当我们选定了一系列pi和ai后,题意转化为求 [x,y]中被7整除余0,且被这一系列pi除余ai的数的个数,可以看成若干个同余方程联立成的一次同余方程组。然后我们就可以很自然而然的想到了中国 剩余定理。需要注意的是,在处理中国剩余定理的过程中,可能会发生超出LongLong的情况,需要写个类似于快速幂的快速乘法来处理。
二进制枚举:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll __int64
#define esp 0.00000000001
const int N=1e2+,M=1e6+,inf=1e9+,mod=;
ll a[N];
ll b[N];
ll p[N];
ll m[N];
ll mulmod(ll x,ll y,ll m)
{
ll ans=;
while(y)
{
if(y%)
{
ans+=x;
ans%=m;
}
x+=x;
x%=m;
y/=;
}
ans=(ans+m)%m;
return ans;
}
void exgcd(ll a, ll b, ll &x, ll &y)
{
if(b == )
{
x = ;
y = ;
return;
}
exgcd(b, a % b, x, y);
ll tmp = x;
x = y;
y = tmp - (a / b) * y;
}
ll CRT(ll a[],ll m[],ll n)
{
ll M = ;
ll ans = ;
for(ll i=; i<n; i++)
M *= m[i];
for(ll i=; i<n; i++)
{
ll x, y;
ll Mi = M / m[i];
exgcd(Mi, m[i], x, y);
//ans = (ans + Mi * x * a[i]) % M;
ans = (ans +mulmod( mulmod( x , Mi ,M ), a[i] , M ) ) % M;
}
ans=(ans + M )% M;
return ans;
}
int main()
{
ll x,y,z,i,t;
int T,cas=;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d%I64d",&x,&y,&z);
for(i=;i<=x;i++)
scanf("%I64d%I64d",&b[i],&a[i]);
ll ans=;
for(i=;i<(<<x);i++)
{
ll cnt=,mul=;
p[cnt]=;
m[cnt++]=;
for(int ji=,t=i;t>;t>>=,ji++)
if(t&)mul*=b[ji],p[cnt]=a[ji],m[cnt++]=b[ji];
ll pp=CRT(p,m,cnt);
if(cnt&)
ans+=z/mul+(z%mul>=pp)-((y-)/mul+(((y-)%mul)>=pp));
else
ans-=z/mul+(z%mul>=pp)-((y-)/mul+(((y-)%mul)>=pp));
}
printf("Case #%d: %I64d\n",cas++,ans);
}
return ;
}
dfs写法:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll __int64
#define esp 0.00000000001
const int N=1e2+,M=1e6+,inf=1e9+,mod=;
ll a[N];
ll b[N];
ll ji;
ll mulmod(ll x,ll y,ll m)
{
ll ans=;
while(y)
{
if(y%)
{
ans+=x;
ans%=m;
}
x+=x;
x%=m;
y/=;
}
ans=(ans+m)%m;
return ans;
}
void exgcd(ll a, ll b, ll &x, ll &y)
{
if(b == )
{
x = ;
y = ;
return;
}
exgcd(b, a % b, x, y);
ll tmp = x;
x = y;
y = tmp - (a / b) * y;
}
ll CRT(ll p,ll m,ll k,ll l)
{
ll M = m * l;
ll ans = ;
ll x, y;
ll Mi = l;
exgcd(Mi, m, x, y);
ans = (ans + mulmod( mulmod( x%M, Mi%M, M) , p%M , M))% M;
Mi = m;
exgcd(Mi, l, x, y);
ans = (ans + mulmod( mulmod(x%M ,Mi%M , M) , k%M, M))% M;
if(ans < ) ans += M;
return ans;
}
void dfs(ll p,ll m,ll pos,ll step,ll x,ll &ans)
{
if(pos==ji)
{
if(step%)
{
ans-=x/m;
if(x%m>=p)
ans--;
}
else
{
ans+=(x/m);
if(x%m>=p)
ans++;
}
return;
}
dfs(CRT(p,m,a[pos],b[pos]),m*b[pos],pos+,step+,x,ans);
dfs(p,m,pos+,step,x,ans);
}
int main()
{
ll x,y,z,i,t;
int T,cas=;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d%I64d",&x,&y,&z);
ji=x;
for(i=; i<x; i++)
scanf("%I64d%I64d",&b[i],&a[i]);
ll ansr=,ansl=;
dfs(,,,,z,ansr);
dfs(,,,,y-,ansl);
printf("Case #%d: %I64d\n",cas++,ansr-ansl);
}
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 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...
- hdu_5768_Lucky7(中国剩余定理+容斥)
题目链接:hdu_5768_Lucky7 题意: 给你一个区间,问你这个区间内是7的倍数,并且满足%a[i]不等于w[i]的数的个数 乍一看以为是数位DP,仔细看看条件,发现要用中国剩余定理,然后容斥 ...
- HDU5768Lucky7(中国剩余定理+容斥定理)(区间个数统计)
When ?? was born, seven crows flew in and stopped beside him. In its childhood, ?? had been unfortun ...
- 【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 题目大意: T组数据,求L~R中满足:1.是7的倍数,2.对n个素数有 %pi!=ai 的数 ...
- hdu 5768 Lucky7 容斥
Lucky7 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...
- HDU 5768 Lucky7 容斥原理+中国剩余定理(互质)
分析: 因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题.当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的 ...
- HDU 5768 Lucky7 (容斥原理 + 中国剩余定理 + 状态压缩 + 带膜乘法)
题意:……应该不用我说了,看起来就很容斥原理,很中国剩余定理…… 方法:因为题目中的n最大是15,使用状态压缩可以将所有的组合都举出来,然后再拆开成数组,进行中国剩余定理的运算,中国剩余定理能够求出同 ...
- HDU 5768 Lucky7(CRT+容斥原理)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5768 [题目大意] 求出一个区间内7的倍数中,对于每个ai取模不等于bi的数的个数. [题解] 首 ...
随机推荐
- 如何用redis来生成唯一Id
在之前的项目中需要用到一个自动增长的主键,该主键需要包含字母,所以没有办法用到数据库的自增主键.楼主要高手的指导下,发现Redis的RedisAtomicLong类可以解决这个麻烦.而且redis为单 ...
- android studio 运行是,app标题栏不显示
解决办法:让所有的活动都继承 AppCompatActivity就行了,如: public class FirstActivity extends AppCompatActivity{ ... }
- HTTP协议及其POST与GET操作差异 & C#中如何使用POST、GET等
转自: http://www.cnblogs.com/skynet/archive/2010/05/18/1738301.html 引言 HTTP协议我想任何IT人士都耳熟能详了,大家都能说出个所以然 ...
- python scrapy,beautifulsoup,regex,sgmparser,request,connection
In [2]: import requests In [3]: s = requests.Session() In [4]: s.headers 如果你是爬虫相关的业务?抓取的网站还各种各样, ...
- Charles抓包工具简单操作
一.界面介绍 1.功能是clear,清理掉所有请求显示信息. 2.功能是搜索关键字,也可以使用ctrl+f实现,可以设置搜索的范围 3.功能是开始或暂停 4.显示所抓取的数据包 5.抓取数据包的请求及 ...
- Struts2.0 封装请求数据和拦截器介绍
1. Struts2 框架中使用 Servlet 的 API 来操作数据 1.1 完全解耦合的方式 Struts2 框架中提供了一个 ActionContext 类,该类中提供了一些方法: stati ...
- 原!!junit mockito 自定义参数匹配 -- ArgumentMatcher
前两天写单元测试的时候,发现一个dao对象 mock成功了,但是调用该dao对象的某个方法时,并没有按照设定的值返回,而是返回null. 但是记得之前也都是这么写没有碰到问题,直接mock对象,调用方 ...
- slurm使用
官方文档:https://slurm.schedmd.com/ 用户命令cheatsheet:https://slurm.schedmd.com/pdfs/summary.pdf 占用GPU sall ...
- Python的3个方法:静态方法(staticmethod),类方法(classmethod)和实例方法
Python的方法主要有3个,即静态方法(staticmethod),类方法(classmethod)和实例方法,如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
- 通过文件对照工具Merge数据库
项目分成线下开发版.线上測试版.线上生产版,因此相应有三个数据库. 对于一些静态数据.经常须要同步.改动了线下的开发版本号,同一时候也须要更新线上的測试版和线上生产版数据库,有时候线上的一些数据库改动 ...