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

UVA 11361 - Investigating Div-Sum Property 数位DP的更多相关文章

  1. 【数位dp】UVA - 11361 - Investigating Div-Sum Property

    经典数位dp!而且这好像是数位dp的套路板子……不需要讨论原来我很头疼的一些边界. 改天用这个板子重做一下原来的一些数位dp题目. http://blog.csdn.net/the_useless/a ...

  2. UVa 11361 - Investigating Div-Sum Property

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  3. Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum (数位dp求和)

    题目链接:https://codeforces.com/contest/1073/problem/E 题目大意:给定一个区间[l,r],需要求出区间[l,r]内符合数位上的不同数字个数不超过k个的数的 ...

  4. CodeForces - 1073E :Segment Sum (数位DP)

    You are given two integers l l and r r (l≤r l≤r ). Your task is to calculate the sum of numbers from ...

  5. Codeforces1073E Segment Sum 【数位DP】

    题目分析: 裸的数位DP,注意细节. #include<bits/stdc++.h> using namespace std; ; int k; ][],sz[][],cnt[][]; ] ...

  6. UVA - 10891 Game of Sum (区间dp)

    题意:AB两人分别拿一列n个数字,只能从左端或右端拿,不能同时从两端拿,可拿一个或多个,问在两人尽可能多拿的情况下,A最多比B多拿多少. 分析: 1.枚举先手拿的分界线,要么从左端拿,要么从右端拿,比 ...

  7. E. Segment Sum(数位dp)

    题意:求一个区间内满足所有数位不同数字个数小于K的数字总和.比如:k=2   1,2,3所有数位的不同数字的个数为1满足,但是123数位上有三个不同的数字,即123不满足. 我们可以使用一个二进制的数 ...

  8. UVA 10891 Game of Sum(区间DP(记忆化搜索))

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  9. 28.uva 10891 Game of Sum 记忆化dp

    这题和上次的通化邀请赛的那题一样,而且还是简化版本... 那题的题解      请戳这里 ... #include<cstdio> #include<algorithm> #i ...

随机推荐

  1. 4.FPGA芯片管脚解释

    用户I/O:不用解释了.   配置管脚: MSEL[1:0] 用于选择配置模式,比如AS.PS等. DATA0 FPGA串行数据输入,连接到配置器件的串行数据输出管脚. DCLK FPGA串行时钟输出 ...

  2. linux校准时间

    Linux下ntpdate时间同步 Ntp服务器安装配置 ntp(Network Time Protocol)协议 RedHat服务器可以下载rpm安装包,然后执行# rpm -ivh ntp-4.2 ...

  3. sql优化点整理

    此文是我最早开始sql优化至今整理的小知识点和经常遇到的问题,弄懂这些对优化大型的sql会有不少帮助 ---------------------------------使用了多余的外连接------- ...

  4. C++中栈的出栈,入栈规则:A,B,C,D,E

    考题: 栈底至栈顶一次存放元素 ABCD 在第五个元素E入栈之前  栈中元素可以出栈,则出栈序列可能是_____a d___________. a.  ABCED b.  DBCEA   c.  CD ...

  5. VIM技巧:翻页

    整页翻页 ctrl-f ctrl-bf=forword b=backward 翻半页ctrl-d ctlr-ud=down u=up 滚一行ctrl-e ctrl-y zz 让光标所在的行居屏幕中央z ...

  6. Careercup - Microsoft面试题 - 5718181884723200

    2014-05-11 05:55 题目链接 原题: difference between thread and process. 题目:请描述进程和线程的区别. 解法:操作系统理论题.标准答案在恐龙书 ...

  7. android开发类似coverflow效果的3d旋转

    源码下载地址:http://download.csdn.net/detail/feijian_/8888219

  8. hdu 1285 确定比赛名次 拓扑排序

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛 ...

  9. 【BZOJ】【1911】【APIO2010】特别行动队commando

    DP/斜率优化 嗯……第三道斜率优化的题目了. 定义 $s[i]=\sum_{k=1}^{i} x[k] $ 方程:$f[i]=max\{ f[j]+a*(s[i]-s[j])^2+b*(s[i]-s ...

  10. 【BZOJ】【1034】【ZJOI2008】泡泡堂BNB

    贪心 类似田忌赛马策略的一个贪心= = 随便YY了一个做法居然A了…… 简单来说就是先强对强,弱对弱,能赢就赢,不能赢就让弱的那个去对强的那个,剩下的人继续依次捉对比赛(继续刚刚的策略),现在人数还是 ...