51nod 1352 集合计数

#include 
using namespace std;

long long exgcd(long long a, long long b, long long &x, long long &y)  
{  
    if (b == 0)
    {  
        x = 1;  
        y = 0;  
        return a;  
    }  
    long long gcd = exgcd(b, a % b, x, y);  
    long long tmp = x;  
    x = y;  
    y = tmp - (a/b) * y;  
    return gcd;  
}  

int main()
{
	long long T,n,a,b,g,x,y,tmp,rem;
	cin>>T;
	while(T--)
	{
		scanf("%lld%lld%lld",&n,&a,&b);
		g=exgcd(a,b,x,y);//a*x+b*y=g 
		if((n+1)%g)
		{
			printf("0\n");
			continue;
		}
		x*=(n+1)/g;//a*x+b*y=n+1 
		x=(x%(b/g)+b/g)%(b/g);
		if(x==0)
			x+=b/g;
		rem=n-a*x;//rem=b*y-1 
		if(rem<0)//if b<=0
		{
			printf("0\n");
			continue;
		}
		printf("%lld\n",rem/(a*b/g)+1);
	}
}

你可能感兴趣的:(----51nod)