分析
首先对于ax+by=c我们可以用扩欧来做
如果要求最小解,y=y/(lcm(a,b)/b)
然后取模则为y=(c-a*x)/b
然后一波快速乘优化就行
#include <iostream> #include <cstdio> using namespace std; typedef long long ll; ll Exgcd(ll a,ll b,ll &x,ll &y) { if (!b) { x=1;y=0; return a; } ll gcd=Exgcd(b,a%b,y,x); y-=a/b*x; return gcd; } ll Solve(ll a,ll b,ll c) { ll x,y,gcd=Exgcd(a,b,x,y); if (c%gcd) return 0; ll q=b/gcd; x=(c/gcd%q*x%q+q)%q;y=(c-a*x)/b; return y<0?0:y/(a/gcd)+1; } int main() { freopen("cake.in","r",stdin); freopen("cake.out","w",stdout); int t; scanf("%d",&t); while (t--) { ll a,b,c; scanf("%lld%lld%lld",&a,&b,&c); printf("%lld ",Solve(a,b,c)); } fclose(stdin);fclose(stdout); }