UVALive 7785 m-ary Partitions(动态规划)

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
题意
输入m和n,找一些数,这些数都是mx(x>=0),计算有多少种组合方式,每种组合方式的和均为n。每个符合条件的数都可以用无数次。
题解
刚开始想的是用母函数做,但是会T,然后有队友用的dp,我最后也改成了动态规划(一直对动态规划不感冒QAQ)。

#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <sstream>
#include <cstdio>
#include <vector>
#include <string>
#include <cmath>
#include <stack>
#include <queue>
#include <map>
#include <set>
#define MAX 0x3f3f3f3f
#define fori(n) for(int i=0;i<n;i++)
#define fori(a,b) for(int i=a;i<b;i++)
#define forj(n) for(int j=0;j<n;j++)
#define forj(a,b) for(int j=a;j<b;j++)
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
typedef long long LL;
const double PI = acos(-1);
LL dp[11111];
int main()
{
    LL m,n,cnt,t;
    cin >> t;
    while(t--)
    {
        mem(dp);
        cin >> cnt >> m >> n;
        cout << cnt << " ";
        dp[0]=1;
        for(int i=1;i<=n;i*=m)
        {
            forj(i,n+1){
                dp[j]+=dp[j-i];    //因为组成的数必须都是m的x次方,所以这里减去 i(i为m的x次方)来化成它的子钱数
            }
        }
        cout << dp[n]<<endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值