您的位置:首页 > 其它

51Nod 1352

2017-06-01 11:00 134 查看
题意:给出一个a,b,n,求出满足a*x + b * y = n + 1,的 x >= 1 && y >= 1的个数;

思路:因为a > 0 && b > 0 ,那么这个方程在二维坐标上表示的是在第一象限的经过两个正坐标的直线,所以只需求出最左边的点和最右边的点,然后把中间的点求出来,中间的点可以利用两个点gcd(横坐标的相差 ,纵坐标相差) - 1求出,然而两个点可以利用ex_gcd求出。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
const int maxn = 40000 + 10;
#define INF 0x3f3f3f3f
#define clr(x,y) memset(x,y,sizeof x )
typedef long long ll;
#define eps 10e-10
const ll Mod = 1000000007;
typedef pair<ll, ll> P;
ll gcd(ll x,ll y)
{
return y ? gcd(y,x % y) : x;
}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y)
{
if(b == 0)
{
d = a;x = 1;y = 0;
return ;
}
ex_gcd(b,a % b,d,y,x);
y -= a / b * x;
}
int main()
{
int Tcase;
scanf("%d",&Tcase);
while(Tcase --)
{
ll a,b,n;
scanf("%I64d%I64d%I64d",&n,&a,&b);
n ++;
ll x,y;
ll d;
ex_gcd(a,b,d,x,y);
// cout << "Pass_by" << endl;
if(n % d)
{
puts("0");
continue;
}
ll x1 = (x * (n/d) % (b/d) + (b/d)) % (b/d);
if(!x1)
x1 += (b/d);
ll y1 = (n - x1 * a) / b;
if(y1 <= 0)
{
puts("0");
continue;
}
ll y2 = (y * (n/d) % (a/d) + a/ d) % (a/d);
if(!y2)
y2 += a/d;
ll x2 = (n - y2 * b) / a;
if(x2 <= 0)
{
puts("0");
continue;
}
// cout << x1 << " " << y1 << " " << x2 << " " << y2 << " " << x2 * a + b * y2 << endl;
ll d1 = abs(x1 - x2),d2 = abs(y1 - y2);
ll ans = gcd(d1,d2);
if(ans)
ans --;
// cout << d1 << " " << d2 << " " << ans << endl;
if(x1 == x2 && y1 == y2)
{
ans ++;
}
else ans += 2;
printf("%I64d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: