Codeforces 710D Two Arithmetic Progressions(同余方程组)
来源:互联网 发布:视频监控网络拓扑图 编辑:程序博客网 时间:2024/05/19 18:13
代码
#include <map>#include <set>#include <ctime>#include <stack>#include <queue>#include <cmath>#include <bitset>#include <string>#include <vector>#include <cstdio>#include <cctype>#include <cstring>#include <sstream>#include <cstdlib>#include <iostream>#include <algorithm>#pragma comment(linker,"/STACK:102400000,102400000")using namespace std;#define MAX 10000005#define MAXN 1000005#define maxnode 205#define sigma_size 26#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define lrt rt<<1#define rrt rt<<1|1#define middle int m=(r+l)>>1#define LL long long#define ull unsigned long long#define mem(x,v) memset(x,v,sizeof(x))#define lowbit(x) (x&-x)#define pii pair<int,int>#define bits(a) __builtin_popcount(a)#define mk make_pair#define limit 10000//const int prime = 999983;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f;const double pi = acos(-1.0);const double inf = 1e18;const double eps = 1e-4;const LL mod = 1e9+7;const ull mx = 133333331;/*****************************************************/inline void RI(int &x) { char c; while((c=getchar())<'0' || c>'9'); x=c-'0'; while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0'; }/*****************************************************/ vector<int> a,b;LL mul(LL a,LL b,LL mod){ LL n = 0; while(b){ if(b&1) n = (n+a)%mod; a = (a*2)%mod; b /= 2; } return n;}void exgcd(LL a,LL b,LL &d,LL &x,LL &y){ if(!b){x=1;y=0;d=a;return;} else {exgcd(b,a%b,d,y,x);y-=a/b*x;}}LL labs(LL a){ if(a<0) return -a; return a;}LL solve(){ //x=b[i](mod a[i]) LL ta=a[0],tb=b[0]; int flag=1; for(int i=1;i<a.size();i++){ LL xa=ta,xb=a[i],c=b[i]-tb,d,x,y; exgcd(xa,xb,d,x,y); if(c%d){ flag=0; break; } LL tmp=xb/d; LL k=1; if(x<0) k*=-1; if(c/d<0) k*=-1; x=(mul(labs(x),labs(c/d),tmp)*k+tmp)%tmp;//加入了二分快速乘,需要把负数先变为正数 tb=ta*x+tb; ta=ta/d*a[i]; } if(!flag) return -INF; return tb;}LL gcd(LL x,LL y){ if(!y) return x; return gcd(y,x%y);}LL lcm(LL x,LL y){ return x*y/gcd(x,y);}int main(){ LL a1,b1,a2,b2,L,R; while(cin>>a1>>b1>>a2>>b2>>L>>R){ a.clear();b.clear(); a.push_back(a1); a.push_back(a2); b.push_back(b1); b.push_back(b2); LL cnt=solve(); L=max(max(b1,b2),L); //cout<<cnt<<endl; if(L>R){ cout<<0<<endl; continue; } if(cnt==-INF){ cout<<0<<endl; continue; } LL tmp=lcm(a1,a2); LL ans=0; if(cnt<=R) ans+=(R-cnt)/tmp+1; if(cnt<L) ans-=(L-1-cnt)/tmp+1; cout<<ans<<endl; } return 0;}
代码2:
#include <map>#include <set>#include <ctime>#include <stack>#include <queue>#include <cmath>#include <bitset>#include <string>#include <vector>#include <cstdio>#include <cctype>#include <cstring>#include <sstream>#include <cstdlib>#include <iostream>#include <algorithm>#pragma comment(linker,"/STACK:102400000,102400000")using namespace std;#define MAX 100005#define MAXN 1000005#define maxnode 205#define sigma_size 26#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define lrt rt<<1#define rrt rt<<1|1#define middle int m=(r+l)>>1#define LL long long#define ull unsigned long long#define mem(x,v) memset(x,v,sizeof(x))#define lowbit(x) (x&-x)#define pii pair<int,int>#define bits(a) __builtin_popcount(a)#define mk make_pair#define limit 10000//const int prime = 999983;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f;const double pi = acos(-1.0);const double inf = 1e18;const double eps = 1e-4;const LL mod = 1e9+7;const ull mx = 133333331;/*****************************************************/inline void RI(int &x) { char c; while((c=getchar())<'0' || c>'9'); x=c-'0'; while((c=getchar())>='0' && c<='9') x=(x<<3)+(x<<1)+c-'0'; }/*****************************************************/ void exgcd(LL a,LL b,LL &d,LL &x,LL &y){ if(!b){x=1;y=0;d=a;return;} else {exgcd(b,a%b,d,y,x);y-=a/b*x;} } LL labs(LL a){ if(a<0) return -a; return a;}int main(){ LL a1,b1,a2,b2,L,R; while(cin>>a1>>b1>>a2>>b2>>L>>R){ LL x,y,d; exgcd(a1,a2,d,x,y); if((b2-b1)%d!=0){ cout<<0<<endl; continue; } x*=(b2-b1)/d; x=(x%labs(a2/d)+labs(a2/d))%labs(a2/d); LL cnt=x*a1+b1; LL tmp=labs(a1*a2/d); //cout<<cnt<<" "<<tmp<<endl; LL ans=0; L=max(L,max(b1,b2)); if(L>R){ cout<<0<<endl; continue; } if(cnt<=R) ans+=(R-cnt)/tmp+1; if(cnt<L) ans-=(L-1-cnt)/tmp+1; cout<<ans<<endl; } return 0;}
0 0
- Codeforces 710D Two Arithmetic Progressions(同余方程组)
- codeforces 710D Two Arithmetic Progressions [同余方程]【数论】
- codeforces 710D Two Arithmetic Progressions
- CodeForces 710D Two Arithmetic Progressions
- CodeForces 710D Two Arithmetic Progressions(数学题)
- 【数论】【扩展欧几里得】Codeforces 710D Two Arithmetic Progressions
- Codeforces Problem 710D Two Arithmetic Progressions(中国剩余定理)
- CodeForces 710D Two Arithmetic Progressions(模线性方程组)
- D. Two Arithmetic Progressions
- codeforces 710D Two Arithmetic Progressions (扩展中国剩余定理)
- cf/Codeforces ECR16-D - Two Arithmetic Progressions-中国剩余定理
- 扩展欧几里得算法,中国剩余定理(Two Arithmetic Progressions,cf 710D)
- codeforces Educational Codeforces Round 16 D. Two Arithmetic Progressions 扩展欧几里得
- [Educational Codeforces Round 16]D. Two Arithmetic Progressions ★ ★ ★
- 同余问题(3)一元线性同余方程组
- Biorhythms(一元线性同余方程组)
- poj_2065 SETI(高斯消元解同余方程组)
- 同余方程组问题
- 使用软件 RAID 宗卷在 Mac mini 上安装 OS X Server
- Java分页代码的实现
- 内核低分辨率定时器实现
- ORM一键还原系统官方版
- [code generation] 模版引擎_groovy
- Codeforces 710D Two Arithmetic Progressions(同余方程组)
- redis如何分配哈希槽
- <a>点击链接&<img>插入图片
- 排序算法———折半插入排序
- 笔记--URL伪静态
- 开源中国客户端Activity管理类(可直接用到项目中)
- MySQL RR隔离 读一致性
- java基础学习之网络编程全双工
- 错误的算法