您的位置:首页 > 其它

URAL1009 K-based Numbers,简单DP

2011-11-22 21:01 323 查看
很简单的DP题,对于K进制,如果运算到第i位,则第i位只有k-1可以选择,然后分i-1位是0还是非零讨论。

/*******************************************************************************
# Author : Neo Fung
# Email : neosfung@gmail.com
# Last modified: 2011-11-22 21:02
# Filename: URAL1009 K-based Numbers.cpp
# Description :
******************************************************************************/
// #include "stdafx.h"
// #define DEBUG

#include <fstream>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <memory.h>
#include <limits.h>
#include <algorithm>
#include <math.h>
using namespace std;

int main(void)
{
#ifdef DEBUG
freopen("C:/Users/neo/Desktop/stdin.txt","r",stdin);
freopen("C:/Users/neo/Desktop/stdout.txt","w",stdout);
#endif

__int64 dp[100];
int n,k;

while(~scanf("%d%d",&n,&k))
{
dp[0]=k-1;
dp[1]=(k-1)*k;

for(int i=2;i<n;++i)
dp[i]=(k-1)*(dp[i-1]+dp[i-2]);

printf("%I64d\n",dp[n-1]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: