跳过正文
  1. Posts/

hdu 3652 B-number (带整除的数位dp )

·659 字·2 分钟

题目链接 题意:给出n,问[1,n]中,满足包含“13”且这个数(不是各位的和)能被13整除的数的个数。 思路:依然是数位dp..不过有一个小tip。。

由于包含13的情况非常难考虑(包含一个“13”,两个“13”…..)

所以要从反面考虑,即不包含13的情况。

但是由于还有另一个条件。

做法是把能被13整除的数考虑成全集U,然后在U中做分划,一部分是含13的,另一部分是不含13的。

这样我们要求两个答案,一个是能被13整除的,另一个是能被13整除并且不含13的,相减即为题目所求。

代码实现
  1/* ***********************************************
  2Author :111qqz
  3Created Time :2016年03月16日 星期三 09时27分49秒
  4File Name :code/hdu/3652.cpp
  5************************************************ */
  6
  7#include <cstdio>
  8#include <cstring>
  9#include <iostream>
 10#include <algorithm>
 11#include <vector>
 12#include <queue>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <ctime>
 19#define fst first
 20#define sec second
 21#define lson l,m,rt<<1
 22#define rson m+1,r,rt<<1|1
 23#define ms(a,x) memset(a,x,sizeof(a))
 24typedef long long LL;
 25#define pi pair < int ,int >
 26#define MP make_pair
 27
 28using namespace std;
 29const double eps = 1E-8;
 30const int dx4[4]={1,0,0,-1};
 31const int dy4[4]={0,-1,1,0};
 32const int inf = 0x3f3f3f3f;
 33LL n;
 34LL dp[20][15];
 35LL dp2[20][2][15];
 36int digit[20];
 37
 38
 39LL dfs (int pos,int sum,bool limit)
 40{
 41    if (pos==0) return sum==0;
 42
 43    if (!limit&&dp[pos][sum]!=-1) return dp[pos][sum];
 44
 45    int mx = limit ? digit[pos]:9;
 46
 47    LL res = 0LL ;
 48    for ( int i = 0 ; i  <=  mx ; i++)
 49    {
 50	res+=dfs(pos-1,(sum*10+i),limit&&i==mx);
 51    }
 52
 53    if(!limit) dp[pos][sum] = res;
 54    return res;
 55}
 56LL dfs2(int pos,bool preis1,int sum,bool limit)
 57{
 58    if (pos==0) return sum==0;
 59
 60    if (!limit&&dp2[pos][preis1][sum]!=-1) return dp2[pos][preis1][sum];
 61
 62    int mx = limit? digit[pos]:9;
 63    LL res = 0 ;
 64    for ( int i = 0 ; i <=  mx ; i++)
 65    {
 66	if (preis1&&i==3) continue;
 67	res+=dfs2(pos-1,i==1,(sum*10+i),limit&&i==mx);
 68    }
 69
 70    if (!limit) dp2[pos][preis1][sum] = res;
 71    return res;
 72}
 73
 74LL solve ( LL n)
 75{
 76    ms(digit,0);
 77
 78    int len =  0;
 79    while (n)
 80    {
 81	digit[++len] = n;
 82	n/= 10;
 83    }
 84    return dfs(len,0,true)-dfs2(len,false,0,true); //在能被13整除的数里划分,减去不含“13”的,就是含“13”的。
 85						  //因为含“13”的情况太复杂了。。。
 86}
 87int main()
 88{
 89	#ifndef  ONLINE_JUDGE
 90	freopen("code/in.txt","r",stdin);
 91  #endif
 92
 93	ms(dp,-1); //dp数组子啊外面清空就好。。。。因为dp数组是所有数据公用的啊。。。。
 94	ms(dp2,-1);
 95	while (~scanf("%lld",&n))
 96	{
 97	    LL ans = solve (n);
 98	    printf("%lld\n",ans);
 99	}
100
101  #ifndef ONLINE_JUDGE
102  fclose(stdin);
103  #endif
104    return 0;
105}

相关文章

hdu 4722 good numbers (带整除的数位dp)

·478 字·1 分钟
题目链接 题意:求一个区间内所有位数字之和能被10整除的数的个数。 思路:数位dp,dfs要一个参数记录从最高位到现在的pos位置的数字之和的结果。 代码实现 1 dp[i][j] 表示长度为i,和为j的方案数。 2 记得开long long ,然而我开了那么多long long 忘了dp 的long long 结果wa到死。。果然大早上不清醒吗== 3 4 5 6/* *********************************************** 7Author :111qqz 8Created Time :2016年03月16日 星期三 08时10分19秒 9File Name :code/hdu/4722.cpp 10************************************************ */ 11 12#include <cstdio> 13#include <cstring> 14#include <iostream> 15#include <algorithm> 16#include <vector> 17#include <queue> 18#include <set> 19#include <map> 20#include <string> 21#include <cmath> 22#include <cstdlib> 23#include <ctime> 24#define fst first 25#define sec second 26#define lson l,m,rt<<1 27#define rson m+1,r,rt<<1|1 28#define ms(a,x) memset(a,x,sizeof(a)) 29typedef long long LL; 30#define pi pair < int ,int > 31#define MP make_pair 32 33using namespace std; 34const double eps = 1E-8; 35const int dx4[4]={1,0,0,-1}; 36const int dy4[4]={0,-1,1,0}; 37const int inf = 0x3f3f3f3f; 38LL l,r; 39int digit[30]; 40LL dp[30][15]; //dp 数组忘记开long long ,wa到死。。。。。。。。。日了哈士奇。 41LL dfs ( int pos,int sum,bool limit) 42{ 43 if (pos==0) 44 { 45 if (sum==0) return 1; 46 else return 0; 47 } 48 if (!limit&&dp[pos][sum]!=-1) return dp[pos][sum]; 49 50 int mx = limit?digit[pos]:9; 51 52 LL res = 0 ; 53 for ( int i = 0 ; i <= mx; i ++) 54 { 55 res+=dfs(pos-1,(sum+i),limit&&i==mx); 56 } 57 58 if (!limit) dp[pos][sum] = res; 59 60 return res; 61 62} 63LL solve ( LL n) 64{ 65// if (n==0) return 1; 66 // if (n<=9) return 0; 67 if (n<0) return 0; 68 ms(digit,0); 69 int len = 0 ; 70 while (n) 71 { 72 digit[++len] = n % 10; 73 n /= 10; 74 } 75 76 return dfs(len,0,true); 77} 78int main() 79{ 80 #ifndef ONLINE_JUDGE 81 freopen("code/in.txt","r",stdin); 82 #endif 83// ios::sync_with_stdio(false); 84 int T; 85 cin>>T; 86 ms(dp,-1); 87 int cas = 0 ; 88 while (T--) 89 { 90 scanf("%lld %lld",&l,&r); 91 LL ans = solve (r)-solve(l-1); 92 93 printf("Case #%d: %lld\n",++cas,ans); 94 } 95 96 #ifndef ONLINE_JUDGE 97 fclose(stdin); 98 #endif 99 return 0; 100}

bzoj 1026 windy数(数位dp入门题)

·757 字·2 分钟
题目链接 题意:不含前导零且相邻两个数字之差至少为2的正整数被称为windy数。 windy想知道,在A和B之间,包括A和B,总共有多少个windy数? 思路:数位dp 这道题的特点是前面不允许前导0,也就是说,如果第i位前面全是0的话,这个数就变成了i位数,i就变成了最高位,而最高位没有前面的数(**如果这里不考虑不允许前导0这个因素而把前面的一个数认为成是0就错了) **最高位的数可以直接取。 还有记忆化调用以及存储的时候也要注意…只有当位数相同的时候转移才有意义。 具体的方法是dfs中多了一个prehasnonzero的bool变量,就是字面意思,判断当前位置前面的位置是够存在一个非0的值。

hdu 3555 Bomb (数位dp入门题)

·401 字·1 分钟
题目链接 题意:问从1到n的所有数中,有多少个数含有数字串“49” 思路:和上一道不要62很像,但是由于是要统计有49的,但是有49的情况实在太多了,正难则反,用减法定理反过来考虑,先统计出不含49的数的个数,这样就和不要62一样了,然后再用总数减。