题目链接: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