您的位置:首页 > 其它

HNU2[J题]Modified LCS 扩展GCD

2014-07-22 08:41 295 查看
题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12831&courseid=268

解题思路:求出最小的两个为正且相等的下标(利用扩展GCD),然后就可以直接求了。

解题代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <vector>
using namespace std;
#define LL __int64
LL x,y,gcd;
void exgcd(LL a ,LL b)
{
if (!b)
{
x=1;
y=1;
gcd=a;
return;
}
exgcd(b,a%b);
LL t=x;
x=y;
y=t-a/b*y;
return ;
}
int main()
{
int T;
LL n1,n2,f1,f2,d1,d2;
//    freopen("in.txt", "r", stdin);
//    freopen("outt.txt","w",stdout);
scanf("%d",&T);
while (T--)
{
cin>>n1>>f1>>d1>>n2>>f2>>d2;
LL f=f2-f1;
exgcd(d1,-d2);
if (f % gcd)
{
puts("0");
continue;
}
else
{
LL r=abs((-d2)/gcd);
x=(x*f/gcd % r+r)%r;
y=(f-x*d1)/(-d2);
LL dx=abs(d1/gcd);
LL dy=abs(d2/gcd);
LL ans=min((n1-x-1)/dy,(n2-y-1)/dx)+1;
//if (ans<0) ans=0;
cout<<ans<<endl;
}
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: