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

  1. //meek///#include<bits/stdc++.h>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include<iostream>
  7. #include<bitset>
  8. #include<vector>
  9. #include <queue>
  10. #include <map>
  11. #include <set>
  12. #include <stack>
  13. using namespace std ;
  14. #define mem(a) memset(a,0,sizeof(a))
  15. #define pb push_back
  16. #define fi first
  17. #define se second
  18. #define MP make_pair
  19. typedef long long ll;
  20.  
  21. const int N = +;
  22. const int M = ;
  23. const int inf = 0x3f3f3f3f;
  24. const ll MOD = ;
  25.  
  26. ll a,b,k,len;
  27. ll vis[][N][N],dp[][N][N],d[];
  28. void init(int n) {
  29. len = ;
  30. mem(d);
  31. while(n) d[len++] = n%,n /= ;
  32.  
  33. for(int i = ;i <= len/; i++)
  34. swap(d[i],d[len-i+]);
  35. }
  36. ll solve(ll n) {
  37. if(n == ) return ;
  38. init(n);
  39. mem(dp);
  40. int p = , q = ;
  41.  
  42. for(int i=;i<=len;i++) {
  43.  
  44. for(int j=;j<=k;j++)
  45. for(int t = ;t <= k; t++) {
  46. for(int x = ;x < ; x++) {
  47. dp[i][(j*+x)%k][(t+x)%k] += dp[i-][j][t];
  48. }
  49. }
  50.  
  51. for(int j = ; j < d[i]; j++)
  52. dp[i][(p*+j)%k][(q+j)%k]++;
  53.  
  54. p = (p*+d[i])%k;
  55. q = (q+d[i])%k;
  56. }
  57. if(p == && q == ) dp[len][][]++;
  58. return dp[len][][];
  59. }
  60. int main() {
  61. int T;
  62. scanf("%d",&T);
  63. while(T--) {
  64. scanf("%lld%lld%lld",&a,&b,&k);
  65. if(k>) printf("0\n");
  66. else
  67. printf("%lld\n",solve(b)-solve(a-));
  68. }
  69. return ;
  70. }

bear

  1. //meek///#include<bits/stdc++.h>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <algorithm>
  6. #include<iostream>
  7. #include<bitset>
  8. #include<vector>
  9. #include <queue>
  10. #include <map>
  11. #include <set>
  12. #include <stack>
  13. using namespace std ;
  14. #define mem(a) memset(a,0,sizeof(a))
  15. #define pb push_back
  16. #define fi first
  17. #define se second
  18. #define MP make_pair
  19. typedef long long ll;
  20.  
  21. const int N = +;
  22. const int M = ;
  23. const int inf = 0x3f3f3f3f;
  24. const ll MOD = ;
  25.  
  26. ll a,b,k;
  27. ll vis[][N][N],dp[][N][N],d[];
  28. ll dfs(int dep,int f,int sum,int P) {
  29. if(dep<) return sum%k==&&P%k==;
  30. if(f&&vis[dep][sum][P]) return dp[dep][sum][P];
  31. if(f) {
  32. ll& ret = dp[dep][sum][P];
  33. vis[dep][sum][P] = ;
  34. for(int i=;i<=;i++) {
  35. ret += dfs(dep-,f,(sum*+i)%k,P+i);
  36. }
  37. return ret;
  38. }
  39. else {
  40. ll ret = ;
  41. for(int i=;i<=d[dep];i++) {
  42. ret +=dfs(dep-,i<d[dep],(sum*+i)%k,P+i);
  43. }
  44. return ret;
  45. }
  46. }
  47. ll solve(int n) {
  48. mem(vis),mem(dp);
  49. int len = ;
  50. while(n) d[len++] = n%,n /= ;
  51. return dfs(len-,,,);
  52. }
  53. int main() {
  54. int T;
  55. scanf("%d",&T);
  56. while(T--) {
  57. scanf("%lld%lld%lld",&a,&b,&k);
  58. printf("%lld\n",solve(b)-solve(a-));
  59. }
  60. return ;
  61. }

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. Android之通过向WebService服务器发送XML数据获取相关服务

    原理图如下:        即客户端向WebService服务器通过HTTP协议发送XML数据(内部包含调用的一些方法和相关参数数据),然后WebService服务器给客户端返回一定的XML格式的数据 ...

  2. iOS8 无法设置定位服务

    针对iOS8系统,需要在plist文件中添加这两个参数 NSLocationAlwaysUsageDescription = YES NSLocationWhenInUseUsageDescripti ...

  3. iOS 七大手势之轻拍,长按,旋转手势识别器方法

    一.监听触摸事件的做法   如果想监听一个view上面的触摸事件,之前的做法通常是:先自定义一个view,然后再实现view的touches方法,在方法内部实现具体处理代码 通过touches方法监听 ...

  4. 4 我们的第一个c#程序

    1.       控制台应用程序. 在我们这个培训中主要使用控制台应用程序来讲解知识点和做练习. 什么是控制台程序? 控制台程序运行在dos窗口.没有可视化的界面.可以通过Dos窗口进入输入和输出显示 ...

  5. VC++程序中加入自定义声音(PlaySound函数用法)

    VC++编程中,我们可以为自己的程序加入音乐,比如当我们按下一个按钮时或者启动程序时,播放一小段音乐. 该功能用到函数: BOOL PlaySound(LPCSTR pszSound, HMODULE ...

  6. [2016-06-28]dhclient命令的进程没杀死,导致不断在向DHCP服务器获取IP

    # Date:2016-06-28 # 问题:主机的配置文件/etc/sysconfig/network-scripts/ifcfg-eth0 已经配置好了静态的IP. 但隔几分钟主机的IP就自己变化 ...

  7. vtune 不能查看pg的源代码

    在编译的时候,要加enable-debug 选项 并且如果之前有没有加的话,一定要make clean

  8. 寻找idea...

    域名:tianhuangdilao.com 天荒地老 现在闲置,寻求好的idea...

  9. 小组开发项目--NABC分析

    我们小组--女神经们,开发项目是重力解锁,我认为我们的项目的最大特点就是不使用开锁键唤醒屏幕.下面我将针对这一特点进行NABC分析: N:经调查一部分人群的手机不能使用就是开机键坏了,我们就是针对这一 ...

  10. SQL 语法 Join与Union

    问题描述: Join与Union使用 问题解决: Join连接,可以分为: tableA如下: tableB如下: 1.1.Inner Join SELECT * FROM TableA INNER ...