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 ;
}

最新文章

  1. springmvc学习笔记--mybatis--使用插件自动生成实体和mapper
  2. SqlSever基础 union all 联合查询,简单的组合 两个查询结果拼在一起
  3. ZOJ 1586 QS Network (最小生成树)
  4. Linq常用
  5. Word 2010发布博客文章
  6. Selenium关于Page Objects
  7. js实现点击copy,可兼容
  8. 如何修改script.bin/script.fex
  9. Elastic Stack-Elasticsearch使用介绍(三)
  10. 我和python的初相识
  11. springboot+mybatis+dubbo+aop日志第二篇
  12. A context-aware personalized travel recommendation system based on geotagged social media data mining
  13. Docker Compose 配置文件详解
  14. Django的session学习
  15. 洛咕 P3702 [SDOI2017]序列计数
  16. Linux驱动中completion接口浅析(wait_for_complete例子,很好)
  17. 安装 git,并创建版本库 记录一下
  18. React组件的使用
  19. Map Reduce Application(Top 10 IDs base on their value)
  20. Redisson教程

热门文章

  1. hdoj 4715 Difference Between Primes 素数筛选+二分查找
  2. 章节十五、5-记录日志---Log4j
  3. bit、byte、kb、mb、g的区别
  4. Android 属性动画实战
  5. 学好C/C++编程,走遍天下都不怕
  6. javascript 异步请求封装成同步请求
  7. 精准测试与开源工具Jacoco的覆盖率能力大PK
  8. Spring项目集成ShiroFilter简单实现权限管理
  9. 第一次Git使用以及码云(Gitee)
  10. 《统计学习方法》极简笔记P5:决策树公式推导