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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

[USACO06FEB]数字三角形

發布時間:2025/3/15 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [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 916

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 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]数字三角形的全部內容,希望文章能夠幫你解決所遇到的問題。

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