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

#include <bits/stdc++.h>
typedef long long int LL;
using namespace std;
const int N = 100010;

int main()
{
    int T,z;
    cin>>T;
    while(T--)
    {
        LL i,j,n,m;
        vector<LL>dp(N, 1);
        cin>>z>>m>>n;
        for(i=m;i<=n;i*=m)
        {
            for(j=i;j<=n;j++)
            {
                dp[j]=dp[j-i]+dp[j];
            }
        }
        cout<<z<<' '<<dp[n]<<endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值