[ARC107D] Number of Multisets

· · 题解

题意简述:

给定 n 和 k,求 n 个可表示为 \frac{1}{2^i} (i \in \mathbb{N}) 的数和为 k 的方案数,结果对 998244353 取模。

分析:

本题思维难度不高,考虑 dp,dp_{i,j} 表示选 i 个满足题目要求的数使它们的和等于 j,同时因为满足题目要求的数都 \le1 所以对于 i,j 一定满足 0 \le j \le i,否则方案不存在。

因为 \frac{1}{2^0} 为整数,所以分成两个子问题考虑。

考虑整数的情况,那么就转换为了 dp_{i-1,j-1}。

考虑分数的情况,不妨再将其转换为 1 的情况,因为满足题目要求的数的分母都为 2 的倍数,所以将所有数乘 2,即转换为 dp_{i,2 \times j}。

综上,得出转移方程:

\left\{ \begin{aligned} &0 , j = 0\\ &0 , i < j\\ &dp_{i-1,j-1} + dp_{i,2 \times j} , \text{otherwise}\\ \end{aligned} \right.

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<queue>
#include<cstdlib>
#include<iomanip>
#include<utility>
#define endl '\n'
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pai;
const double eps = 1e-10;
const int base = 13331;
const ll mod = 998244353;
ll maxn = -2147483647-1,minn = 0x7f7f7f7f;
ll n,k,dp[3005][3005];
ll solve(ll i,ll j){
    if(!j||i<j){
        return 0;
    }
    if(dp[i][j]){
        return dp[i][j];
    }
    else{
        return dp[i][j] = (solve(i-1,j-1)+solve(i,2*j))%mod;
    }
}
int main(){
    //freopen("filename.in","r",stdin);
    //freopen("filename.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n>>k;
    for(int i=1;i<=n;++i){
        dp[i][i] = 1;
    }
    cout<<solve(n,k)%mod<<endl;
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

总结:

这种类型的题目比较典,很容易想到 dp,分数转为整数比较巧妙。