hdu3652 B-number(數位dp+dfs),hdu3652b-number

來源:互聯網
上載者:User

hdu3652 B-number(數位dp+dfs),hdu3652b-number
B-numberTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3376 Accepted Submission(s): 1891


Problem DescriptionA wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- string "13" and can be divided by 13. For example, 130 and 2613 are wqb-numbers, but 143 and 2639 are not. Your task is to calculate how many wqb-numbers from 1 to n for a given integer n.
InputProcess till EOF. In each line, there is one positive integer n(1 <= n <= 1000000000).
OutputPrint each answer in a single line.
Sample Input

131002001000

Sample Output
1122

Authorwqb0039
Source2010 Asia Regional Chengdu Site —— Online Contest 
題意:找出1~n有多少個數既含有13又能被13整除。分析:記憶化搜尋配合數位dp求解。
#include <iostream>#include <cstdio>#include <cstring>#include <stack>#include <queue>#include <map>#include <set>#include <vector>#include <cmath>#include <algorithm>using namespace std;const double eps = 1e-6;const double pi = acos(-1.0);const int INF = 0x3f3f3f3f;const int MOD = 1000000007;#define ll long long#define CL(a) memset(a,0,sizeof(a))int dp[15][15][3],s[15];//dp[i][j][k],i表示位元,j表示餘數,k表示末尾是1、末尾不是1、含有13.int dfs(int pos, int mod, int have, int lim)//前三個數對應數組dp,lim表示上限{    int num,ans,mod_x,have_x;    if (pos <= 0)        return mod==0&&have==2;    if (!lim && dp[pos][mod][have]!=-1) //沒有上限且被訪問過        return dp[pos][mod][have];    ans = 0;    num = lim?s[pos]:9;//如果有上限,只能取到當前位元,如果沒上限,可取到9                        //假設該位是2,下一位是3,如果現在算到該位為1,那麼下一位是能取到9的,如果該位為2,下一位只能取到3    for (int i=0; i<=num; i++)    {        mod_x = (mod*10+i)%13;//該位的每種情況對13模數        have_x = have;        if (have==0 && i==1)//末尾加1            have_x=1;        if (have==1 && i!=1)//末尾已經為1了            have_x=0;        if (have==1 && i==3)//末尾是1,現在加3            have_x=2;        ans+=dfs(pos-1, mod_x, have_x, lim&&i==num);//如果i==num,下一位能取的最大數就為s[pos-1],i!=num,下一位能取到9    }    if (!lim)        dp[pos][mod][have] = ans;    return ans;}int main (){    int n,t;    while (scanf ("%d",&n)==1)    {        CL(s);        memset(dp, -1, sizeof(dp));        t = 0;        while (n)        {            s[++t]=n%10;            n/=10;        }        cout<<dfs(t, 0, 0, 1)<<endl;    }    return 0;}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.