首页 > 代码库 > HDU 4507 吉哥系列故事——恨7不成妻

HDU 4507 吉哥系列故事——恨7不成妻

需要推下平方和的式子。。维护个数,和,平方和。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define mod 1000000007LL
using namespace std;
long long bit[30],tab[50],ret=0,t,l,r;
struct pnt
{
    long long val1,val2,val3;
    pnt (long long val1,long long val2,long long val3):val1(val1),val2(val2),val3(val3) {}
    pnt () {}
}dp[30][30][30];
void get_bit(long long x)
{
    ret=0;
    while (x) {bit[++ret]=x%10;x/=10;}
}
void get_table()
{
    tab[0]=1;
    for (long long i=1;i<=40;i++) tab[i]=tab[i-1]*10%mod;
    for (long long i=0;i<=19;i++)
        for (long long j=0;j<=9;j++)
            for (long long k=0;k<=9;k++)
            {
                dp[i][j][k].val1=dp[i][j][k].val2=dp[i][j][k].val3=-1;
            }
    return;
}
pnt comb(pnt x,pnt y,long long pos,long long num)
{
    long long r1=0,r2=0,r3=0;
    r1=((y.val3*tab[2*(pos-1)]%mod*num%mod*num%mod+(2*(y.val2*tab[pos-1]%mod)%mod*num)%mod)%mod+y.val1)%mod;
    r2=(y.val3*num%mod*tab[pos-1]%mod+y.val2)%mod;
    r3=y.val3;
    x.val1=(x.val1+r1)%mod;
    x.val2=(x.val2+r2)%mod;
    x.val3=(x.val3+r3)%mod;
    return x;
}
pnt dfs(long long pos,long long val1,long long val2,bool flag)
{
    if (!pos)
    {
        if (val1 && val2) return pnt(0,0,1);
        else return pnt(0,0,0);
    }
    if ((!flag) && (~dp[pos][val1][val2].val1)) return dp[pos][val1][val2];
    pnt ans=pnt(0,0,0);long long up=flag?bit[pos]:9;
    for (long long i=0;i<=up;i++)
    {
        if (i==7) continue;
        ans=comb(ans,dfs(pos-1,(val1*10+i)%7,(val2+i)%7,flag&&i==up),pos,i);
    }
    if (!flag) dp[pos][val1][val2]=ans;
    return ans;
}
long long work(long long x)
{
    get_bit(x);
    return dfs(ret,0,0,1).val1;
}
int main()
{
    scanf("%I64d",&t);get_table();
    for (long long i=1;i<=t;i++)
    {
        scanf("%I64d%I64d",&l,&r);
        printf("%I64d\n",(work(r)-work(l-1)+mod)%mod);
    }    
    return 0;
}

 

HDU 4507 吉哥系列故事——恨7不成妻