我想为什么这道题直接套模板不能做出来呢?
查看原帖
我想为什么这道题直接套模板不能做出来呢?
813227
Hunter19019楼主2023/4/28 16:38
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#include<cstring>
#include<queue>
#include<map>
#include<set>
#define x first
#define y second
using namespace std;
typedef long long ll;
const int N = 3e+6+100;

typedef pair<int,int> PII;

ll read()
{
    ll x=0,f=1;
    char c=getchar();
    while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}
    while(c>='0'&&c<='9') x=x*10+c-'0',c=getchar();
    return x*f;
}

const int mo=1e9+7,inv2=500000004,inv6=166666668;




struct node
{
    ll f,g,h;
};

node calc(ll a,ll b,ll c,ll n)
{
    node tmp;
    if (!a)
    {
        tmp.f=tmp.g=tmp.h=0;
        return tmp;
    }
    if (a>=c || b>=c)
    {
        tmp=calc(a%c,b%c,c,n);
        n%=mo;
        tmp.h=(tmp.h+
               n*(n+1)%mo*(2*n+1)%mo*inv6%mo*(a/c)%mo*(a/c)%mo
               +(n+1)*(b/c)%mo*(b/c)%mo
               +(ll)2*(a/c)*tmp.g%mo
               +(ll)2*(b/c)*tmp.f%mo
               +n*(n+1)%mo*(a/c)%mo*(b/c))%mo;
        tmp.f=(tmp.f
               +n*(n+1)/2%mo*(a/c)
               +(n+1)*(b/c))%mo;
        tmp.g=(tmp.g
               +n*(n+1)%mo*(2*n+1)%mo*inv6%mo*(a/c)
               +n*(n+1)/2%mo*(b/c))%mo;
        return tmp;
    }
    ll m=((ll)a*n+b)/c;
    node nxt=calc(c,c-b-1,a,m-1);
    n%=mo; m%=mo;
    tmp.f=((n*m-nxt.f)%mo+mo)%mo;
    tmp.g=(ll)((n*(n+1)%mo*m-nxt.f-nxt.h)%mo+mo)*inv2%mo;
    tmp.h=((m*(m+1)%mo*n-(ll)2*(nxt.g+nxt.f)%mo-tmp.f)%mo+mo)%mo;
    return tmp;
}

int main(){
    ll a,b,c,n;
    a = read();
    b = read();
    c = read();
    ll ans1;
    if(b > a)
    {
        n = c / a;
        auto ans = calc(b-a,c,b,n);
        ans1 = ans.f;
    }
    else
    {
        n = c / b;
        auto ans = calc(a-b,c,a,n);
        ans1 = ans.f;
    }


    ans1 -= (n*(n+1) >> 1);
    ans1 += n+1;
    cout << ans1;
    return 0;
}

直接套板子出不来,必须单独用f函数,这是为什么呢?

2023/4/28 16:38
加载中...