类欧几里得算法

算是知道了扩展欧几里得这个算法了吧。。。。

转载自http://www.cnblogs.com/cyz666/
这里写图片描述
这里写图片描述

f的模板

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

g的模板

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

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

typedef long long LL;

int a,b,c,l,r;

struct data
{
    int f,g,h;
};

data calc(int a,int b,int c,LL n)
{
    data 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;
    data 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()
{
    freopen("task.in","r",stdin); freopen("task.out","w",stdout);
    scanf("%d%d%d%d%d",&a,&c,&b,&l,&r);
    printf("%d\n",(calc(a,b,c,r).g-calc(a,b,c,l-1).g+mo)%mo);
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值