[BZOJ2987][类欧几里得算法]Earthquake

题意


求Ax+By=C的非负整数解的个数


类欧模板题

转化成 ax+bc 的形式,然后可以看Manchery大神的手推http://blog.csdn.net/u014609452/article/details/52343530

#include 
#include 
#include 
#include 

using namespace std;

typedef long long ll;

ll a,b,c;

ll gcd(ll a,ll b){
    return b?gcd(b,a%b):a;
}

#define T ((-b+c-1)/c)
#define N ((a*n+b)/c)

ll solve(ll n,ll a,ll b,ll c){
    if(!a) return (b/c)*n;
    if(b<0) return solve(n,a,b+T*c,c)-T*n;
    if(b>=c) return solve(n,a,b%c,c)+(b/c)*n;
    if(a>=c) return solve(n,a%c,b,c)+((a/c)*n*(n+1)>>1);
    return (n+1)*N-solve(N,c,-b+a-1,a);
}

int main(){
    cin>>a>>b>>c;
    printf("%lld\n",(c/a)+(c/b)+solve(c/a,a,c%a-a,b)+1);
}

你可能感兴趣的:(类欧几里得算法)