您的位置:首页 > 其它

lightOJ 1068 - Investigation (数位dp)

2016-07-15 11:03 218 查看
1068 - Investigation

PDF (English) Statistics Forum

Time Limit: 2 second(s) Memory Limit: 32 MB

An integer is divisible by 3 if the sum of its digits is also divisible by 3. For example, 3702 is divisible by 3 and 12 (3+7+0+2) is also divisible by 3. This property also holds for the integer 9.

In this problem, we will investigate this property for other integers.

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case contains three positive integers A, B and K (1 ≤ A ≤ B < 231 and 0 < K < 10000).

Output

For each case, output the case number and the number of integers in the range [A, B] which are divisible by K and the sum of its digits is also divisible by K.

Sample Input

Output for Sample Input

3

1 20 1

1 20 2

1 1000 4

Case 1: 20

Case 2: 5

Case 3: 64

这种题目考验智商!!

n大于82的时候是没用的,因为数位之和小于82.

还有,k在变化

所以每次都要memset

#include<iostream>
using namespace std;
#include<cstring>
#include<cstdio>
#include<algorithm>

int digit[20];
int k;
int dp[10][83][83];

int dfs(int pos,int lim,int dig,int sum) {
if(pos<0)return dig==0&&sum==0;
if(!lim&&(~dp[pos][dig][sum]))return dp[pos][dig][sum];
int ans=0;
int len=lim?digit[pos]:9;
for(int i=0; i<=len; ++i) {
ans+=dfs(pos-1,lim&&(i==len),((dig*10+i)%k)%83,(sum+i)%k);
}

if(!lim)dp[pos][dig][sum]=ans;
return ans;
}
int work(int val) {
int len=0;
while(val) {
digit[len++]=val%10;
val/=10;
}
return dfs(len-1,1,0,0);
}

int main() {
int n,m,T;
int coun=0;
scanf("%d",&T);
while(T--&&scanf("%d%d%d",&n,&m,&k)) {
memset(dp,-1,sizeof(dp));
printf("Case %d: %d\n",++coun,(work(m)-work(n-1)));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: