Timus 1009. K-based Numbers
1009. K-based Numbers
Time limit: 0.5 second
Memory limit: 64 MB
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
input | output |
---|---|
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; }
原文地址:http://www.cnblogs.com/pk28/
与有肝胆人共事,从无字句处读书。
欢迎关注公众号:
欢迎关注公众号: