Investigating Div-Sum Property UVA - 11361
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
求n到m间有多少个数本身能整除k,各位相加和也能整除k,注意直接for循环用普通逻辑做会时间超限
需用到数位dp
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
int a,b,MOD,T;
int dp[][][],pow_10[];
int f(int d,int m1,int m2)//数位dp
{
if(dp[d][m1][m2]!=-)
{
return dp[d][m1][m2];
}
dp[d][m1][m2]=;
for(int i=; i<; i++)
{
dp[d][m1][m2]+=f(d-,(m1+i)%MOD,(m2+i*pow_10[d-])%MOD);
}
return dp[d][m1][m2];
}
int calc(int x)//处理下每个数
{
int len=;
if(!x)
{
len=;
}
int t=x;
while(t)
{
++len;
t/=;
}
int res=,LeftSide=,SumDigits=;
for(int i=; i<=len; i++)
{
while((ll)LeftSide+(ll)pow_10[len-i]-1ll<=(ll)x)
{
res+=f(len-i,SumDigits%MOD,LeftSide%MOD);
LeftSide+=pow_10[len-i];
++SumDigits;
}
}
return res;
}
int main()
{
scanf("%d",&T);
pow_10[]=;
for(int i=; i<=; ++i)
{
pow_10[i]=pow_10[i-]*;//首先存下每位的值,如0对应1,1对应10
}
for(; T; --T)
{
scanf("%d%d%d",&a,&b,&MOD);
if(MOD>)//2的31次方各位数之和最大为81
{
puts("");
continue;
}
memset(dp,-,sizeof(dp));
for(int i=; i<MOD; ++i)
{
for(int j=; j<MOD; ++j)
{
dp[][i][j]=;
}
}
dp[][][]=;
printf("%d\n",calc(b)-calc(a-));
}
return ;
}
Investigating Div-Sum Property UVA - 11361的更多相关文章
- UVa 11361 - Investigating Div-Sum Property
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- 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 d ...
- UVa 11361 (计数 递推) Investigating Div-Sum Property
题意: 统计[a, b]中有多少个数字满足:自身是k的倍数,而且各个数字之和也是k的倍数. 分析: 详细分析见<训练之南>吧,=_=|| 书上提出了一个模板的概念,有了模板我们就可以分块计 ...
- 【数位dp】UVA - 11361 - Investigating Div-Sum Property
经典数位dp!而且这好像是数位dp的套路板子……不需要讨论原来我很头疼的一些边界. 改天用这个板子重做一下原来的一些数位dp题目. http://blog.csdn.net/the_useless/a ...
- Data Structure Binary Tree: Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
http://www.geeksforgeeks.org/convert-an-arbitrary-binary-tree-to-a-tree-that-holds-children-sum-prop ...
- Data Structure Binary Tree: Check for Children Sum Property in a Binary Tree
http://www.geeksforgeeks.org/check-for-children-sum-property-in-a-binary-tree/ #include <iostream ...
- 唯一分解定理(以Minimun Sum LCM UVa 10791为例)
唯一分解定理是指任何正整数都可以分解为一些素数的幂之积,即任意正整数n=a1^p1*a2^p2*...*ai^pi:其中ai为任意素数,pi为任意整数. 题意是输入整数n,求至少2个整数,使得它们的最 ...
- Minimum Sum LCM UVA - 10791(分解质因子)
对于一个数n 设它有两个不是互质的因子a和b 即lcm(a,b) = n 且gcd为a和b的最大公约数 则n = a/gcd * b: 因为a/gcd 与 b 的最大公约数也是n 且 a/gcd ...
- UVa 1640 (计数) The Counting Problem
题意: 统计[a, b]或[b, a]中0~9这些数字各出现多少次. 分析: 这道题可以和UVa 11361比较来看. 同样是利用这样一个“模板”,进行区间的分块,加速运算. 因为这里没有前导0,所以 ...
随机推荐
- Java.基础 -------- 一个Java源文件中可以有很多类,但只能有一个类是public的
一个Java源文件中可以有很多类,但只能有一个类是public的 Java程序是从一个public类main函数开始执行的,只能有一个public是为了给类装载器提供方便,一个publi ...
- java中什么是继承笔记
继承 怎样实现继承:1,先提取共有的属性和方法,放到一个类里,这个叫父类.基类.超类 2.编写子类 修饰符 class 子类名 extends 父类名 好处:提高代码的复用性 子类怎么去 ...
- 素数筛法(Eratosthenes筛法)
介绍 Eratosthenes筛法,又名埃氏筛法,对于求1~n区间内的素数,时间复杂度为n log n,对于10^6^ 以内的数比较合适,再超出此范围的就不建议用该方法了. 筛法的思想特别简单: 对于 ...
- 【KakaJSON手册】02_JSON转Model_02_数据类型
由于JSON格式的能表达的数据类型是比较有限的,所以服务器返回的JSON数据有时无法自动转换成客户端想要的数据类型. 比如服务器返回的时间可能是个毫秒数1565480696,但客户端想要的是Date类 ...
- 云上RDS架构
概述 越来越多的企业选择上云,最基础的云服务就是IaaS(Infrastructure as a Service)服务,直观理解就是虚拟主机,用户不用再自建机房,自己购买服务器,而是直接向云厂商购买虚 ...
- gunicorn 基础配置使用
flask 自带的 web 服务器稳定性较差,只能用于测试.最近做的 web 项目,每次启动,需要敲一堆参数文件,今天学习了官方文档里关于配置的说明,记录一下. 创建一个 gunicorn.conf ...
- c# 读取 txt 文件中数据(int)
今天在学图的算法做测试是,需要读取文本文件中的点坐标,本来很简单的事情,折腾了半天,记录一下找到的一种简单粗暴的解决方法,以便以后查看. 第一种方法 : StringReader string lin ...
- Appium+python自动化(三十四)- 有图有真相,很美很精彩 - 屏幕截图和Android APP类型简介(超详解)
简介 在实际自动化项目运行过程中,很多时候App可以会出现各种异常,为了更好的定位问题,除了捕捉日志我们还需要对运行时的设备状态来进行截屏.从而达到一种“有图有真相”的效果. 截图方法 方法1 sav ...
- python方法的使用
1.函数定义 def 函数名(形参): 函数体 return 返回值 2.函数执行 ...
- Redis缓存,持久化,高可用
一,Redis作缓存服务器 本篇博客是接着上一篇博客未分享完的技术点. redis作为缓存服务器是众多企业中的选择之一,虽然该技术很成熟但也是存在一定的问题.就是缓存带来的缓存穿透,缓存击穿, ...