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
^ ^
4=4
4
=
4
.Two numbers
a,b(a≤b)
a
,
b
(
a
≤
b
)
are given,figure out the answer of
F[a]+F[a+1]+F[a+2]+…+F[b−2]+F[b−1]+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 .
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 ) 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 在十进制表示下各位数字的异或和,例如 ^ 2 2 ^ ^ 4=4 4 = 4 。给你两个数 a,b(a≤b) a , b ( a ≤ b ) 。求 F[a]+F[a+1]+F[a+2]+…+F[b−2]+F[b−1]+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 位且数位的异或和位的数的数量, ans[i] a n s [ i ] 为数位异或和为 i i 的数的数量,然后输入之前先预处理出数组,然后每次询问算出 1 1 ~的总数和 1 1 ~的总数相减就可以了。
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;
}