Gym100920J

求Ax+By<=C,非負整數對(x,y)的個數ios

首先令y=0;則x<=(C/A);ans=(C/A)+1;c++

將Ax+By=C反轉以後利用類歐幾里得算法:f(a,b,c,n)=∑((a*i+b)/c) (0<=i<=n);求解算法

反轉以後,a=A,b=C%A,c=b,n=C/A;spa

#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define bug printf("bug\n");
#define N 1000005
#define pb emplace_back
#define fi first
#define se second
#define sc scanf
#define pf printf
#define Endl '\n'
using namespace std;
const ll inf=1e18;
const ll mod=1000000007;
ll qm(ll a,ll b){
    ll ans=1;
    while(b){
        if(b&1)ans=ans*a%mod;
        a=a*a%mod;b>>=1;
    }
    return (ans%mod+mod)%mod;
}
ll cal(ll a,ll b,ll c,ll n){
    if(a==0)return (b/c)*(n+1);
    if(a>=c||b>=c)return cal(a%c,b%c,c,n)+n*(n+1)*(a/c)/2+(b/c)*(n+1);
    ll m=(a*n+b)/c;
    return n*m-cal(c,c-b-1,a,m-1);
}
int main()
{

    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    ll a,b,c;
    cin>>a>>b>>c;
    cout<<cal(a,c%a,b,c/a)+c/a+1<<endl;

    return 0;
}
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息