[HDU5435] A serious math problem(数位dp)

Problem Description

Xiao Jun likes math and he has a serious math question for you to finish.
Define F[x] to the xor sum of all digits of x under the decimal system,for example F(1234)=1 F ( 1234 ) = 1 ^ 2 2 ^ 3 ^ 4=4 4 = 4 .Two numbers a,b(ab) a , b ( a ≤ b ) are given,figure out the answer of F[a]+F[a+1]+F[a+2]++F[b2]+F[b1]+F[b F [ a ] + F [ a + 1 ] + F [ a + 2 ] + … + F [ b − 2 ] + F [ b − 1 ] + F [ b ] doing a modulo 109+7 10 9 + 7 .

Input

The first line of the input is a single integer T(T<26) T ( T < 26 ) , indicating the number of testcases.

Then T testcases follow.In each testcase print three lines :

The first line contains one integers a a .
The second line contains one integers b.
1|a|,|b|100001 1 ≤ | a | , | b | ≤ 100001 ,|a| , | a | means the length of a a .

Output

For each test case, output one line “Case #x: y”, where x is the case number (starting from 1) and y is the answer.

Sample Input

4
0
1
2
2
1
10
9999
99999

Sample Output

Case #1: 1
Case #2: 2
Case #3: 46
Case #4: 649032

中文大意

定义 F[x] F [ x ] x x 在十进制表示下各位数字的异或和,例如F(1234)=1 ^ 2 2 ^ 3 ^ 4=4 4 = 4 。给你两个数 a,b(ab) a , b ( a ≤ b ) 。求 F[a]+F[a+1]+F[a+2]++F[b2]+F[b1]+F[b] F [ a ] + F [ a + 1 ] + F [ a + 2 ] + … + F [ b − 2 ] + F [ b − 1 ] + F [ b ] 109+7 10 9 + 7 的值 。

题解

定义 dp[i][j] d p [ i ] [ j ] 为有 i i 位且数位的异或和位j的数的数量, ans[i] a n s [ i ] 为数位异或和为 i i 的数的数量,然后输入之前先预处理出dp数组,然后每次询问算出 1 1 ~a的总数和 1 1 ~b的总数相减就可以了。

Code

#include<bits/stdc++.h>
using namespace std;

const int mod=1e9+7;
int t,tot=0;
long long dp[100010][20],ans[20];
char a[100010],b[100010];

long long solve(char *s){
    memset(ans,0,sizeof(ans));
    long long len=strlen(s),pre=0,ret=0;
    for(int i=0;i<len;i++){
        int p=s[i]-'0';
        for(int j=0;j<p;j++)
          for(int k=0;k<16;k++)
//            ans[j^pre^k]=(ans[j^pre^k]+dp[len-i-1][k])%mod;
            ans[k]=(ans[k]+dp[len-i-1][k^j^pre]);
        pre^=p;
    }
    for(int i=1;i<16;i++)ret=(ret+(long long)i*ans[i])%mod;
    return ret;
}

int main()
{
    scanf("%d",&t);
    dp[0][0]=1;
    for(int i=0;i<10;i++)dp[1][i]=1;
    for (int i = 2; i < 100010; i++)  
        for (int k = 0; k < 10; k++)  
            for (int j = 0; j <= 15; j++)  
            {  
                dp[i][j ^ k] += dp[i - 1][j];  
                dp[i][j ^ k] %= mod;  
            }  
//    for(int i=2;i<=100010;i++)
//      for(int j=0;j<16;j++)
//        for(int k=0;k<=9;k++)
//          dp[i][j]=(dp[i][j]+dp[i-1][j^k])%mod;
    while(t--){
        scanf("%s",a);
        scanf("%s",b);
        int add=0,len=strlen(b);
        for(int i=0;i<len;i++)add^=b[i]-'0';
        printf("Case #%d: %lld\n",++tot,((solve(b)-solve(a)+(long long)add)%mod+mod)%mod);
    }
    return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值