【jzoj3327】【陶陶的难题】【类欧几里得】

来源:互联网 发布:淘宝美工作品欣赏 编辑:程序博客网 时间:2024/06/13 07:40

description

陶陶给Crash出了一个大难题,他要求Crash计算出下面式子的值:

这里写图片描述

其中A,B,C,L,R均为给定正整数。由于答案可能会很大,你只需要输出答案mod 1,000,000,007后的值。

solution

可以发现这就是裸的类欧,求出g即可。

类欧几里得问题推导

code

#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>#define LL long long#define fo(i,j,k) for(int i=j;i<=k;i++)#define fd(i,j,k) for(int i=j;i>=k;i--)#define fr(i,j) for(int i=begin[j];i;i=next[i])using namespace std;int const mn=1e5+9,mo=1e9+7;int a,b,c,l,r;LL ni2,ni6;struct rec{    LL f,g,h;    rec(LL F,LL G,LL H){f=F;g=G;h=H;}};rec likegcd(LL a,LL b,LL c,LL n){    if(!a)return rec(b/c*(n+1)%mo,        b/c*(n+1)%mo*n%mo*ni2%mo,        (b/c)*(b/c)%mo*(n+1)%mo);    if((a>=c)||(b>=c)){        rec tmp=likegcd(a%c,b%c,c,n);        return rec((a/c*n%mo*(n+1)%mo*ni2%mo+b/c*(n+1)%mo+tmp.f)%mo,        (a/c*(2*n+1)%mo*(n+1)%mo*n%mo*ni6%mo            +b/c*(n+1)%mo*n%mo*ni2%mo+tmp.g)%mo,        ((a/c)*(a/c)%mo*(2*n+1)%mo*(n+1)%mo*n%mo*ni6%mo            +(b/c)*(b/c)%mo*(n+1)%mo+(a/c)*(b/c)%mo*(n+1)%mo*n%mo            +2*(b/c)*tmp.f%mo+2*(a/c)*tmp.g%mo+tmp.h)%mo);    }    LL m=(a*n+b)/c;    rec tmp=likegcd(c,c-b-1,a,m-1);    return rec((n*m%mo-tmp.f)%mo,        (n*m%mo*(n+1)%mo*ni2%mo-tmp.f*ni2%mo-tmp.h*ni2%mo)%mo,        (n*m%mo*(m+1)%mo-2*tmp.f-2*tmp.g-(n*m%mo-tmp.f)%mo)%mo);}LL Pow(LL x,LL y){    LL z=1;    while(y){        if(y&1)z=z*x%mo;        x=x*x%mo;        y>>=1;    }    return z;}int main(){    freopen("task.in","r",stdin);    freopen("task.out","w",stdout);    scanf("%d%d%d%d%d",&a,&b,&c,&l,&r);    ni2=Pow(2,mo-2),ni6=Pow(6,mo-2);    rec tmp=likegcd(a,c,b,r);    rec tm2=likegcd(a,c,b,l-1);    printf("%lld",((tmp.g-tm2.g)%mo+mo)%mo);    return 0;}
原创粉丝点击