Investigation LightOJ - 1068

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

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

Output

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.

Sample Input

3

1 20 1

1 20 2

1 1000 4

Sample Output

Case 1: 20

Case 2: 5

Case 3: 64

题解:dp[ pos ][ res1 ][ res2 ]表示当前位置,数位之和模K的余数,这些数位组成的数模K的余数。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 const int INF=0x3f3f3f3f;
 8
 9 int L,R,K;
10 int t[10],num[10],dp[10][90][90];
11
12 void Inite(){
13     t[0]=1;
14     for(int i=1;i<10;i++) t[i]=t[i-1]*10;
15 }
16
17 int DFS(int pos,int res1,int res2,bool F){
18     if(pos==-1){
19         if(res1==0&&res2==0) return 1;
20         else return 0;
21     }
22
23     if(!F&&dp[pos][res1][res2]!=INF) return dp[pos][res1][res2];
24     int maxv=F?num[pos]:9;
25
26     int ans=0;
27     for(int i=0;i<=maxv;i++){
28         int x=(res1+i*t[pos])%K;
29         int y=(res2+i)%K;
30         ans=ans+DFS(pos-1,x,y,(F&&i==maxv)?true:false);
31     }
32
33     if(!F) dp[pos][res1][res2]=ans;
34     return ans;
35 }
36
37 int Solve(int temp){
38     if(temp==0) return 1;
39     int cnt=0;
40     while(temp){
41         num[cnt++]=temp%10;
42         temp/=10;
43     }
44     return DFS(cnt-1,0,0,true);
45 }
46
47 int main()
48 {   Inite();
49
50     int kase;
51     cin>>kase;
52     for(int k=1;k<=kase;k++){
53         cin>>L>>R>>K;
54         memset(dp,0x3f,sizeof(dp));
55
56         int ans;
57         if(K>82) ans=0;
58         else ans=Solve(R)-Solve(L-1);
59         printf("Case %d: %d\n",k,ans);
60     }
61     return 0;
62 } 
时间: 2024-08-07 00:03:57

Investigation LightOJ - 1068的相关文章

数位dp小记

转载自:http://blog.csdn.net/guognib/article/details/25472879 参考: http://www.cnblogs.com/jffifa/archive/2012/08/17/2644847.html kuangbin :http://www.cnblogs.com/kuangbin/category/476047.html http://blog.csdn.net/cmonkey_cfj/article/details/7798809 http:/

LightOJ 1030 Discovering Gold【概率】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题意:基础概率题. 代码: #include <stdio.h> #include <string.h> #include <vector> #include <string> #include <algorithm> #include <iostream> #include <iterator>

pat 1068 动态规划/Fina More Conis

1068. Find More Coins (30) Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special re

LightOJ - 1370 Bi-shoe and Phi-shoe

题目链接:http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1370 题目大意:给N个数a[i], N <= 1e6,问使 Φ(x) >= a[i] 成立的最小x的所有x的和是多少. 解题思路:我们知道的是对于素数 m 来说,phi[m] = m - 1.另一方面,对于一个合数 m 来说, phi[m] < phi[x] , x > m && x 是素数. 因此,我们可以认为,每

51nod 1068 Bash游戏 V3 博弈

1068 Bash游戏 V3 题目来源: Ural 1180 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题  收藏  关注 有一堆石子共有N个.A B两个人轮流拿,A先拿.每次拿的数量只能是2的正整数次幂,比如(1,2,4,8,16....),拿到最后1颗石子的人获胜.假设A B都非常聪明,拿石子的过程中不会出现失误.给出N,问最后谁能赢得比赛. 例如N = 3.A只能拿1颗或2颗,所以B可以拿到最后1颗石子.(输入的N可能为大数) Input 第1行:一个

lightoj 1057 - Collecting Gold(状压dp)

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1057 题解:看似有点下记忆话搜索但是由于他是能走8个方向的也就是说两点的距离其实就是最大的x轴或y轴的差.然后只有15个藏金点状压一下加dfs就行了. #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #define inf 0X3f3f3f

Lightoj 1088 - Points in Segments 【二分】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1088 题意: 有一维的n个点和q条线段.询问每条线段上的点有多少个: 思路:寻找这些点中对于每条线段的上下界即可. 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include

poj 1068 Parencodings

Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 22797   Accepted: 13363 Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways: q By an integer sequence P = p1 p2...pn

Lightoj 1090 - Trailing Zeroes (II)

题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1090 题目大意: 给出n,r,p,q四个数字1<=n,r,p,q<=1000000,求出的末尾有几个0? 解题思路: 是不是一下子懵了,数字好大,复杂度好高,精度怎么办···············,就问你怕不怕? 因为你是Acmer,这都不应该是问题.因为10的因子只有2和5,所以可以打表保存从1到当前数字相乘的积中分别含有2,5的个数.然后算出中分别含有2,5的个数,