1. 程式人生 > >【ICPC 2018 Malaysia】UPC-9302 ELI'S CURIOUS MIND(遞推)

【ICPC 2018 Malaysia】UPC-9302 ELI'S CURIOUS MIND(遞推)

題目描述
Eli is a teenager who loves to study chemistry. She recently joined a chemistry research lab.
Dr. Phil wants her to just play around with some chemicals and observe their reaction.
Therefore, he gave her a one-row tray of test tubes with the different chemical inside of them and told her:
"Mix these chemical together anyhow that you like, but the you have to follow two rules:

  1. Never make a mixture that has two chemicals that their tubes are next to each other.
  2. Keep adding more chemical to the mixture until it is not violating the new rule. "
    For example, in the image you can see she was given 5 tubes and she is able to make 4 different mixture without violating the rule: {1,3,5}, {2,4}, {2,5}, {1,4}.
    But she cannot mix 1 and 3 only because she still can add 5 without violating the rules.
    She is curious to know how many different mixtures she can make without violating the rule with any given number of tubes. That’s why she asks you write a code to calculate it for her.

輸入
The input will consist of a sequence of numbers N, 1≤N≤ 76. Each number will be on a separate line. The input will be terminated by 0.

輸出
Output the number of different mixture she can make without violating the rule mentioned above on a single line as show in the sample. The number of all subsets will be less than 231 .

樣例輸入
1
2
3
4
5
30
0

樣例輸出
Case #1: 0
Case #2: 0
Case #3: 1
Case #4: 3
Case #5: 4
Case #6: 4410

題意: 有編號為1~n的試管,現在規定相鄰兩個試管不能混合,其餘的可以混合,問可以得到多少種不同的混合物,若一種混合方式包含了另一種集合中的所有混合物,即算同一種混合方式。

舉例說明:
1個試管:0種混合方式
2個試管:0種混合方式
3個試管:1種混合方式 {1,3}
4個試管:3種混合方式 {1,3}、{2,4}、{1,4}
5個試管:4種混合方式 {1,3,5}、{2,4}、{2,5}、{1,5}
6個試管:5種混合方式 {1,3,5}、{1,3,6}、{2,4,6}、{2,5}、{1,4,6}
…以此類推

可以發現,因為要求不相鄰的試管混合,因此每次新添一個試管i,新試管的混合方案總是和第i-2和i-3位置的所有試管混合,不會跨越到i-4是因為i到i-4之間肯定會有一個i-2插在中間,就比如6不會和2再添新組合是因為2,4,6中包含了2,6。
因此每次第i個試管的混合物方式總是i-2與i-3個試管的總和。
又因為,雖然新試管不能和i-1中某些相鄰的集合混合。但是其混合方式是存在的,我們直接繼承下來,繼承下來i-1個試管中沒有用到試管i的所有組合方式。
這樣一來一個遞推就可以得到結果。
最後利用樣例或者手推得到的試管1到試管5的結果做遞推的初始化。
其中3個試管時就1種沒有問題,4個試管時,因為繼承了4-1=3的集合{1,3},因此要刪掉這個繼承值,我們只計數用到新試管的混合方式。因此4個試管時2種。
同理,5個試管時,{1,3,5}繼承自{1,3},不算,{2,4}沒有用到新試管,不算,真正新生成的混合方式只有{1,5}和{2,5},因此5個試管時是2.
在後續增加更多的試管時,就不會出現這樣的憑空增加了,因為最後全部是以{1,3}、{1,5}、{2,4}、{2,5}所生成的新序列,因為之前說的,只考慮i-2和i-3的增加,小於i-4的位置都會夾著i-3或i-2.
程式碼如下:

#include<bits/stdc++.h>
#define LL long long
#define M(a,b) memset(a,b,sizeof a)
#define pb(x) push_back(x)
using namespace std;
const int maxn=1e3+7;
int n;
LL a[80];
int main()
{
    int cas=1;
    a[1]=a[2]=0;
    a[3]=1,a[4]=a[5]=2;
    for(int i=6;i<=76;i++)a[i]=a[i-2]+a[i-3];
    while(scanf("%d",&n)&&n) printf("Case #%d: %lld\n",cas++,a[n-1]+a[n]);
}

相關推薦

ICPC 2018 MalaysiaUPC-9302 ELI'S CURIOUS MIND()

題目描述 Eli is a teenager who loves to study chemistry. She recently joined a chemistry research lab. Dr. Phil wants her to just

ACM-ICPC 2018 焦作賽區網路預賽 Poor God Water(+構造矩陣)

God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make

WebLogic任意文件上傳漏洞復現與分析 -CVE-2018-2894

安全 tar lan ide web htm 過濾 eval base CVE-2018-2894 漏洞影響版本:10.3.6.0, 12.1.3.0, 12.2.1.2, 12.2.1.3 下載地址:http://download.oracle.com/otn/nt/mi

ICPC 2018 Malaysia(solve10/10)

題目連結:upc oj A題 題意:給你n個人數到第m個人出來,問你最後一個是誰。 思想:約瑟夫環數學問題(公式) B題 題意:給你n個城市m條路,然後給你k條路徑,必須從u到v,然後問你走完這k條路最小花費是多少,如果不能走通輸出-1 思想:先將k個點和其他k-

HDU 1568 Fibonacci求斐波那契數的前4位/

urn content new targe 接下來 bsp hide 斐波那契 href Fibonacci Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other

劍指offer斐波那契數列非迴求解第N項

public class Solution {    public int Fibonacci(int n) {       //錯誤輸入處理       if

劍指offer斐波那契數列非歸求解第N項

非遞歸 acc 斐波那契 錯誤 bsp fibonacci 更新 off for public class Solution { public int Fibonacci(int n) { //錯誤輸入處理 if(n<0) return

劍指Offer輸入一個正數s,打印出所有和為s 的連續正數序列(序列大小至少為2) (C++)

題目用例: s = 15,那麼由於1+2+3+4+5 = 4+5+6=7+8 = 15,所以存在3個這樣的序列。 分析: 由於序列大小至少為2,我們定義兩個變數l和r,分別表示序列的最小值和最大值。 對於r而言,r取何值其實是有一個範圍的,即當序列只有2

BZOJ4944NOI2017泳池 概率DP 常係數線性 特徵多項式 多項式取模

題目大意   有一個1001×n1001×n的的網格,每個格子有qq的概率是安全的,1−q1−q的概率是危險的。   定義一個矩形是合法的當且僅當: 這個矩形中每個格子都是安全的 必須緊貼網格的下邊界   問你最大的合法子矩形大小

ACM-ICPC 2018 南京賽區網絡預賽 JSum

ont bre for 2個 namespace 最小 esp 我們 nan 【鏈接】 我是鏈接,點我呀:) 【題意】 在這裏輸入題意 【題解】 線性篩求出每個數的最小質因子x for 從1-n 對於i,它的最小質因子為x 考慮i=ab 如果i能被x^3整除

ACM-ICPC 2018 徐州賽區網絡預賽E. End Fantasy VIX 血辣 (矩陣運算的推廣)

有向圖 opened help 問題 with problem rep har ace Morgana is playing a game called End Fantasy VIX. In this game, characters have nn skills,

ACM-ICPC 2018 沈陽賽區網絡預賽 KSupreme Number

string ase -a pri 位數 整除 while ant efi 【鏈接】 我是鏈接,點我呀:) 【題意】 在這裏輸入題意 【題解】 顯然每個數字只可能是1,3,5,7 然後如果3,5,7這些數字出現兩次以上。顯然兩個3||5||7都能被11整除。

ACM-ICPC 2018 沈陽賽區網絡預賽 DMade In Heaven

amp can cos white clu clas prior -- hit 【鏈接】 我是鏈接,點我呀:) 【題意】 在這裏輸入題意 【題解】 點可以重復走的k短路。 【代碼】 #include <bits/stdc++.h> #defi

ACM-ICPC 2018 沈陽賽區網絡預賽 GSpare Tire

std += anti 數字 n+2 num names return include 【鏈接】 我是鏈接,點我呀:) 【題意】 在這裏輸入題意 【題解】 讓你求出1..n中和m互質的位置i. 讓你輸出∑ai 這個ai可以oeis一波。 發現是ai = i(

費馬小定理+快速冪取模ACM-ICPC 2018 焦作賽區網絡預賽 G. Give Candies

print using pri long long ger ssi bit one ive G. Give Candies There are N children in kindergarten. Miss Li bought them N candies. To mak

The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online - H Traveling on the Axis-思維模擬題目

view 本地 lang char following ring listitem swe src H Traveling on the Axis    作者: 浙江大學競賽命題組 單位: ACMICPC 時間限制: 500 ms 內存限制: 64 MB 代碼長度

ACM/ICPC 2018亞洲區預選賽北京賽站網絡賽 A、Saving Tang Monk II 狀態搜索

ddd namespace bfs lease use break 猴子 解法 nal 任意門:http://hihocoder.com/problemset/problem/1828 Saving Tang Monk II 時間限制:1000ms 單點時限:1000ms

多重揹包+二進位制優化ACM-ICPC 2018 焦作賽區網路預賽 - K. Transport Ship

題目連結<https://nanti.jisuanke.com/t/31720> 題意: 給出若干個船,每個船都有的容量,和的數量。有Q次詢問,給出一定體積的貨物,問你恰好裝滿的方案數量。 題解: 很明顯的多重揹包,但是要加上二進位制優化。 所謂二進位制優

dpACM-ICPC 2018 焦作賽區網路預賽 - B - Mathematical Curse

題目連結<https://nanti.jisuanke.com/t/31711> 題意: 有n個數,m個加減乘除的運算子。按順序取出m個數,進行對應的運算子操作。給你一個初始值,問能構成的最大結果是多少。 題解: 考慮只有加減這兩種運算子的情況,那麼可以想到一

2018 ACM-ICPC北京賽區 Palindromes 迴文數

題目傳送門 時間限制:1000ms 單點時限:1000ms 記憶體限制:512MB 描述 Recently, Nvoenewr learnt palindromes in his class. A palindrome is a nonnegative integer t