hdu 4507 吉哥系列故事——恨7不成妻 數位DP

來源:互聯網
上載者:User

中文題

做法:奇葩,要求區間中數位平方和。sigma(a1,a2... an) = n*sigma(first)*sigma(first) + 2*sigma(first)*sigma(he) + sigma(squre);

a1,..a2...an代表區間中的每個數, first 代表他們共用的首位*10^(i - 1),i是這個數的位元,squre 代表平方和, he 代表其他位元組成數位和。

#include <cstdio>#include <cstring>typedef __int64 LL;const int mod = 1000000000 + 7;const int LMT = 7;const int LEN = 20;//一開始的取膜方法寫錯了,重複不同數的取膜最後答案會錯誤//為了保險,能模數就模數LL _squre[7][7][LEN], _sum[7][7][LEN],ten[LEN], have[7][7][LEN];int num[LEN];void init(void){   memset(_squre, -1, sizeof(_squre));   memset(_sum, -1, sizeof(_sum));   memset(have, -1, sizeof(have));}struct __ret{    LL sq, sm, hv;    __ret(LL a, LL b, LL c): sq(a), sm(b), hv(c) {}    __ret():sq(0), sm(0), hv(0){}};__ret dfs(const int &now, const int &rest, const int &sum, const bool &tag){    if(rest == 0)     {        if (now && sum) return __ret(0, 0, 1);        else return __ret(-1, -1, 0);    }    if (!tag && _squre[now][sum][rest] != -1)        return __ret(_squre[now][sum][rest], _sum[now][sum][rest], have[now][sum][rest]);    int i,end = (tag ? num[rest] : 9);    __ret tem;    LL sqr = 0, sm = 0, hv = 0, prep;    for(i = 0; i <= end; ++i)    if(i != 7)    {            prep = i * ten[rest - 1] % mod;            tem = dfs((now*10 + i)% 7, rest - 1, (sum + i) % 7, tag && (i == end));            if(tem.sq != -1)            {              sqr += (prep * prep % mod * tem.hv % mod + 2 * tem.sm * prep %mod  + tem.sq) % mod;              sqr %= mod;              sm += (tem.hv%mod*prep % mod + tem.sm) % mod;              sm %= mod;              hv += tem.hv;              hv %= mod;            }    }    if(!tag)//這裡又錯了return了數組值,哭...    {        _squre[now][sum][rest] = sqr;        _sum[now][sum][rest] = sm;        have[now][sum][rest] = hv;    }    return __ret(sqr, sm, hv);}int get_num(LL x){    int res = 0;    do      num[++res] = int(x % 10);    while(x /= 10);    return res;}int main(void){    init();    int T,lena,lenb;    LL a,b,resa,resb, ans;    init();    ten[0] = 1;    for(int i = 1; i <= 18; ++i)        ten[i] = (ten[i - 1] * 10) % mod;    scanf("%d", &T);    while(T--)    {        scanf("%I64d%I64d",&a, &b);        if(a > b)        {            a = a ^ b;            b = a ^ b;            a = a ^ b;        }        a--;        lena = get_num(a);        resa = dfs(0, lena, 0, 1).sq;        lenb = get_num(b);        resb = dfs(0, lenb, 0, 1).sq;        ans = resb - resa;        if(ans < 0) ans = ans + mod;        printf("%I64d\n", ans);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.