U
来源:互联网 发布:淘宝刷单工作室怎么开 编辑:程序博客网 时间:2024/04/29 20:57
题目链接:https://cn.vjudge.net/contest/194559#problem/U
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 starts with an integer T (≤ 200), denoting the number of test cases.
Each case contains three positive integers A, B and K (1 ≤ A ≤ B < 231 and 0 < K < 10000).
For each case, output the case number and the number of integers in the range [A, B] which are divisible by K and the sum of its digits is also divisible by K.
3
1 20 1
1 20 2
1 1000 4
Case 1: 20
Case 2: 5
Case 3: 64
大意:每个位的总和能整除k,并且这个数能整除k的个数
分析+代码:
/*
此题属于正常的题。
但是容易超时的地方在于数据量大就想到用longlong,实际上可以清楚地知道,如果
2的31次方小于4000000000,十位数数,假设十位数都为9,那么最大为90。
也就是说k大于90的话,就没意思了,怎样都不会被整除的
*/
#include<iostream>
#include<string.h>
using namespace std;
typedef long long ll;
int dig[20];
int dp[20][100][100];
int dfs(int pos,int sum,int dangqian,int k,int limit)
{
if(pos==0)
{
if(sum%k==0&&dangqian%k==0)
return 1;
else
return 0;
}
if(dp[pos][sum][dangqian]!=-1&&!limit)
return dp[pos][sum][dangqian];
int up=limit?dig[pos]:9;
ll ans=0;
for(int i=0;i<=up;i++)
{
ans+=dfs(pos-1,(sum*10+i)%k,i+dangqian,k,limit&&i==up);
}
if(!limit)
dp[pos][sum][dangqian]=ans;
return ans;
}
int solve(int x,int k)
{
if(k>=90)
return 0;
int id=0;
while(x)
{
dig[++id]=x%10;
x/=10;
}
return dfs(id,0,0,k,1);
}
int main()
{
int T;
cin>>T;
ll a,b;
int k;
int p=1;
// memset(dp,-1,sizeof(dp));
while(T--)
{
memset(dp,-1,sizeof(dp));
cin>>a>>b>>k;
cout<<"Case "<<p<<":"<<" "<<solve(b,k)-solve(a-1,k)<<endl;
p++;
}
return 0;
}
- u
- u
- u
- u
- U
- U
- U
- U
- U
- U
- U
- U
- U
- U
- U
- U
- U
- U
- hyper-v虚拟机内为centos6的虚拟机动态扩容硬盘
- laravel post TokenMismatchException in VerifyCsrfToken.php line 53 问题解决方法
- kmp板子 洛谷P3375
- java 字符串截取的三种方法(推荐)
- HDU-1532 Drainage Ditches(最大流基础)
- U
- 图片上传各参数含义
- python+uiautomator: IOError: RPC server not started!
- 习题5 5.1
- Java反射机制
- Android 字符串使用StringBuffer更容易处理
- Centos7.2 saltstack安装与使用
- Swing工具包应用-------table表格及相关样式
- 习题6.2(3)