您的位置:首页 > 运维架构

UVA 11361 - Investigating Div-Sum Property 数位DP

2015-12-29 13:00 405 查看
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
The first line of input is an integer T (T < 100) that indicates the number of test cases. Each case is
a line containing 3 positive integers A, B and K. 1 ≤ A ≤ B < 2
31 and 0 < K < 10000.
Output
For each case, output the number of integers in the range [A, B] which is divisible by K and the sum
of its digits is also divisible by K.
Sample Input
3
1 20 1
1 20 2
1 1000 4
Sample Output
20
5
64

题意:给出a,b,k,问说在[a,b]这个区间有多少n,满足n整除k,以及n的各个为上的数字之和也整除k。

题解:dp[i][j][k] 表示 i位 j=数%K,k=位数和%K

//meek///#include<bits/stdc++.h>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<iostream>
#include<bitset>
#include<vector>
#include <queue>
#include <map>
#include <set>
#include <stack>
using namespace std ;
#define mem(a) memset(a,0,sizeof(a))
#define pb push_back
#define fi first
#define se second
#define MP make_pair
typedef long long ll;

const int N = 100+100;
const int M = 1000001;
const int inf = 0x3f3f3f3f;
const ll MOD = 1000000000;

ll a,b,k;
ll vis[100]

,dp[100]

,d[100];
ll dfs(int dep,int f,int sum,int P) {
if(dep<0) return sum%k==0&&P%k==0;
if(f&&vis[dep][sum][P]) return  dp[dep][sum][P];
if(f) {
ll& ret = dp[dep][sum][P];
vis[dep][sum][P] = 1;
for(int i=0;i<=9;i++) {
ret += dfs(dep-1,f,(sum*10+i)%k,P+i);
}
return ret;
}
else {
ll ret = 0;
for(int i=0;i<=d[dep];i++) {
ret +=dfs(dep-1,i<d[dep],(sum*10+i)%k,P+i);
}
return ret;
}
}
ll solve(int n) {
mem(vis),mem(dp);
int len = 0;
while(n) d[len++] = n%10,n /= 10;
return dfs(len-1,0,0,0);
}
int main() {
int T;
scanf("%d",&T);
while(T--) {
scanf("%lld%lld%lld",&a,&b,&k);
printf("%lld\n",solve(b)-solve(a-1));
}
return 0;
}


meek
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: