Description
A partition of an integer n is a set of positive integers which sum to n, typically written in descending
order. For example:
10 = 4+3+2+1
A partition is m-ary if each term in the partition is a power of m. For example, the 3-ary partitions
of 9 are:
9
3+3+3
3+3+1+1+1
3+1+1+1+1+1+1
1+1+1+1+1+1+1+1+1
Write a program to find the number of m-ary partitions of an integer n.Input
The first line of input contains a single decimal integer P, (1 ≤ P ≤ 1000), which is the number of data
sets that follow. Each data set should be processed identically and independently.
Each data set consists of a single line of input. The line contains the data set number, K, followed by
the base of powers, m, (3 ≤ m ≤ 100), followed by a space, followed by the integer, n, (3 ≤ n ≤ 10000),
for which the number of m-ary partitions is to be found.Output
For each data set there is one line of output. The output line contains the data set number, K, a space,
and the number of m-ary partitions of n. The result should fit in a 32-bit unsigned integer.Sample Input
5
1 3 9
2 3 47
3 5 123
4 7 4321
5 97 9999Sample Output
1 5
2 63
3 75
4 144236
5 111
题解:
dp题,类似找零钱题型,每个钱数可由它的子钱数推得。
代码如下:
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<set>
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define swap(a,b) (a=a+b,b=a-b,a=a-b)
#define maxn 27
#define N 100000000
#define INF 0x3f3f3f3f
#define mod 1000000009
#define e 2.718281828459045
#define eps 1.0e18
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define read(x) scanf("%d",&x)
#define put(x) printf("%d\n",x)
#define memset(x,y) memset(x,y,sizeof(x))
#define Debug(x) cout<<x<<" "<<endl
#define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r
#define ll long long
//std::ios::sync_with_stdio(false);
//cin.tie(NULL);
//const int maxn=;
using namespace std;
ll dp[11111];
ll a[111];
int main()
{
ll t;
cin>>t;
while(t--)
{
ll n,m,k;
cin>>m>>k>>n;
cout<<m<<" ";
ll s=1,l=0;
memset(a,0);
memset(dp,0);
while(s<=n)
{
a[l++]=s;
s*=k;
}
dp[0]=1;
for(ll i=0; i<l; i++)
for(ll j=a[i]; j<=n; j++)
dp[j]+=dp[j-a[i]];
cout<<dp[n]<<endl;
}
return 0;
}