[codeforces379D]New Year Letter

Vasya计划在新年夜写信给圣诞老人,请求特定次数的'AC'子串作为礼物。通过遵循一个特定的字符串生成算法,他需要找到两个初始字符串s1和s2,使得最终的第k个字符串sk包含恰好x次'AC'子串。这个挑战涉及了字符串操作和递归算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

time limit per test:1 second
memory limit per test:256 megabytes

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, s1s_1s1 anf s2s_2s2, consisting of uppercase English letters. Then the boy makes string sk, using a recurrent equation sn = sn − 2 + sn − 1s_n = s_{n - 2} + s_{n - 1}sn=sn2+sn1, 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 s1s_1s1 should have length n, and string s2s_2s2 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, s1s_1s1 and s2s_2s2 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)k, x, n, m (3 ≤ k ≤ 50; 0 ≤ x ≤ 10^9; 1 ≤ n, m ≤ 100)k,x,n,m(3k50;0x109;1n,m100).

Output

In the first line print string s1s_1s1, consisting of nnn uppercase English letters. In the second line print string s2s_2s2, 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

Input1

3 2 2 2

Output1

AC
AC

Input2

3 3 2 2

Output2

Happy new year!

Input3

3 0 2 2

Output3

AA
AA

Input4

4 3 2 1

Output4

Happy new year!

Input5

4 2 2 1

Output5

Happy new year!

题意:
给一个字符串生成方式:sn=sn−2+sn−1s_n=s_{n-2}+s_{n-1}sn=sn2+sn1,然后询问,如果要使得sks_ksk中子串AC的数量刚好为xxxs1s_1s1,s2s_2s2分别是多少,s1s_1s1,s2s_2s2长度刚好分别为nnnmmm(k&lt;=50,x&lt;=109,n,m&lt;=100)(k&lt;=50,x&lt;=10^9,n,m&lt;=100)(k<=50,x<=109,n,m<=100)

题解:
枚举s1s_1s1s2s_2s2中的子串AC数量、开头字母、结尾字母,然后O(k)O(k)O(k)判断sks_ksk中的子串数量就行了。

#include<bits/stdc++.h>
#define LiangJiaJun main
#define ll long long
using namespace std;
int k,x,n,m;
struct S{
    int f,b;
    ll cnt;
}a[54];
int l[54];
char s[4][104];
bool check(int x){

     if(a[x].f==1)s[x][1]='A';
     if(a[x].f==2)s[x][1]='C';
     if(a[x].f==3)s[x][1]='B';
     if(a[x].b==1)s[x][l[x]]='A';
     if(a[x].b==2)s[x][l[x]]='C';
     if(a[x].b==3)s[x][l[x]]='B';

     if(l[x]==1&&a[x].f!=a[x].b)return 0;
     if(l[x]==2&&a[x].f==1&&a[x].b==2&&a[x].cnt!=1)return 0;
     if(l[x]==2&&a[x].f==a[x].b&&a[x].cnt!=0)return 0;
     if(l[x]==2&&a[x].f==2&&a[x].b==1&&a[x].cnt!=0)return 0;
     if(a[x].cnt==0){
        for(int i=2;i<l[x];i++)s[x][i]='B';
        return 1;
     }
     int cnt=0;
     int bg;
     if(s[x][1]=='A')bg=1;
     else bg=2;
     while(bg<l[x]&&cnt<a[x].cnt){
        s[x][bg]='A';
        if(bg+1==l[x]){
            if(s[x][l[x]]!='C')return 0;
            else s[x][l[x]]='C';
        }
        else s[x][bg+1]='C';
        ++cnt;
        bg+=2;
     }
     for(int i=bg;i<l[x];i++)s[x][bg]='B';
     return (cnt==a[x].cnt);
}
void output(){
     for(int i=1;i<=2;i++){
         for(int j=1;j<=l[i];j++)printf("%c",s[i][j]);
         puts("");
     }
     return ;
}
int w33ha(){
    l[1]=n;
    l[2]=m;
    for(int i=0;i<=n/2;i++){
        for(int j=0;j<=m/2;j++){
            for(int o=1;o<=3;o++){
                for(int p=1;p<=3;p++){
                    for(int q=1;q<=3;q++){
                        for(int r=1;r<=3;r++){
                            a[1].f=o;
                            a[1].b=p;
                            a[1].cnt=i;
                            a[2].f=q;
                            a[2].b=r;
                            a[2].cnt=j;
                            if(check(1)&&check(2)){
                                bool flag=1;
                                for(int y=3;y<=k;y++){
                                    a[y].f=a[y-2].f;
                                    a[y].b=a[y-1].b;
                                    if(a[y-2].b==1&&a[y-1].f==2){
                                        a[y].cnt=1;
                                    }
                                    else a[y].cnt=0;
                                    a[y].cnt+=a[y-2].cnt+a[y-1].cnt;
                                    if(a[y].cnt>(1LL*x)){
                                        flag=0;
                                        break;
                                    }
                                }
                                if(!flag)continue;
                                if(a[k].cnt==(1LL*x)){
                                    output();
                                    return 0;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    puts("Happy new year!");
    return 0;
}

int LiangJiaJun(){
    while(scanf("%d%d%d%d",&k,&x,&n,&m)!=EOF)w33ha();
    return 0;
}

### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值