标签:
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 103473 | Accepted: 20116 |
Description
Input
Output
Sample Input
1 2 3 4 5
Sample Output
4
好久没刷数论了(或者说从来没真正开始刷过?=。=
是该啃啃了,最后一段时间,努力试试在这方面找点突破口
扩展欧几里得板子题,注意把青蛙AB的前后顺序弄对就好。
首先让A始终是靠左(纬度小)的那只,如果输入顺序不同,强行swap就好
其次,考虑两青蛙每秒蹦的距离,如果A > B 公式即为(Sa-Sb)*x ≡ Xb-Xa(mod L)
S表示速度 X表示纬度
如果A < B (Sb-Sa)*x ≡ Xa-Xb(mod L)
然后上板子。。
至于扩偶怎么搞,原理是什么,左拐ACdream或各大神犇blog
代码如下:
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout)
using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8;
LL extend_gcd(LL a,LL b,LL &x,LL &y)
{
if(a == 0 && b == 0) return -1;
if(b == 0)
{
x = 1;
y = 0;
return a;
}
LL d = extend_gcd(b,a%b,y,x);
y -= a/b*x;
return d;
}
LL mod_reverse(LL a,LL m,LL c)
{
LL x,y,d;
d = extend_gcd(a,m,x,y);
if(c%d) return -1;
x = (x*(c/d))%m;
x = (x%(m/d)+m/d)%(m/d);
return x;
}
int main()
{
//fread();
//fwrite();
LL x,y,m,n,l;
while(~scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&l))
{
if(x > y)
{
swap(x,y);
swap(m,n);
}
LL ans;
if(m < n) ans = mod_reverse(n-m,l,l-y+x);
else ans = mod_reverse(m-n,l,y-x);
if(ans == -1) puts("Impossible");
else printf("%lld\n",ans);
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/challengerrumble/article/details/51252380