日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HDU6092——Rikka with Subset 【dp】

發布時間:2025/3/8 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HDU6092——Rikka with Subset 【dp】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目網址:

https://vjudge.net/problem/HDU-6092

As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:?

Yuta has?nn?positive?A1?AnA1?An?and their sum is?mm. Then for each subset?SS?of?AA, Yuta calculates the sum of?SS.?

Now, Yuta has got?2n2n?numbers between?[0,m][0,m]. For each?i∈[0,m]i∈[0,m], he counts the number of?iis he got as?BiBi.?

Yuta shows Rikka the array?BiBi?and he wants Rikka to restore?A1?AnA1?An.?

It is too difficult for Rikka. Can you help her???

Input

The first line contains a number?t(1≤t≤70)t(1≤t≤70), the number of the testcases.?

For each testcase, the first line contains two numbers?n,m(1≤n≤50,1≤m≤104)n,m(1≤n≤50,1≤m≤104).

The second line contains?m+1m+1?numbers?B0?Bm(0≤Bi≤2n)B0?Bm(0≤Bi≤2n).

Output

For each testcase, print a single line with?nn?numbers?A1?AnA1?An.?

It is guaranteed that there exists at least one solution. And if there are different solutions, print the lexicographic minimum one.

Sample Input

2 2 3 1 1 1 1 3 3 1 3 3 1

Sample Output

1 2 1 1 1

Hint

In the first sample, $A$ is $[1,2]$. $A$ has four subsets $[],[1],[2],[1,2]$ and the sums of each subset are $0,1,2,3$. So $B=[1,1,1,1]$

解題思路:

用dp[j]來表示組成 j 所需的方案數,那么對于數 i 來說,數 i 的個數就是子序列中和為 i 的個數即 b[i] 減去其他數字組成 i 的個數即 dp[i],由此可以算出數字 i 的個數。

由于dp[j]來表示組成 j 所需的方案數,那么對于dp[j]來說,dp[j] = dp[j] + dp[j-i]

因此我們可以先遍歷要確定的數字即最外層的循環 for(ll i=1;i<=m;i++),再確定數字 i 的個數,內兩層循環相當于一個01背包,以確定更新dp數組

代碼:

#include <cstdio> #include <iostream> #include <algorithm> #include <cmath> #include <cstdlib> #include <cstring> #include <map> #include <stack> #include <queue> #include <vector> #include <bitset> #include <set> #include <utility> #include <sstream> #include <iomanip> using namespace std; typedef long long ll; typedef unsigned long long ull; #define inf 0x3f3f3f3f #define rep(i,l,r) for(int i=l;i<=r;i++) #define lep(i,l,r) for(int i=l;i>=r;i--) #define ms(arr) memset(arr,0,sizeof(arr)) //priority_queue<int,vector<int> ,greater<int> >q; const int maxn = (int)1e5 + 5; const ll mod = 1e9+7; ll a[120]; ll dp[10100]; ll b[10020]; int main() {#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int t;scanf("%d",&t);while(t--) {ms(dp);int n,m;scanf("%d %d",&n,&m);rep(i,0,m) scanf("%I64d",&b[i]);dp[0]=1;int cnt=0;for(ll i=1;i<=m;i++) {if(b[i]) {if(cnt==n) break;ll num=b[i]-dp[i];for(ll j=1;j<=num;j++) {a[++cnt]=i;for(ll k=m;k>=i;k--) {dp[k]=dp[k]+dp[k-i];}}}}for(int i=1;i<cnt;i++) {printf("%I64d ",a[i]);}printf("%I64d\n",a[cnt]);}return 0; }

?

總結

以上是生活随笔為你收集整理的HDU6092——Rikka with Subset 【dp】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。