类欧几里得模板

求f

$\displaystyle f(a, b, c, n) =   \sum_{i=0}^n\left \lfloor \frac{ai+b}{c} \right \rfloor$

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const ll mod = 1e9+7;

ll f(ll a, ll b, ll c, ll n)
{
    if(!a)  return (b/c)*(n+1)%mod;
    if(a >= c || b >= c)  return (f(a%c, b%c, c, n) + n*(n+1)/2%mod*(a/c)%mod + (n+1)*(b/c)%mod) % mod;
    ll m = (a*n+b) / c;
    return (n*m%mod - f(c, c-b-1, a, m-1) + mod)%mod;
}

int main()
{
    printf("%lld\n", f(1, 0, 2, 5));  //0+1+1+2+2=6
}

 

猜你喜欢

转载自www.cnblogs.com/lfri/p/11657653.html