gcd,lcm,ext_gcd,inv
Least Common Multiple http://acm.hdu.edu.cn/showproblem.php?pid=1019
#include<cstdio>
int gcd(int a,int b){
return b?gcd(b,a%b):a;
}
int lcm(int a,int b){
return a/gcd(a,b)*b;
}
int main(){
int n,m,ans,x;
while(~scanf("%d",&n)){
while(n--){
ans=;
scanf("%d",&m);
while(m--){
scanf("%d",&x);
ans=lcm(ans,x);
}
printf("%d\n",ans);
}
}
return ;
}
Turn the pokers http://acm.hdu.edu.cn/showproblem.php?pid=4869
#include<cstdio>
#include<algorithm>
using namespace std;
typedef __int64 LL;
const int M=;
const int mod=;
int ext_gcd(int a,int b,int &x,int &y){//扩展gcd d=gcd(a,b)=a*x+b*y; return d,x,y;
int t,ret;
if(!b){
x=;
y=;
return a;
}
ret=ext_gcd(b,a%b,x,y);
t=x;
x=y;
y=t-a/b*y;
return ret;
}
int inv(int a,int b,int c){//ext_gcd求逆元 (b/a)%c
int x,y;
ext_gcd(a,c,x,y);
return (1LL*x*b%c+c)%c;
}
LL C[M];
LL INV[M];
int main() {
for(int i=; i<M; i++) {
INV[i]=inv(i,,mod);
}
int n,m,a;
while(~scanf("%d%d",&n,&m)) {
C[]=;
for(int i=;i<=m;i++){
C[i]=C[i-]*(m-i+)%mod*INV[i]%mod;
}
int L=,R=,nl,nr,tmp;
for(int i=;i<n;i++){
scanf("%d",&a);
tmp=min(m-L,a);
nr=L+tmp-(a-tmp);
tmp=min(R,a);
nl=R-tmp+(a-tmp);
if(nl>nr) swap(nl,nr);
if(L<=a&&a<=R){
if(L%==a%){
nl=;
}
else{
nl=min(nl,);
}
}
if((m-R)<=a&&a<=(m-L)){
if((m-L)%==a%){
nr=m;
}
else{
nr=max(nr,m-);
}
}
if(L>=a) nl=min(nl,L-a);
if(m-R>=a) nr=max(nr,R+a);
L=nl;
R=nr;
}
int ans=;
for(int i=L;i<=R;i+=){
ans+=C[i];
ans%=mod;
}
printf("%d\n",ans);
}
return ;
}
#include<cstdio>
#include<algorithm>
using namespace std;
typedef __int64 LL;
const int M=;
const int mod=;
LL C[M];
LL INV[M];
void inv_init(){//初始化%mod的乘法逆元
INV[]=;
for(int i=;i<M;i++){
INV[i]=INV[mod%i]*(mod-mod/i)%mod;
}
}
int main() {
inv_init();
int n,m,a;
while(~scanf("%d%d",&n,&m)) {
C[]=;
for(int i=;i<=m;i++){
C[i]=C[i-]*(m-i+)%mod*INV[i]%mod;
}
int L=,R=,nl,nr,tmp;
for(int i=;i<n;i++){
scanf("%d",&a);
tmp=min(m-L,a);
nr=L+tmp-(a-tmp);
tmp=min(R,a);
nl=R-tmp+(a-tmp);
if(nl>nr) swap(nl,nr);
if(L<=a&&a<=R){
if(L%==a%){
nl=;
}
else{
nl=min(nl,);
}
}
if((m-R)<=a&&a<=(m-L)){
if((m-L)%==a%){
nr=m;
}
else{
nr=max(nr,m-);
}
}
if(L>=a) nl=min(nl,L-a);
if(m-R>=a) nr=max(nr,R+a);
L=nl;
R=nr;
}
int ans=;
for(int i=L;i<=R;i+=){
ans+=C[i];
ans%=mod;
}
printf("%d\n",ans);
}
return ;
}
end
gcd,lcm,ext_gcd,inv的更多相关文章
- HDU4497 GCD and LCM(数论,质因子分解)
HDU4497 GCD and LCM 如果 \(G \% L != 0\) ,那么输出 \(0\) . 否则我们有 \(L/G=(p_1^{r_1})\cdot(p_2^{r_2})\cdot(p_ ...
- HDU 4497 GCD and LCM (数学,质数分解)
题意:给定G,L,分别是三个数最大公因数和最小公倍数,问你能找出多少对. 析:数学题,当时就想错了,就没找出规律,思路是这样的. 首先G和L有公因数,就是G,所以就可以用L除以G,然后只要找从1-(n ...
- 【基础数学】质数,约数,分解质因数,GCD,LCM
1.质数: 质数(prime number)又称素数,有无限个.一个大于1的自然数,除了1和它本身外,不能整除以其他自然数(质数),换句话说就是该数除了1和它本身以外不再有其他的因数. 2.约数: 如 ...
- SPOJ LGLOVE 7488 LCM GCD Love (区间更新,预处理出LCM(1,2,...,n))
题目连接:http://www.spoj.com/problems/LGLOVE/ 题意:给出n个初始序列a[1],a[2],...,a[n],b[i]表示LCM(1,2,3,...,a[i]),即1 ...
- ACM数论之旅3---最大公约数gcd和最小公倍数lcm(苦海无边,回头是岸( ̄∀ ̄))
gcd(a, b),就是求a和b的最大公约数 lcm(a, b),就是求a和b的最小公倍数 然后有个公式 a*b = gcd * lcm ( gcd就是gcd(a, b), ( •̀∀•́ ) ...
- 数学--数论--HDU 5382 GCD?LCM?(详细推导,不懂打我)
Describtion First we define: (1) lcm(a,b), the least common multiple of two integers a and b, is the ...
- LightOj 1236 - Pairs Forming LCM (分解素因子,LCM )
题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意:给你一个数n,求有多少对(i, j)满足 LCM(i, j) = n, ...
- GCD SUM 强大的数论,容斥定理
GCD SUM Time Limit: 8000/4000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) SubmitStatu ...
- OC 线程操作 - GCD使用 -同步函数,异步函数,串行队列,并发队列
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{ // GCD 开几条线程并不是我们 ...
- 【UOJ#33】【UR #2】树上GCD(长链剖分,分块)
[UOJ#33][UR #2]树上GCD(长链剖分,分块) 题面 UOJ 题解 首先不求恰好,改为求\(i\)的倍数的个数,最后容斥一下就可以解决了. 那么我们考虑枚举一个\(LCA\)位置,在其两棵 ...
随机推荐
- Java实现邮箱找回密码 --转载
通过邮件找回密码功能的实现 1.最近开发一个系统,有个需求就是,忘记密码后通过邮箱找回.现在的系统在注册的时候都会强制输入邮箱,其一目的就是 通过邮件绑定找回,可以进行密码找回.通过java发送邮件的 ...
- iOS - OC & Xcode
一.入门 1.1 iOS模版介绍 1.2 简单工程项目 1.3 设置App启动的设备方向 1.4 Xcode界面介绍 1.5 快速查找文件 1.6 快速更改同名变量 1.7 将代码提取为方法 1.8 ...
- Swift - Property ''not initialized at super.init call
Property ''not initialized at super.init call 这个错误应该挺常见的的,为什么在百度上没有找到呢,stack over flow找到了,也不能说是什么解决办 ...
- iOS数据持久化(二)SQLite
一.什么是SQLite SQLite是一款轻型的嵌入式数据库,它占用资源非常的低,处理速度快,非常适合用于移动端开发. 二.使用 创建DataBaseHandle.h & DataB ...
- SQL Server数据库学习总结
经过一段时间的学习,也对数据库有了一些认识,数据库基本是由表,关系,操作组成:对于初学者首先要学的 一图胜“十”言:SQL Server 数据库总结 一个大概的总结 经过一段时间的学习,也对数 ...
- 《锋利的jQuery》心得笔记--One Sections
第一章 1. $是jQuery的一个简写形式 2. 在jQuery中无法使用DOM对象的任何方法:比如:$ (“#id”).innerHTML.$ (“#id”).checked, 可以使 ...
- HDU 5024 Wang Xifeng's Little Plot(枚举)
题意:求一个图中只有一个90°拐点的路的最大长度. 分析:枚举每一个为'.'的点,求出以该点为拐点的八种路中的最大长度,再比较所有点,得出最大长度即可. 如上样例,这样是个90°的角... 注意:最多 ...
- c++对象内存布局
这篇文章我要简单地讲解下c++对象的内存布局,虽然已经有很多很好的文章,不过通过实现发现有些地方不同的编译器还是会有差别的,希望和大家交流. 在没有用到虚函数的时候,C++的对象内存布局和c语言的st ...
- Base64加密
实际开发中可能需要使用到可解密的加密方式,例如客户端记住用户的密码,客户端不能记住明文密码,那就需要对明文密码进行加密,然后在表单提交之后先对密码进行解密,在进行MD5加密和数据库中的密码进行比较实现 ...
- php Smarty date_format [格式化时间日期]
Example 5-8. date_format[日期格式] index.php: 复制代码代码如下: $smarty = new Smarty; $smarty->assign('yester ...