【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7
题目链接:
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 ;
}
/*
// //
*/
【中国剩余定理】【容斥原理】【快速乘法】【数论】HDU 5768 Lucky7的更多相关文章
- HDU 5768 Lucky7 (中国剩余定理 + 容斥 + 快速乘法)
Lucky7 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...
- HDU 5768 Lucky7 (容斥原理 + 中国剩余定理 + 状态压缩 + 带膜乘法)
题意:……应该不用我说了,看起来就很容斥原理,很中国剩余定理…… 方法:因为题目中的n最大是15,使用状态压缩可以将所有的组合都举出来,然后再拆开成数组,进行中国剩余定理的运算,中国剩余定理能够求出同 ...
- HDU 5768:Lucky7(中国剩余定理 + 容斥原理)
http://acm.hdu.edu.cn/showproblem.php?pid=5768 Lucky7 Problem Description When ?? was born, seven ...
- HDU 5768 Lucky7 容斥原理+中国剩余定理(互质)
分析: 因为满足任意一组pi和ai,即可使一个“幸运数”被“污染”,我们可以想到通过容斥来处理这个问题.当我们选定了一系列pi和ai后,题意转化为求[x,y]中被7整除余0,且被这一系列pi除余ai的 ...
- hdu 5768 Lucky7 中国剩余定理+容斥+快速乘
Lucky7 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem D ...
- HDU 5768 Lucky7 (中国剩余定理+容斥)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5768 给你n个同余方程组,然后给你l,r,问你l,r中有多少数%7=0且%ai != bi. 比较明显 ...
- ACM/ICPC 之 中国剩余定理+容斥原理(HDU5768)
二进制枚举+容斥原理+中国剩余定理 #include<iostream> #include<cstring> #include<cstdio> #include&l ...
- 中国剩余定理&Lucas定理&按位与——hdu 5446
链接: hdu 5446 http://acm.hdu.edu.cn/showproblem.php?pid=5446 题意: 给你三个数$n, m, k$ 第二行是$k$个数,$p_1,p_2,p_ ...
- hdu 5768 Lucky7 容斥
Lucky7 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5768 Description When ?? was born, seven crow ...
随机推荐
- 那些年不错的Android开源项目(转)
第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Pro ...
- Android(java)学习笔记246:ContentProvider使用之学习ContentProvider(内容提供者)的目的
1.使用ContentProvider,把应用程序私有的数据暴露给别的应用程序,让别的应用程序完成对自己私有的数据库数据的增删改查的操作. 2.ContentProvider的应用场景: 获取手机系统 ...
- yii phpexcel <转>
原文详情参见 这里 1.下载phpexcel,将压缩包中的classes复制到protected/extensions下并修改为PHPExcel. 2.修改YII配置文件config/main.php ...
- Bellman_ford POJ 3259 Wormholes
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 41728 Accepted: 15325 Descr ...
- Linux access
1.access函数 功能描述:检查调用进程是否可以对指定的文件执行某种操作. 用法: #include <unistd.h> #include <fcntl.h>int ac ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
- 刷新的时候jquery获取checkbox是否为选中和设置选中
$(document).ready(function(){ $('.uninstall_list_checkbox').click(function(){ if($(this).parent('.un ...
- ASPNET5应用程序启动
1. 启动类 在asp.net5当中,Startup类提供应用程序的入口,对所有应用程序是必须的.争对特定环境的配置的starup class和方法也是有可能的, 但是,不管怎样, 一个Startup ...
- Android开发手记(19) 数据存储四 ContentProvider
转载自:http://www.cnblogs.com/devinzhang/archive/2012/01/20/2327863.html Android为数据存储提供了五种方式: 1.SharedP ...
- Swift - 21 - 字典实战和UIKit初探
//: Playground - noun: a place where people can play import UIKit // 数据源 let colors = [ "Air Fo ...