zoukankan      html  css  js  c++  java
  • hdu 4507 吉哥系列故事——恨7不成妻 数位DP

    数位DP,注意状态DP的转移

    代码如下:

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<algorithm>
     4 #include<iomanip>
     5 #include<cmath>
     6 #include<cstring>
     7 #include<vector>
     8 #define ll __int64
     9 #define mod 1000000007
    10 using namespace std;
    11 struct node
    12 {
    13     ll n,sum,pow;
    14     node():n(0),sum(0),pow(0){}
    15 }dp[20][7][7];
    16 int bit[20];
    17 ll p1[20];
    18 node dfs(int pos,int m,int sum,bool f)
    19 {
    20     node p,q;
    21     if(pos==-1){
    22         p.n=m&&sum;
    23         return p;
    24     }
    25     if(!f&&dp[pos][sum][m].n!=-1) return dp[pos][sum][m];
    26     int e=f?bit[pos]:9;
    27     for(int i=0;i<=e;i++){
    28         if(i==7) continue;
    29         q=dfs(pos-1,(10*m+i)%7,(sum+i)%7,f&&i==bit[pos]);
    30         p.n+=q.n;
    31         p.n%=mod;
    32         p.sum+=p1[pos]*i%mod*q.n+q.sum;
    33         p.sum%=mod;
    34         p.pow+=(p1[pos]*p1[pos]%mod*i%mod*i%mod*q.n+2*p1[pos]*i%mod*q.sum%mod+q.pow);
    35         p.pow%=mod;
    36     }
    37     if(!f) dp[pos][sum][m]=p;
    38     return p;
    39 }
    40 ll cal(ll n)
    41 {
    42     int m=0;
    43     ll s=n;
    44     while(n){
    45         bit[m++]=n%10;
    46         n/=10;
    47     }
    48     return dfs(m-1,0,0,1).pow;
    49 }
    50 int main(){
    51     int t;
    52     ll a,b;
    53     memset(dp,-1,sizeof(dp));
    54     p1[0]=1;
    55     for(int i=1;i<20;i++)
    56         p1[i]=p1[i-1]*10%mod;
    57     scanf("%d",&t);
    58     while(t--){
    59         scanf("%I64d%I64d",&a,&b);
    60         printf("%I64d
    ",(cal(b)-cal(a-1)+mod)%mod);
    61     }
    62     return 0;
    63 }
    View Code
  • 相关阅读:
    Leetcode 50.Pow(x,n) By Python
    Leetcode 347.前K个高频元素 By Python
    Leetcode 414.Fizz Buzz By Python
    Leetcode 237.删除链表中的节点 By Python
    Leetcode 20.有效的括号 By Python
    Leetcode 70.爬楼梯 By Python
    Leetcode 190.颠倒二进制位 By Python
    团体程序设计天梯赛 L1-034. 点赞
    Wannafly挑战赛9 C-列一列
    TZOJ Start
  • 原文地址:https://www.cnblogs.com/xin-hua/p/3308192.html
Copyright © 2011-2022 走看看