欢迎访问https://blog.youkuaiyun.com/lxt_Lucia~~
宇宙第一小仙女\(^o^)/~~萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗~~
一、问题:
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 9999
Sample Output
1 5
2 63
3 75
4 144236
5 111
二、题意:
第一行数字 T 代表测试样例个数。
接下来的 T 行每行第一个数 cnt 表示序号,第二个数m,第三个数n。
求:使不超过n的m的幂(每种幂有无穷个)的和等于n,有多少种组合方法。注意:幂的种类和各种幂的个数均相同的视为同一种,如3+1+1+1和1+1+1+3为同一种。
三、思路:
对于每一个数n,都可以拆成两个数的和,那么拼出来n的方法数就等于 (拼出n-1的方法+拼出1的方法) + (拼出n-2的方法+拼出2的方法) + ..... 下面代码中的 dp数组记录的就是拼出n的方法数。
四、代码:
#include <cstdio>
#include <cstring>
#define read(x) scanf("%d",&x)
#define mem(a,b) memset(a,0,sizeof(a))
#define fori(a,b) for(int i=a;i<=b;i++)
#define forj(a,b) for(int j=a;j<=b;j++)
int main()
{
int T;
read(T);
while(T--)
{
int dp[11000], a[110];
int cnt, m, n, z=1, k=-1;
scanf("%d %d %d", &cnt, &m, &n);
mem(a, 0);
mem(dp, 0);
dp[0] = 1;
while(z <= n)
{
a[++k] = z;
z *= m;
}
fori(0, k)
forj(a[i], n)
dp[j] += dp[j-a[i]];
printf("%d %d\n", cnt, dp[n]);
}
return 0;
}
欢迎访问https://blog.youkuaiyun.com/lxt_Lucia~~
宇宙第一小仙女\(^o^)/~~萌量爆表求带飞=≡Σ((( つ^o^)つ~ dalao们点个关注呗~~