zoukankan      html  css  js  c++  java
  • Timus 1009. K-based Numbers

    1009. K-based Numbers

    Time limit: 0.5 second
    Memory limit: 64 MB
    Let’s consider K-based numbers, containing exactly N digits. We define a number to be valid if itsK-based notation doesn’t contain two successive zeros. For example:
    • 1010230 is a valid 7-digit number;
    • 1000198 is not a valid number;
    • 0001235 is not a 7-digit number, it is a 4-digit number.
    Given two numbers N and K, you are to calculate an amount of valid K based numbers, containing Ndigits.
    You may assume that 2 ≤ K ≤ 10; N ≥ 2; N + K ≤ 18.

    Input

    The numbers N and K in decimal notation separated by the line break.

    Output

    The result in decimal notation.

    Sample

    inputoutput
    2
    10
    
    90

    好简单的dp

    dp[i][j]表示 第i位为j的合法数量

    那么dp[i][j]=sum(dp[i-1][t])

    /* ***********************************************
    Author        :guanjun
    Created Time  :2016/9/6 14:29:49
    File Name     :timus1009.cpp
    ************************************************ */
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <stdio.h>
    #include <algorithm>
    #include <vector>
    #include <queue>
    #include <set>
    #include <map>
    #include <string>
    #include <math.h>
    #include <stdlib.h>
    #include <iomanip>
    #include <list>
    #include <deque>
    #include <stack>
    #define ull unsigned long long
    #define ll long long
    #define mod 90001
    #define INF 0x3f3f3f3f
    #define maxn 10010
    #define cle(a) memset(a,0,sizeof(a))
    const ull inf = 1LL << 61;
    const double eps=1e-5;
    using namespace std;
    priority_queue<int,vector<int>,greater<int> >pq;
    struct Node{
        int x,y;
    };
    struct cmp{
        bool operator()(Node a,Node b){
            if(a.x==b.x) return a.y> b.y;
            return a.x>b.x;
        }
    };
    
    bool cmp(int a,int b){
        return a>b;
    }
    int n,k;
    int dp[110][110];
    int main()
    {
        #ifndef ONLINE_JUDGE
        //freopen("in.txt","r",stdin);
        #endif
        //freopen("out.txt","w",stdout);
        while(~scanf("%i %i",&n,&k)){
    
            for(int i=1;i<k;i++)dp[1][i]=1;
            dp[1][0]=0;
            int ans=0;
    
            for(int i=2;i<=n;i++){
                for(int j=0;j<k;j++){
                    if(j==0)for(int t=1;t<k;t++)dp[i][j]+=dp[i-1][t];
                    else for(int t=0;t<k;t++)dp[i][j]+=dp[i-1][t];
                    //cout<<dp[2][j]<<endl;
                }
            }
            for(int i=0;i<k;i++)ans+=dp[n][i];
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    hdu find the most comfortable road(贪心+并查集)
    hdu 迷瘴(贪心)
    Excel中指定的sheet名称或索引读取数据到DataTable
    SQLServer 批量插入数据的两种方法
    javascript基础教程3
    Enterprise Library 4.1学习笔记 配置应用程序块(c/s和b/s均适用)
    关于同一用户不能同时在两台电脑上登录问题的解决方案
    获取当天为一年中的第几周
    javascript基础教程4
    Excel导出方法选择(转自gwf25sz)
  • 原文地址:https://www.cnblogs.com/pk28/p/5845857.html
Copyright © 2011-2022 走看看