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
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
相关文章推荐
- 岁末将至:盘点2015年十佳Linux /Android黑客单片机
- 父Shell与子Shell
- linux+jenkins+tomcat+git 安装过程的的一些小问题
- Linux 下用yum安装mysql
- ARM-LINUX开发环境的搭建(三)
- #! /bin/sh 与 #! /bin/bash 的区别
- linux状态及原理全剖析
- 五大理由分配你的告警
- 高手的Blog及ACM好的网站[转]
- linux sort 命令详解
- Linux下进程信息的深入分析
- linux常用命令
- eclipse导入一个项目后,不能加载到tomcat里面
- ARM-LINUX开发环境搭建(二)
- linux上 pip install matplotlib 失败
- [Nginx]多个域名绑定一个IP 的nginx设置
- eclipse配置Tomcat服务器server locations的方法
- 利用 cronolog 对 tomcat 7 进行日志切割
- df、du、fdisk:Linux磁盘管理三板斧的使用心得
- 神奇的【brew】Mac上安装OpenCV