天天看點

CodeForces - 379D New Year Tree Decorations(暴力枚舉)

題目連結:http://codeforces.com/problemset/problem/379/D

題意:給出四個數 k , x , n , m k,x,n,m k,x,n,m,構造出長度分别為 n , m n,m n,m的字元串 s 1 s_1 s1​, s 2 s_2 s2​, s i = s i − 1 + s i − 2 ( i > 2 ) s_i=s_{i-1}+s_{i-2}(i>2) si​=si−1​+si−2​(i>2),使 s k s_k sk​中 " A C " "AC" "AC"的數量為 x x x,若無法實作輸出 H a p p y Happy Happy n e w new new y e a r ! year! year!;

D. New Year Letter

time limit per test1 second

memory limit per test256 megabytes

inputstandard input

outputstandard output

Many countries have such a New Year or Christmas tradition as writing a letter to Santa including a wish list for presents. Vasya is an ordinary programmer boy. Like all ordinary boys, he is going to write the letter to Santa on the New Year Eve (we Russians actually expect Santa for the New Year, not for Christmas).

Vasya has come up with an algorithm he will follow while writing a letter. First he chooses two strings, s1 anf s2, consisting of uppercase English letters. Then the boy makes string sk, using a recurrent equation sn = sn - 2 + sn - 1, operation ‘+’ means a concatenation (that is, the sequential record) of strings in the given order. Then Vasya writes down string sk on a piece of paper, puts it in the envelope and sends in to Santa.

Vasya is absolutely sure that Santa will bring him the best present if the resulting string sk has exactly x occurrences of substring AC (the short-cut reminds him оf accepted problems). Besides, Vasya decided that string s1 should have length n, and string s2 should have length m. Vasya hasn’t decided anything else.

At the moment Vasya’s got urgent New Year business, so he asks you to choose two strings for him, s1 and s2 in the required manner. Help Vasya.

Input

The first line contains four integers k, x, n, m (3 ≤ k ≤ 50; 0 ≤ x ≤ 109; 1 ≤ n, m ≤ 100).

Output

In the first line print string s1, consisting of n uppercase English letters. In the second line print string s2, consisting of m uppercase English letters. If there are multiple valid strings, print any of them.

If the required pair of strings doesn’t exist, print “Happy new year!” without the quotes.

Examples

input

3 2 2 2

output

AC

AC

input

3 3 2 2

output

Happy new year!

input

3 0 2 2

output

AA

AA

input

4 3 2 1

output

Happy new year!

input

4 2 2 1

output

Happy new year!

解題思路:暴力枚舉;

具體做法:用 i , j i,j i,j分别表示 s 1 , s 2 s_1,s_2 s1​,s2​中 " A C " "AC" "AC"的個數,用 a a a表示 s 1 s_1 s1​頭部是否有‘A’,用 b b b表示 s 1 s_1 s1​尾部是否有‘C’,用 c c c表示 s 2 s_2 s2​頭部是否有‘A’,用 d d d表示 s 2 s_2 s2​尾部是否有‘C’;

用 f i f_i fi​表示字元串 s i s_i si​中"AC"的個數,那麼, f 1 = i f_1=i f1​=i, f 2 = j f_2=j f2​=j,若 b + c = 2 b+c=2 b+c=2則 f 3 = i + j + 1 f_3=i+j+1 f3​=i+j+1,否則 f 3 = i + j f_3=i+j f3​=i+j,繼續推可以得到當 t > 3 t>3 t>3時的表達式 f t = f t − 1 + f t − 2 + f_t=f_{t-1}+f_{t-2}+ ft​=ft−1​+ft−2​+

((t%2==1 && d+c==2)||(t%2==0 && a+d==2))}

注意:不能 f 3 f_3 f3​通過該遞推式求出,要單獨讨論,此時是 s 1 s_1 s1​尾部與 s 2 s_2 s2​頭部相連,而 t > 3 t>3 t>3時是 s 1 s_1 s1​頭部與 s 2 s_2 s2​尾部相連 ( ( (t為偶數 ) ) )或 s 2 s_2 s2​頭部與 s 2 s_2 s2​尾部相連 ( ( (t為奇數 ) ) );

代碼如下:

#include<bits/stdc++.h>
#define int long long
using namespace std;
int k,x,n,m,f[20005];
signed main()
{
    cin>>k>>x>>n>>m;
    for (int i=0;i<=n>>1;i++)
    for (int j=0;j<=m>>1;j++)//用i,j分别表示s1,s2中"AC"的個數
    {
        for (int a=0;a<=1;a++)
        for (int b=0;b<=1;b++)//用a表示s1頭部是否有‘A’,用b表示s1尾部是否有‘C’,
        for (int c=0;c<=1;c++)
        for (int d=0;d<=1;d++)//用c表示s2頭部是否有‘A’,用d表示s2尾部是否有‘C’;
        {
            if (2*i+a+b>n || 2*j+c+d>m) continue;//超過限制長度時傳回
            f[1]=i;
            f[2]=j;
            f[3]=i+j+(b+c==2);
            for (int t=4;t<=k;t++)//通過遞推得到sk中"AC"的數量
            {
                f[t]=f[t-1]+f[t-2]+(((t&1)==1 && d+c==2)||((t&1)==0 && a+d==2));
            }
            if (f[k]==x)//輸出結果
            {
                if (a) cout<<"C";
                for (int t=1;t<=i;t++) cout<<"AC";
                for (int t=1;t<=n-2*i-a-b;t++) cout<<"X";//補充不足的位數
                if (b) cout<<"A";
                cout<<endl;
                if (c) cout<<"C";
                for (int t=1;t<=j;t++) cout<<"AC";
                for (int t=1;t<=m-2*j-c-d;t++) cout<<"X";
                if (d) cout<<"A";
                return 0;
            }
        }
    }
    cout<<"Happy new year!";//無法實作時輸出Happy new year!
    return 0;
}
           

推薦一篇題解:

https://www.cnblogs.com/stepsys/p/10383485.html