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, s 1 s_1 s1 anf s 2 s_2 s2, consisting of uppercase English letters. Then the boy makes string sk, using a recurrent equation s n = s n − 2 + s n − 1 s_n = s_{n - 2} + s_{n - 1} 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 s 1 s_1 s1 should have length n, and string s 2 s_2 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, s 1 s_1 s1 and s 2 s_2 s2 in the required manner. Help Vasya.
Input
The first line contains four integers k , x , n , m ( 3 ≤ k ≤ 50 ; 0 ≤ x ≤ 1 0 9 ; 1 ≤ n , m ≤ 100 ) k, x, n, m (3 ≤ k ≤ 50; 0 ≤ x ≤ 10^9; 1 ≤ n, m ≤ 100) k, x, n, m(3 ≤ k ≤ 50;0 ≤ x ≤ 109;1 ≤ n, m ≤ 100).
Output
In the first line print string s 1 s_1 s1, consisting of n n n uppercase English letters. In the second line print string s 2 s_2 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
Input1
3 2 2 2Output1
AC ACInput2
3 3 2 2Output2
Happy new year!Input3
3 0 2 2Output3
AA AAInput4
4 3 2 1Output4
Happy new year!Input5
4 2 2 1Output5
Happy new year!题意: 给一个字符串生成方式: s n = s n − 2 + s n − 1 s_n=s_{n-2}+s_{n-1} sn=sn−2+sn−1,然后询问,如果要使得 s k s_k sk中子串AC的数量刚好为 x x x, s 1 s_1 s1, s 2 s_2 s2分别是多少, s 1 s_1 s1, s 2 s_2 s2长度刚好分别为 n n n, m m m。 ( k < = 50 , x < = 1 0 9 , n , m < = 100 ) (k<=50,x<=10^9,n,m<=100) (k<=50,x<=109,n,m<=100)
题解: 枚举 s 1 s_1 s1, s 2 s_2 s2中的子串AC数量、开头字母、结尾字母,然后 O ( k ) O(k) O(k)判断 s k s_k sk中的子串数量就行了。
#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; }