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的更多相关文章

  1. UVa 11361 - Investigating Div-Sum Property

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

  2. 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 ...

  3. UVa 11361 (计数 递推) Investigating Div-Sum Property

    题意: 统计[a, b]中有多少个数字满足:自身是k的倍数,而且各个数字之和也是k的倍数. 分析: 详细分析见<训练之南>吧,=_=|| 书上提出了一个模板的概念,有了模板我们就可以分块计 ...

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

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

  5. 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 ...

  6. 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 ...

  7. 唯一分解定理(以Minimun Sum LCM UVa 10791为例)

    唯一分解定理是指任何正整数都可以分解为一些素数的幂之积,即任意正整数n=a1^p1*a2^p2*...*ai^pi:其中ai为任意素数,pi为任意整数. 题意是输入整数n,求至少2个整数,使得它们的最 ...

  8. 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 ...

  9. UVa 1640 (计数) The Counting Problem

    题意: 统计[a, b]或[b, a]中0~9这些数字各出现多少次. 分析: 这道题可以和UVa 11361比较来看. 同样是利用这样一个“模板”,进行区间的分块,加速运算. 因为这里没有前导0,所以 ...

随机推荐

  1. 微服务与网关技术(SIA-GateWay)

    一.背景 软件架构,总是在不断的演进中... 把时间退回到二十年之前,当时企业级领域研发主要推崇的还是C/S模式,PB.Delphi这样的开发软件是企业应用开发的主流.随着时间的推移,基于浏览器的B/ ...

  2. bytedance专题

    一 挑战字符串 1 无重复字符的最长子串(见leetcode bug free) 2 最长公共前缀(见leetcode bug free) 3 字符串的排列 给定两个字符串 s1 和 s2,写一个函数 ...

  3. Cocos Creator经典游戏制作之:信使(The Messenger)

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  4. git码云的使用基础(为了以后更好的协同操作)

    git手册 安装教程 windows 不需要什么操作点点点就好 设置一个文件夹当成本地仓库 第一次上传 git init# 创建一个本地的仓库 git add. 当前文件夹下所有内容# 添加到暂存区 ...

  5. Nacos(一):Nacos介绍

    前言 6月份阿里开源的Nacos出了1.0.1版本,从去年7月份第一个release版本到现在一直在默默关注 官方的版本规划为:Nacos从0.8.0开始支持生产可用,1.0版本可大规模生产可用,2. ...

  6. python的魔术方法大全

    在Python中,所有以“__”双下划线包起来的方法,都统称为“Magic Method”(魔术方法),例如类的初始化方法 __init__ ,Python中所有的魔术方法均在官方文档中有相应描述,这 ...

  7. 根据屏幕分辨率判断当前手机型号(swift4.1)

    import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoa ...

  8. 【在 Nervos CKB 上做开发】Nervos CKB 脚本编程简介[1]:验证模型

    CKB 脚本编程简介[1]: 验证模型 本文作者:Xuejie 原文链接:Introduction to CKB Script Programming 1: Validation Model 本文译者 ...

  9. Hadoop RPC机制详解

    网络通信模块是分布式系统中最底层的模块,他直接支撑了上层分布式环境下复杂的进程间通信逻辑,是所有分布式系统的基础.远程过程调用(RPC)是一种常用的分布式网络通信协议,他允许运行于一台计算机的程序调用 ...

  10. 数组的方法 forEach filter map slice splice

    目前一些数组的实用的方法 1 arr.splice(i,n) 删除从i(索引值)开始之后的那个元素.返回值是删除的元素,改变原数组: 参数: i 索引值      n 个数 let arr = [1, ...