[USACO06FEB]数字三角形
題目描述
FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to 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=4) might go like this:
3 1 2 44 3 67 916Behind 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 N. 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.
有這么一個游戲:
寫出一個1~N的排列a[i],然后每次將相鄰兩個數相加,構成新的序列,再對新序列進行這樣的操作,顯然每次構成的序列都比上一次的序列長度少1,直到只剩下一個數字位置。下面是一個例子:
3 1 2 4
4 3 6
7 9 16 最后得到16這樣一個數字。
現在想要倒著玩這樣一個游戲,如果知道N,知道最后得到的數字的大小sum,請你求出最初序列a[i],為1~N的一個排列。若答案有多種可能,則輸出字典序最小的那一個。
[color=red]管理員注:本題描述有誤,這里字典序指的是1,2,3,4,5,6,7,8,9,10,11,12
而不是1,10,11,12,2,3,4,5,6,7,8,9[/color]
輸入輸出格式
輸入格式:
兩個正整數n,sum。
輸出格式:
輸出包括1行,為字典序最小的那個答案。
當無解的時候,請什么也不輸出。(好奇葩啊)
輸入輸出樣例
輸入樣例#1:
4 16
輸出樣例#1:
3 1 2 4
說明
對于40%的數據,n≤7;
對于80%的數據,n≤10;
對于100%的數據,n≤12,sum≤12345。
.
.
.
.
.
.
.
.
分析
運用的dfs思想,f代表這一層的楊輝三角。a代表這一層的排列。
.
.
.
.
.
.
.
.
程序:
var f:array[0..130,0..130]of longint; a:array[0..130]of longint; v:array[0..130]of boolean; j,n,m,sum,i:longint;procedure dfs(i:longint); var j:longint; beginif m>sum then exit;if i=n+1 thenbeginif sum=m thenbeginfor j:=1 to n dowrite(a[j],' ');halt;end;exit;end;for j:=1 to n doif v[j]=true thenbegina[i]:=j;m:=m+f[n,i]*j;v[j]:=false;dfs(i+1);a[i]:=0;m:=m-f[n,i]*j;v[j]:=true;end; end;beginread(n,sum);for i:=1 to n dof[i,1]:=1;for i:=2 to n dofor j:=2 to i dof[i,j]:=f[i-1,j]+f[i-1,j-1];m:=0;fillchar(v,sizeof(v),true);dfs(1); end.轉載于:https://www.cnblogs.com/YYC-0304/p/9499989.html
總結
以上是生活随笔為你收集整理的[USACO06FEB]数字三角形的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 格点统计
- 下一篇: A % B Problem