标签:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 94176 | Accepted: 17413 |
Description
Input
Output
Sample Input
1 2 3 4 5
Sample Output
4
思路:直接列方程:(a+mx) mod L= (b+nx) mod L 变形得 (m-n)x mod L= (b-a) mod L 即 (m-n)=(b-a)(mod L) ,注意同余方程ax=b(mod n)中a必须为正数
水题。1A!
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; const int maxn=1000100; const int INF=(1<<28); typedef long long ll; ll a,b,m,n,L; ll x,y; ll exgcd(ll a,ll b,ll &x,ll &y) { if(b==0){ x=1;y=0; return a; } ll r=exgcd(b,a%b,x,y); ll t=y; y=x-(a/b)*y; x=t; return r; } bool mod_linear_equation(ll a,ll b,ll n) { ll d=exgcd(a,n,x,y); if(b%d) return false; x=x*(b/d)%n; x=(x+n/d)%(n/d); return true; } int main() { while(cin>>a>>b>>m>>n>>L){ if(m-n<0){ swap(m,n); swap(a,b); } if(mod_linear_equation(m-n,b-a,L)) cout<<x<<endl; else cout<<"Impossible"<<endl; } return 0; }
标签:
原文地址:http://www.cnblogs.com/--560/p/4373104.html