UVA 11361 - Investigating Div-Sum Property 数位DP
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的更多相关文章
- 【数位dp】UVA - 11361 - Investigating Div-Sum Property
经典数位dp!而且这好像是数位dp的套路板子……不需要讨论原来我很头疼的一些边界. 改天用这个板子重做一下原来的一些数位dp题目. http://blog.csdn.net/the_useless/a ...
- UVa 11361 - Investigating Div-Sum Property
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum (数位dp求和)
题目链接:https://codeforces.com/contest/1073/problem/E 题目大意:给定一个区间[l,r],需要求出区间[l,r]内符合数位上的不同数字个数不超过k个的数的 ...
- 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 ...
- Codeforces1073E Segment Sum 【数位DP】
题目分析: 裸的数位DP,注意细节. #include<bits/stdc++.h> using namespace std; ; int k; ][],sz[][],cnt[][]; ] ...
- UVA - 10891 Game of Sum (区间dp)
题意:AB两人分别拿一列n个数字,只能从左端或右端拿,不能同时从两端拿,可拿一个或多个,问在两人尽可能多拿的情况下,A最多比B多拿多少. 分析: 1.枚举先手拿的分界线,要么从左端拿,要么从右端拿,比 ...
- E. Segment Sum(数位dp)
题意:求一个区间内满足所有数位不同数字个数小于K的数字总和.比如:k=2 1,2,3所有数位的不同数字的个数为1满足,但是123数位上有三个不同的数字,即123不满足. 我们可以使用一个二进制的数 ...
- UVA 10891 Game of Sum(区间DP(记忆化搜索))
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- 28.uva 10891 Game of Sum 记忆化dp
这题和上次的通化邀请赛的那题一样,而且还是简化版本... 那题的题解 请戳这里 ... #include<cstdio> #include<algorithm> #i ...
随机推荐
- 为iPhone6设计自适应布局
Apple从iOS6加入了Auto Layout后开始就比较委婉的开始鼓励.建议开发者使用自适应布局,但是到目前为止,我感觉大多数开发者一直在回避这个问题,不管是不是由于历史原因造成的,至少他们在心底 ...
- 归并排序 & 计数排序 & 基数排序 & 冒泡排序 & 选择排序 ----> 内部排序性能比较
2.3 归并排序 接口定义: int merge(void* data, int esize, int lpos, int dpos, int rpos, int (*compare)(const v ...
- .net(C#)操作文件的几种方法汇总
.net(C#)操作文件的几种方法汇总 System.IO命名空间下类的用法:在System.IO名称空间中包含了用于文件输入输出的主要类.File:实用类,提供许多静态方法,用于移动.复制和删除文件 ...
- easyui 布局自适应
最近在把以前写的一个项目改成用easyui做前端.过程中遇到了不少问题.其中一个就是datagrid不能很好的布局.想了好多办法都有局限.最后想到会不会是布局(easyui-layout)的问题,经过 ...
- ubuntu下xampp的安装
转载:http://blog.csdn.net/zhoushengchao/article/details/6006584 首先,请从www.xampp.org下载最新版XAMPP.然后,将安装文件服 ...
- 17.Quartus 怎么回读CPLD里面的东西
可以使用Quartus® II Programmer的“Examine”特性回读编程目标文件(.POF)是CPLD不是FPGA 先用auto检测加没加加密位,然后执行ex,然后save,Examine ...
- 微软职位内部推荐-Sr SDE
微软近期Open的职位: MSN reaches nearly half a billion people across the globe where we are the #1 portal in ...
- Capacity Scheduler 队列设置
先附一个官网地址 Capacity Scheduler是YARN中默认的资源调度器. 相关参数配置: 资源分配相关参数 ``` (1) capacity:队列的资源容量(百分比). 当系统非常繁忙时, ...
- 随机四则运算 C语言
设计思想:出三十道一百以内数的随机四则运算题目,先随机两个一百以内的数字,再通过随机数确定四则运算算符,最后通过for循环输出三十道 源代码程序: #include <stdlib.h># ...
- jQuery中ready与load事件
jQuery中ready与load事件(来自慕课网) jQuery有3种针对文档加载的方法 $(document).ready(function() { // ...代码... }) //docume ...