UVA 11361 - Investigating Div-Sum Property 数位DP

时间:2023-03-08 16:56:11

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 = +;
const int M = ;
const int inf = 0x3f3f3f3f;
const ll MOD = ; ll a,b,k,len;
ll vis[][N][N],dp[][N][N],d[];
void init(int n) {
len = ;
mem(d);
while(n) d[len++] = n%,n /= ; for(int i = ;i <= len/; i++)
swap(d[i],d[len-i+]);
}
ll solve(ll n) {
if(n == ) return ;
init(n);
mem(dp);
int p = , q = ; for(int i=;i<=len;i++) { for(int j=;j<=k;j++)
for(int t = ;t <= k; t++) {
for(int x = ;x < ; x++) {
dp[i][(j*+x)%k][(t+x)%k] += dp[i-][j][t];
}
} for(int j = ; j < d[i]; j++)
dp[i][(p*+j)%k][(q+j)%k]++; p = (p*+d[i])%k;
q = (q+d[i])%k;
}
if(p == && q == ) dp[len][][]++;
return dp[len][][];
}
int main() {
int T;
scanf("%d",&T);
while(T--) {
scanf("%lld%lld%lld",&a,&b,&k);
if(k>) printf("0\n");
else
printf("%lld\n",solve(b)-solve(a-));
}
return ;
}

bear

//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 = +;
const int M = ;
const int inf = 0x3f3f3f3f;
const ll MOD = ; ll a,b,k;
ll vis[][N][N],dp[][N][N],d[];
ll dfs(int dep,int f,int sum,int P) {
if(dep<) return sum%k==&&P%k==;
if(f&&vis[dep][sum][P]) return dp[dep][sum][P];
if(f) {
ll& ret = dp[dep][sum][P];
vis[dep][sum][P] = ;
for(int i=;i<=;i++) {
ret += dfs(dep-,f,(sum*+i)%k,P+i);
}
return ret;
}
else {
ll ret = ;
for(int i=;i<=d[dep];i++) {
ret +=dfs(dep-,i<d[dep],(sum*+i)%k,P+i);
}
return ret;
}
}
ll solve(int n) {
mem(vis),mem(dp);
int len = ;
while(n) d[len++] = n%,n /= ;
return dfs(len-,,,);
}
int main() {
int T;
scanf("%d",&T);
while(T--) {
scanf("%lld%lld%lld",&a,&b,&k);
printf("%lld\n",solve(b)-solve(a-));
}
return ;
}

meek