首页 > 技术文章 > URAL1002——DP——K-based Numbers. Version 2(未AC)

zero-begin 2015-05-08 16:41 原文

Description

Let’s consider K-based numbers, containing exactly N digits. We define a number to be valid if its K-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 N digits.
You may assume that 2 ≤ K ≤ 10; N ≥ 2; N + K ≤ 1800.

Input

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

Output

The result in decimal notation.

Sample Input

inputoutput
2
10
90
 大意:增大了数据,范围,就是求1到n位数用k进制形式的排除两个零在一起的一共多少情况
这题目有毒orz。。。。。超内存怎么搞。。。不过学习了bign(重载),妈妈再也不用担心我不会高精度了~~~
MLE代码。。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
using namespace std;
const int maxn = 2000;
struct bign
{
    int len ,s[maxn];
    bign(){
    memset(s,0,sizeof(s));
    len = 1;
    }
    bign operator = (const char *num){
        len = strlen(num);
        for(int i = 0; i < len ;i++)
            s[i] = num[len-i-1] - '0';
            return *this;
    }
    bign operator = (int num){
        char s[maxn];
        sprintf(s,"%d", num);
        *this = s;
        return *this;
    }
    bign(int num) { *this = num;}
    bign(const char *num){*this = num;}
    string str()const{
        string res ="";
        for(int i = 0 ; i < len ;i++)
            res = (char)(s[i] + '0') + res;
        if(res == "") res = "0";
        return res;
    }
    bign operator + (const bign &b)const{
        bign c;
        c.len = 0;
        for(int i = 0 ,g = 0 ; g||i < max(len,b.len);i++){
            int x = g;
            if( i < len) x += s[i];
            if(i < b.len) x += b.s[i];
            c.s[c.len++] = x%10;
            g = x/10;
        }
        return c;
    }
};
istream &operator >>(istream &in,bign &x){
    string s;
    in >> s;
    x = s.c_str();
    return in;
}
ostream &operator << (ostream &out, const bign &x){
    out << x.str();
    return out;
}
bign dp[1900][11];
int n,k;
int main()
{
    scanf("%d%d",&n,&k);
    for(int i = 1; i < k ;i++)
        dp[1][i] = 1;
        for(int i = 2; i <= n ;i++){
            for(int j = 0 ; j < k ; j++){
                for(int l = 0 ; l < k;l++){
                    if(!j && !l) continue;
                    dp[i][l] = dp[i][l] + dp[i-1][j];
                }
            }
        }
        bign ans = 0;
        for(int i = 0 ; i < k ;i++)
            ans = ans + dp[n][i];
        cout<<ans<<endl;
     return 0;
}

  

推荐阅读