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\)位置,在其两棵 ...
随机推荐
- UILabel 整理
UILabel 多行文字自动换行 (自动折行) 1.UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(10, 100, 3 ...
- JS学习笔记 -- 定时器,提示框的应用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 《JavaScript高级程序设计》心得笔记-----第一篇章
第一章 JavaScript由ECMAScript.DOM.BOM组成.其中BOM功能在HTML5中有了正式的规范,使BOM的兼容性越来越高. 第二章 1.<script>属性中的asyn ...
- Essential C++ 学习笔记02--Array/Vector 与指针
Essential C++ 1.5-1.6节,3.1节笔记 Array/Vector/指针,难度偏大, 但若学习初期不熟悉基本用法,则难以写出有效代码. 1. 基本概念 Array 是一段连续内存,数 ...
- UIKit,Core Data , Core Graphics, Core Animation,和OpenGLES框架
iOS的主要框架介绍 框架是一个目录,这个目录包含了共享库,访问共享库里代码的头文件,和其它的图片和声音的资源文件.一个共享库定义的方法或函数可以被应用程序调用. IOS提供了很多你可以在应用程序 ...
- 使用JDBC从数据库中查询数据
* ResultSet 结果集:封装了使用JDBC 进行查询的结果 * 1. 调用Statement 对象的 executeQuery(sql) 方法可以得到结果集 * 2. ResultSet 返回 ...
- ARM公布“物联网”嵌入式mbed OS系统软件平台
继ARM公司发布了为嵌入式微控制器设计的Cortex-M7架构处理器,ARM又公布了专为廉价低功耗“物联网”设计的新版软件及系统平台,以加速物联网设备的发展及部署.该软件为基于ARM现有Cortex- ...
- Vivado HLS与System Generator:联系与区别
在很多年以前的ISE套件里面,有个功能强大的AccelDSP,它可以可自动地进行浮点到定点转换,并把算法生成可综合的HDL,还可以创建用于验证的测试平台,但是在4年前左右的时候销声匿迹了,当时的说法是 ...
- SQL server数据库内置账户SA登录设置
SQL server数据库内置账户SA登录不了 设置SQL Server数据库给sa设置密码的时候 提示18456 解决步骤: 第二步:右击sa,选择属性: 第三步:点击状态选项卡:勾选授予 ...
- 一个PHP加密脚本,达到一定免杀效果
<?php /**************************************** *author: L.N. *blog : [url]http://lanu.sinaapp.co ...