題目描述
FJ
and his cows enjoy playing a mental game. They write down the numbers from 11 toN(1 \le N \le 10)N(1≤N≤10) in a certain order and then sum adjacent numbers to produce a new list with one fewer number. They repeat this until only a single number is left. For example, one instance of the game (when N=4N=4) might go like this:
3 1 2 4
4 3 6
7 9
16
Behind FJ
's back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from only the final total and the number NN. Unfortunately, the game is a bit above FJ
's mental arithmetic capabilities.
Write a program to help FJ
play the game and keep up with the cows.
有這麼一個遊戲:
寫出一個11至NN的排列a_iai,然後每次將相鄰兩個數相加,構成新的序列,再對新序列進行這樣的操作,顯然每次構成的序列都比上一次的序列長度少11,直到只剩下一個數字位置。下面是一個例子:
3,1,2,43,1,2,4
4,3,64,3,6
7,97,9
1616
最後得到1616這樣一個數字。
現在想要倒着玩這樣一個遊戲,如果知道NN,知道最後得到的數字的大小sumsum,請你求出最初序列a_iai,爲11至NN的一個排列。若答案有多種可能,則輸出字典序最小的那一個。
[color=red]管理員注:本題描述有誤,這裏字典序指的是1,2,3,4,5,6,7,8,9,10,11,121,2,3,4,5,6,7,8,9,10,11,12
而不是1,10,11,12,2,3,4,5,6,7,8,91,10,11,12,2,3,4,5,6,7,8,9[/color]
輸入輸出格式
輸入格式:
兩個正整數n,sumn,sum。
輸出格式:
輸出包括11行,爲字典序最小的那個答案。
當無解的時候,請什麼也不輸出。(好奇葩啊)
輸入輸出樣例
輸入樣例#1: 複製
4 16
輸出樣例#1: 複製
3 1 2 4
說明
對於40\%40%的數據,n≤7n≤7;
對於80\%80%的數據,n≤10n≤10;
對於100\%100%的數據,n≤12,sum≤12345n≤12,sum≤12345。
解法:首先要考慮的一個問題是,給定一個序列如何知道最後的和會是多少。
可以手動模擬一下發現,bottom-up的走一遍,對於第i層的第j個數,它的產生是由於第i-1層第j個數和第i-1層第j-1個數相加得到的。
這樣遞推上去我們可以計算出每個數初始會被加幾次。
將之存儲即可。(我們也可以發現最後這會組成楊輝三角)
然後就是怎麼找到正確的排列的問題了。
一開始想求助stl大法的next_Permutation。依次掃,但顯然會超時。
於是我們就dfs,只需要加一點剪枝就可以過。
這裏有一步計算最大值和最小值,如果讀入的sum不在此範圍內直接return。(這步可做可不做。)
#include <bits/stdc++.h>
#define pii pair<int, int>
#define vi vector<int>
#define ll long long
#define eps 1e-5
using namespace std;
ll sum;
int n;
int t[20];
int res[20][20];
int cal1()//計算最小值
{
int temp[20] = {0};
if(n % 2)
{
temp[n / 2 + 1] = 1;
int idx = 2, s = 0;
for(int i = 1; i <= n / 2; i++)
{
temp[n / 2 + 1 + i] = idx++;
temp[n / 2 + 1 - i] = idx++;
}
for(int i = 1; i <= n; i++)
s += temp[i] * res[1][i];
return s;
}
else
{
temp[n / 2] = 1;
temp[n / 2 + 1] = 2;
int idx = 3, s = 0;
for(int i = 1; i <= n / 2 - 1; i++)
{
temp[n / 2 - i] = idx++;
temp[n / 2 + 1 + i] = idx++;
}
for(int i = 1; i <= n; i++)
s += temp[i] * res[1][i];
return s;
}
}
int cal2()//計算最大值
{
int temp[20] = {0};
if(n % 2)
{
temp[n / 2 + 1] = n;
int idx = n - 1, s = 0;
for(int i = 1; i <= n / 2; i++)
{
temp[n / 2 + 1 + i] = idx--;
temp[n / 2 + 1 - i] = idx--;
}
for(int i = 1; i <= n; i++)
s += temp[i] * res[1][i];
return s;
}
else
{
temp[n / 2] = n;
temp[n / 2 + 1] = n - 1;
int idx = n - 2, s = 0;
for(int i = 1; i <= n / 2 - 1; i++)
{
temp[n / 2 - i] = idx--;
temp[n / 2 + 1 + i] = idx--;
}
for(int i = 1; i <= n; i++)
s += temp[i] * res[1][i];
return s;
}
}
bool vis[20];
void dfs(int len, int now, vi& p)
{
if(now > sum)
return;
if(now == sum && len < n) return;
if(len == n)
{
if(now == sum)
{
for(auto idx: p)
cout << idx << ' ';
cout << endl;
exit(0);
}
return;
}
for(int i = 1; i <= n; i++)
{
if(!vis[i])
{
vis[i] = 1;
p.push_back(i);
dfs(len + 1, now + res[1][len + 1] * i, p);
vis[i] = 0;
p.pop_back();
}
}
}
int main()
{
// freopen("/Users/vector/Desktop/testdata.in", "r", stdin);
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> sum;
for(int i = 1; i <= n; i++)
t[i] = i;
res[n][1] = 1;
for(int i = n ; i >= 1; i--)
{
for(int j = 1; j <= n - i + 1; j++)
{
res[i - 1][j] += res[i][j];
res[i - 1][j + 1] += res[i][j];
}
}
// for(int i = 1; i <= n; i++)
// cout << res[1][i] << ' ' ;
int mmin = cal1(), mmax = cal2();
if(sum > mmax || sum < mmin)
return 0;
vi q;
dfs(0, 0, q);
return 0;
}