https://www.luogu.org/blog/shiqingyu/solution-p5170  公式要记

(ai+b)/c;

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int mod=998244353;
const int inv2=(mod+1)/2;
ll FF(ll a,ll b,ll c,ll n){  // 0-n  (ai+b)/c;
if(a==0) return (n+1)%mod*(b/c)%mod;
if(n==0) return (b/c)%mod;
if(a>=c || b>=c)
return  ( 1ll*n%mod*(n+1)%mod*inv2%mod*(a/c)%mod+1ll*(n+1)%mod*(b/c)%mod+FF(a%c,b%c,c,n) )%mod;
ll m=(1ll*a*n+b)/c;
return  ( 1ll*(n%mod)*(m%mod)%mod-FF(c,c-b-1,a,m-1))%mod;
}
ll GG(ll a,ll b,ll c,ll n){  // 0-n  ( (ai+b)/c  )*( (ai+b)/c  )
    
}
ll HH(ll a,ll b,ll c,ll n){  // 0-n ( (ai+b)/c )*i

}
int main(){
    int T; scanf("%d",&T);
    while(T--){
        int a,b,c,n; scanf("%d %d %d %d",&n,&a,&b,&c);
        printf("%lld 0 0\n",(FF(a,b,c,n)%mod+mod)%mod);
    }

}
View Code

 

#include<bits/stdc++.h>
#define ll long long
const int mod=1e9+7;
const int inv2=(1+mod)/2;
const int inv6=1; // 这个错得
using namespace std;
struct ou{
    int a,b,c,n;
    ll f(ll a, ll b, ll c, ll n) {  // 0-n  (ai+b)/c;
        if (a == 0)
            return (n + 1) * (b / c) % mod;
        if (n == 0) return (b / c);
        if (a >= c || b >= c)
            return (f(a % c, b % c, c, n) + (a / c) * n % mod * (n + 1) % mod * inv2 % mod + (b / c) * (n + 1) % mod) % mod;
        ll m = (a * n + b) / c;
        return (n * m % mod - f(c, c - b - 1, a, m - 1)) % mod;
    }
    ll g(ll a, ll b, ll c, ll n) {  // 0-n  ( (ai+b)/c  )*( (ai+b)/c  )
        if (a == 0) return (b / c) * n % mod * (n + 1) % mod * inv2 % mod;
        if (n == 0) return 0;
        if (a >= c || b >= c) return (g(a % c, b % c, c, n) + (a / c) * n % mod * (n + 1) % mod * (2 * n + 1) % mod * inv6 % mod + (b / c) * n % mod * (n + 1) % mod * inv2 % mod) % mod;
        ll m = (a * n + b) / c;
        return (n * (n + 1) % mod * m % mod - f(c, c - b - 1, a, m - 1) - h(c, c - b - 1, a, m - 1)) % mod * inv2 % mod;
    }
    ll h(ll a, ll b, ll c, ll n) {   //  0-n ( (ai+b)/c )*i
        if (a == 0) return (n + 1) * (b / c) % mod * (b / c) % mod;
        if (n == 0) return (b / c) * (b / c) % mod;
        if (a >= c || b >= c)
            return ((a / c) * (a / c) % mod * n % mod * (n + 1) % mod * (2 * n + 1) % mod * inv6 % mod +
                    (b / c) * (b / c) % mod * (n + 1) % mod + (a / c) * (b / c) % mod * n % mod * (n + 1) % mod +
                    h(a % c, b % c, c, n) + 2 * (a / c) % mod * g(a % c, b % c, c, n) % mod +
                    2 * (b / c) % mod * f(a % c, b % c, c, n) % mod) % mod;
        ll m = (a * n + b) / c;
        return (n * m % mod * (m + 1) % mod - 2 * g(c, c - b - 1, a, m - 1) - 2 * f(c, c - b - 1, a, m - 1) - f(a, b, c, n)) % mod;
    }
};
int main(){

}
View Code

 ax+by<=c  非负整数解

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll FF(ll a,ll b,ll c,ll n){  // 0-n  (ai+b)/c;

    if(a==0) return (n+1)*(b/c);
    if(n==0) return (b/c);
    if(a>=c || b>=c)
    return   1ll*n*(n+1)/2*(a/c)+1ll*(n+1)*(b/c)+FF(a%c,b%c,c,n);
    ll m=(1ll*a*n+b)/c;
    return   1ll*n*m-FF(c,c-b-1,a,m-1);
}
int main(){

        ll a,b,c; scanf("%lld %lld %lld",&a,&b,&c);
        printf("%lld\n",FF(a,c%a,b,c/a)+c/a+1);


}
View Code

 a/b<x/y<c/d  求最小得x ,y  

#include<bits/stdc++.h>
#define ll long long
using namespace std;
void make(ll lb,ll la,ll rb,ll ra,ll &x,ll &y){
    ll m=lb/la+1;
    if(m<rb/ra+(rb%ra>0)){
        x=m;
        y=1;
         return ;
    }
    m--;
    lb-=m*la;
    rb-=m*ra;
    make(ra,rb,la,lb,y,x);  // -m
    x+=y*m;
    return  ;
}
int main(){
    int T; scanf("%d",&T);
    while(T--){
        ll x,p;  scanf("%lld %lld",&p,&x);
        ll b=0,y=0;
        make(p,x,p,x-1,b,y);
        ll a=x*b-p*y;
        //cout<<endl;
        printf("%lld/%lld\n",a,b);
    }
}
View Code
{1,0},
{2,0},
{3,0},
{4,0},
{5,0},
{6,0},
{7,0},
{8,0},
{9,0},
{1,1},
{1,199981},
{1,199982},
{1,199983},
{1,199984},
{1,199985},
{1,199986},
{1,199987},
{1,199988},
{1,199989},
{1,199990},
{1,200000},
{1,200001},
{1,1599981},
{1,1599982},
{1,1599983},
{1,1599984},
{1,1599985},
{1,1599986},
{1,1599987},
{1,1599988},
{1,1599989},
{1,1599990},
{1,2600000},
{1,2600001},
{1,13199998},
{2,28263827},
{1,35000000},
{2,35000000},
{1,35000001},
{1,35199981},
{1,35199982},
{1,35199983},
{1,35199984},
{1,35199985},
{1,35199986},
{1,35199987},
{1,35199988},
{1,35199989},
{1,35199990},
{1,35200000},
{1,35200001},
{1,117463825},
{2,242463827},
{3,371599983},
{3,371599984},
{3,371599985},
{3,371599986},
{3,371599987},
{3,371599988},
{3,371599989},
{3,371599990},
{3,371599991},
{3,371599992},
{4,499999984},
{4,499999985},
{4,499999986},
{4,499999987},
{4,499999988},
{4,499999989},
{4,499999990},
{4,499999991},
{4,499999992},
{4,499999993},
{1,500000000},
{2,500000000},
{3,500000000},
{4,500000000},
{1,500000001},
{1,500199981},
{1,500199982},
{1,500199983},
{1,500199984},
{1,500199985},
{1,500199986},
{1,500199987},
{1,500199988},
{1,500199989},
{1,500199990},
{1,500200000},
{1,500200001},
{1,501599981},
{1,501599982},
{1,501599983},
{1,501599984},
{1,501599985},
{1,501599986},
{1,501599987},
{1,501599988},
{1,501599989},
{1,501599990},
{1,502600000},
{1,502600001},
{1,513199998},
{2,528263827},
{1,535000000},
{2,535000000},
{1,535000001},
{1,535199981},
{1,535199982},
{1,535199983},
{1,535199984},
{1,535199985},
{1,535199986},
{1,535199987},
{1,535199988},
{1,535199989},
{1,535199990},
{1,535200000},
{1,535200001},
{1,1111111110},
{7,9465000000},
{8,9465000000},
{7,9471736170},
{8,9486799989},
{8,9486799990},
{8,9486799991},
{8,9486799992},
{8,9486799993},
{8,9486799994},
{8,9486799995},
{8,9486799996},
{8,9486799997},
{8,9497400000},
{8,9498399989},
{8,9498399990},
{8,9498399991},
{8,9498399992},
{8,9498399993},
{8,9498399994},
{8,9498399995},
{8,9498399996},
{8,9498399997},
{6,9500000000},
{7,9500000000},
{8,9500000000},
{6,9628399986},
{6,9628399987},
{6,9628399988},
{6,9628399989},
{6,9628399990},
{6,9628399991},
{6,9628399992},
{6,9628399993},
{6,9628399994},
{6,9628399995},
{7,9757536170},
{8,9882536171},
{7,9965000000},
{8,9965000000},
{7,9971736170},
{8,9986799989},
{8,9986799990},
{8,9986799991},
{8,9986799992},
{8,9986799993},
{8,9986799994},
{8,9986799995},
{8,9986799996},
{8,9986799997},
{8,9997400000},
{8,9998399989},
{8,9998399990},
{8,9998399991},
{8,9998399992},
{8,9998399993},
{8,9998399994},
{8,9998399995},
{8,9998399996},
{8,9998399997},
{2,10000000000},
{3,10000000000},
{4,10000000000},
{5,10000000000},
{6,10000000000},
{7,10000000000},
{8,10000000000},
{9,10000000000},
{2,10028263827},
{2,10035000000},
{2,10242463827},
{3,10371599983},
{3,10371599984},
{3,10371599985},
{3,10371599986},
{3,10371599987},
{3,10371599988},
{3,10371599989},
{3,10371599990},
{3,10371599991},
{3,10371599992},
{4,10499999984},
{4,10499999985},
{4,10499999986},
{4,10499999987},
{4,10499999988},
{4,10499999989},
{4,10499999990},
{4,10499999991},
{4,10499999992},
{4,10499999993},
{2,10500000000},
{3,10500000000},
{4,10500000000},
{2,10528263827},
{2,10535000000},
{7,19465000000},
{8,19465000000},
{7,19471736170},
{8,19486799989},
{8,19486799990},
{8,19486799991},
{8,19486799992},
{8,19486799993},
{8,19486799994},
{8,19486799995},
{8,19486799996},
{8,19486799997},
{8,19497400000},
{8,19498399989},
{8,19498399990},
{8,19498399991},
{8,19498399992},
{8,19498399993},
{8,19498399994},
{8,19498399995},
{8,19498399996},
{8,19498399997},
{6,19500000000},
{7,19500000000},
{8,19500000000},
{6,19628399986},
{6,19628399987},
{6,19628399988},
{6,19628399989},
{6,19628399990},
{6,19628399991},
{6,19628399992},
{6,19628399993},
{6,19628399994},
{6,19628399995},
{7,19757536170},
{8,19882536171},
{7,19965000000},
{8,19965000000},
{7,19971736170},
{8,19986799989},
{8,19986799990},
{8,19986799991},
{8,19986799992},
{8,19986799993},
{8,19986799994},
{8,19986799995},
{8,19986799996},
{8,19986799997},
{8,19997400000},
{8,19998399989},
{8,19998399990},
{8,19998399991},
{8,19998399992},
{8,19998399993},
{8,19998399994},
{8,19998399995},
{8,19998399996},
{8,19998399997},
{3,20000000000},
{4,20000000000},
{5,20000000000},
{6,20000000000},
{7,20000000000},
{8,20000000000},
{9,20000000000},
{3,20371599983},
{3,20371599984},
{3,20371599985},
{3,20371599986},
{3,20371599987},
{3,20371599988},
{3,20371599989},
{3,20371599990},
{3,20371599991},
{3,20371599992},
{4,20499999984},
{4,20499999985},
{4,20499999986},
{4,20499999987},
{4,20499999988},
{4,20499999989},
{4,20499999990},
{4,20499999991},
{4,20499999992},
{4,20499999993},
{3,20500000000},
{4,20500000000},
{7,29465000000},
{8,29465000000},
{7,29471736170},
{8,29486799989},
{8,29486799990},
{8,29486799991},
{8,29486799992},
{8,29486799993},
{8,29486799994},
{8,29486799995},
{8,29486799996},
{8,29486799997},
{8,29497400000},
{8,29498399989},
{8,29498399990},
{8,29498399991},
{8,29498399992},
{8,29498399993},
{8,29498399994},
{8,29498399995},
{8,29498399996},
{8,29498399997},
{6,29500000000},
{7,29500000000},
{8,29500000000},
{6,29628399986},
{6,29628399987},
{6,29628399988},
{6,29628399989},
{6,29628399990},
{6,29628399991},
{6,29628399992},
{6,29628399993},
{6,29628399994},
{6,29628399995},
{7,29757536170},
{8,29882536171},
{7,29965000000},
{8,29965000000},
{7,29971736170},
{8,29986799989},
{8,29986799990},
{8,29986799991},
{8,29986799992},
{8,29986799993},
{8,29986799994},
{8,29986799995},
{8,29986799996},
{8,29986799997},
{8,29997400000},
{8,29998399989},
{8,29998399990},
{8,29998399991},
{8,29998399992},
{8,29998399993},
{8,29998399994},
{8,29998399995},
{8,29998399996},
{8,29998399997},
{4,30000000000},
{5,30000000000},
{6,30000000000},
{7,30000000000},
{8,30000000000},
{9,30000000000},
{4,30499999984},
{4,30499999985},
{4,30499999986},
{4,30499999987},
{4,30499999988},
{4,30499999989},
{4,30499999990},
{4,30499999991},
{4,30499999992},
{4,30499999993},
{4,30500000000},
{7,39465000000},
{8,39465000000},
{7,39471736170},
{8,39486799989},
{8,39486799990},
{8,39486799991},
{8,39486799992},
{8,39486799993},
{8,39486799994},
{8,39486799995},
{8,39486799996},
{8,39486799997},
{8,39497400000},
{8,39498399989},
{8,39498399990},
{8,39498399991},
{8,39498399992},
{8,39498399993},
{8,39498399994},
{8,39498399995},
{8,39498399996},
{8,39498399997},
{6,39500000000},
{7,39500000000},
{8,39500000000},
{6,39628399986},
{6,39628399987},
{6,39628399988},
{6,39628399989},
{6,39628399990},
{6,39628399991},
{6,39628399992},
{6,39628399993},
{6,39628399994},
{6,39628399995},
{7,39757536170},
{8,39882536171},
{7,39965000000},
{8,39965000000},
{7,39971736170},
{8,39986799989},
{8,39986799990},
{8,39986799991},
{8,39986799992},
{8,39986799993},
{8,39986799994},
{8,39986799995},
{8,39986799996},
{8,39986799997},
{8,39997400000},
{8,39998399989},
{8,39998399990},
{8,39998399991},
{8,39998399992},
{8,39998399993},
{8,39998399994},
{8,39998399995},
{8,39998399996},
{8,39998399997},
{5,40000000000},
{6,40000000000},
{7,40000000000},
{8,40000000000},
{9,40000000000},
{7,49465000000},
{8,49465000000},
{7,49471736170},
{8,49486799989},
{8,49486799990},
{8,49486799991},
{8,49486799992},
{8,49486799993},
{8,49486799994},
{8,49486799995},
{8,49486799996},
{8,49486799997},
{8,49497400000},
{8,49498399989},
{8,49498399990},
{8,49498399991},
{8,49498399992},
{8,49498399993},
{8,49498399994},
{8,49498399995},
{8,49498399996},
{8,49498399997},
{6,49500000000},
{7,49500000000},
{8,49500000000},
{6,49628399986},
{6,49628399987},
{6,49628399988},
{6,49628399989},
{6,49628399990},
{6,49628399991},
{6,49628399992},
{6,49628399993},
{6,49628399994},
{6,49628399995},
{7,49757536170},
{8,49882536171},
{7,49965000000},
{8,49965000000},
{7,49971736170},
{8,49986799989},
{8,49986799990},
{8,49986799991},
{8,49986799992},
{8,49986799993},
{8,49986799994},
{8,49986799995},
{8,49986799996},
{8,49986799997},
{8,49997400000},
{8,49998399989},
{8,49998399990},
{8,49998399991},
{8,49998399992},
{8,49998399993},
{8,49998399994},
{8,49998399995},
{8,49998399996},
{8,49998399997},
{6,50000000000},
{7,50000000000},
{8,50000000000},
{9,50000000000},
{7,59465000000},
{8,59465000000},
{7,59471736170},
{8,59486799989},
{8,59486799990},
{8,59486799991},
{8,59486799992},
{8,59486799993},
{8,59486799994},
{8,59486799995},
{8,59486799996},
{8,59486799997},
{8,59497400000},
{8,59498399989},
{8,59498399990},
{8,59498399991},
{8,59498399992},
{8,59498399993},
{8,59498399994},
{8,59498399995},
{8,59498399996},
{8,59498399997},
{6,59500000000},
{7,59500000000},
{8,59500000000},
{6,59628399986},
{6,59628399987},
{6,59628399988},
{6,59628399989},
{6,59628399990},
{6,59628399991},
{6,59628399992},
{6,59628399993},
{6,59628399994},
{6,59628399995},
{7,59757536170},
{8,59882536171},
{7,59965000000},
{8,59965000000},
{7,59971736170},
{8,59986799989},
{8,59986799990},
{8,59986799991},
{8,59986799992},
{8,59986799993},
{8,59986799994},
{8,59986799995},
{8,59986799996},
{8,59986799997},
{8,59997400000},
{8,59998399989},
{8,59998399990},
{8,59998399991},
{8,59998399992},
{8,59998399993},
{8,59998399994},
{8,59998399995},
{8,59998399996},
{8,59998399997},
{7,60000000000},
{8,60000000000},
{9,60000000000},
{7,69465000000},
{8,69465000000},
{7,69471736170},
{8,69486799989},
{8,69486799990},
{8,69486799991},
{8,69486799992},
{8,69486799993},
{8,69486799994},
{8,69486799995},
{8,69486799996},
{8,69486799997},
{8,69497400000},
{8,69498399989},
{8,69498399990},
{8,69498399991},
{8,69498399992},
{8,69498399993},
{8,69498399994},
{8,69498399995},
{8,69498399996},
{8,69498399997},
{7,69500000000},
{8,69500000000},
{7,69757536170},
{8,69882536171},
{7,69965000000},
{8,69965000000},
{7,69971736170},
{8,69986799989},
{8,69986799990},
{8,69986799991},
{8,69986799992},
{8,69986799993},
{8,69986799994},
{8,69986799995},
{8,69986799996},
{8,69986799997},
{8,69997400000},
{8,69998399989},
{8,69998399990},
{8,69998399991},
{8,69998399992},
{8,69998399993},
{8,69998399994},
{8,69998399995},
{8,69998399996},
{8,69998399997},
{8,70000000000},
{9,70000000000},
{8,79465000000},
{8,79486799989},
{8,79486799990},
{8,79486799991},
{8,79486799992},
{8,79486799993},
{8,79486799994},
{8,79486799995},
{8,79486799996},
{8,79486799997},
{8,79497400000},
{8,79498399989},
{8,79498399990},
{8,79498399991},
{8,79498399992},
{8,79498399993},
{8,79498399994},
{8,79498399995},
{8,79498399996},
{8,79498399997},
{8,79500000000},
{8,79882536171},
{8,79965000000},
{8,79986799989},
{8,79986799990},
{8,79986799991},
{8,79986799992},
{8,79986799993},
{8,79986799994},
{8,79986799995},
{8,79986799996},
{8,79986799997},
{8,79997400000},
{8,79998399989},
{8,79998399990},
{8,79998399991},
{8,79998399992},
{8,79998399993},
{8,79998399994},
{8,79998399995},
{8,79998399996},
{8,79998399997},
{9,80000000000},
View Code