枚举开头结尾的字母,枚举ac的个数,总AC个数就是两个Fibonacci数列的和。。。。。

D. New Year Letter
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard 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.

Sample test(s)
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!
#include <iostream>
#include <cstring>
#include <cstdio> using namespace std; int dp[120];
char sa[12000],sb[12000];
long long int fib[120]; int getMAX(char s,char e,int l)
{
if(l==1)
{
return 0;
}
if(l==2)
{
if(s=='A'&&e=='C') return 1;
else return 0;
}
if(l==3)
{
if(s=='A'||e=='C') return 1;
else return 0;
}
if(l>=4)
{
if(s=='A'&&e=='C') return (l-4)/2+2;
else if((s!='A'&&e=='C')||(s=='A'&&e!='C')) return (l-3)/2+1;
else return (l-2)/2;
}
} long long int getfibK(int a,int b,int x)
{
fib[0]=0;fib[1]=a;fib[2]=b;
for(int i=3;i<=x;i++)
{
fib[i]=fib[i-1]+fib[i-2];
}
if(fib[x]>1e9) return -1;
return fib[x];
} int main()
{
int k,x,n,m;
scanf("%d%d%d%d",&k,&x,&n,&m);
char s1,e1,s2,e2;
for(s1='A';s1<='C';s1++)
{
for(e1='A';e1<='C';e1++)
{
if(n==1&&s1!=e1) continue;
for(s2='A';s2<='C';s2++)
{
for(e2='A';e2<='C';e2++)
{
if(m==1&&s2!=e2) continue;
memset(dp,0,sizeof(dp));
char ls1=s1,le1=e1,ls2=s2,le2=e2;
for(int i=3;i<=k;i++)
{
dp[i]=dp[i-2]+(le1=='A'&&ls2=='C')+dp[i-1];
char ts=ls2;
ls2=ls1;
ls1=ts;le1=le2;
}
long long int tx=dp[k];
if(tx>x) continue;
int MaxS1=getMAX(s1,e1,n),MaxS2=getMAX(s2,e2,m);
for(int i=0;i<=MaxS1;i++)
{
for(int j=0;j<=MaxS2;j++)
{
long long int ty=getfibK(i,j,k);
if(ty<0) continue;
if(tx+ty==(long long int )x)
{
int posA=i,posB=j;
int cnta=1,cntb=1;
sa[0]=s1;sa[n-1]=e1;
sb[0]=s2;sb[m-1]=e2;
if(n!=1)
{
while(posA--)
{
if(cnta==1&&sa[0]=='A')
{
sa[cnta++]='C';
}
else
{
sa[cnta++]='A';
sa[cnta++]='C';
}
}
for(int ck=cnta;ck<n-1;ck++)
{
sa[ck]='B';
}
}
if(m!=1)
{
while(posB--)
{
if(cntb==1&&sb[0]=='A')
{
sb[cntb++]='C';
}
else
{
sb[cntb++]='A';
sb[cntb++]='C';
}
}
for(int ck=cntb;ck<m-1;ck++)
{
sb[ck]='B';
}
}
printf("%s\n",sa);
printf("%s\n",sb);
return 0;
}
}
}
}
}
}
}
printf("Happy new year!\n");
return 0;
}

CodeForces 379 D. New Year Letter的更多相关文章

  1. Codeforces 379 F. New Year Tree

    \(>Codeforces \space 379 F. New Year Tree<\) 题目大意 : 有一棵有 \(4\) 个节点个树,有连边 \((1,2) (1,3) (1,4)\) ...

  2. 【codeforces 379D】New Year Letter

    [题目链接]:http://codeforces.com/contest/379/problem/D [题意] 让你构造出两个长度分别为n和m的字符串s[1]和s[2] 然后按照连接的规则,顺序连接s ...

  3. Codeforces Round #370 - #379 (Div. 2)

    题意: 思路: Codeforces Round #370(Solved: 4 out of 5) A - Memory and Crow 题意:有一个序列,然后对每一个进行ai = bi - bi  ...

  4. codeforces Good Bye 2013 379D New Year Letter

    题目链接:http://codeforces.com/problemset/problem/379/D [题目大意] 告诉你初始字符串S1.S2的长度和递推次数k, 使用类似斐波纳契数列的字符串合并的 ...

  5. Codeforces 379D - New Year Letter

    原题地址:http://codeforces.com/contest/379/problem/D 题目大意:给出一种生成字符串的方法 s[k] = s[k - 2] + s[k - 1],其中加法意为 ...

  6. Codeforces Round #116 (Div. 2, ACM-ICPC Rules) C. Letter 暴力

    C. Letter Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/180/problem/C D ...

  7. Codeforces Beta Round #14 (Div. 2) A. Letter 水题

    A. Letter 题目连接: http://www.codeforces.com/contest/14/problem/A Description A boy Bob likes to draw. ...

  8. Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径

    E. Anton and Tree 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ...

  9. Codeforces Round #379 (Div. 2) D. Anton and Chess 水题

    D. Anton and Chess 题目连接: http://codeforces.com/contest/734/problem/D Description Anton likes to play ...

随机推荐

  1. 【MFC学习笔记-作业5-小数据库】【单选框,复选框,滚动条,列表框】

    界面已经实现完毕. 要完成的操作就是1.性别分组(2选1) 2.属性勾选 3.年龄通过滚动条调整 4.职称通过下方的列表框选择 5.输入姓名 6.存入左方的列表框 7.当选择左方列表框的人时,可以显示 ...

  2. DevExpress GridView使用技巧之列标题点击事件

    在这里使用GridView的MouseDown事件.这里同样使用的是GridHitInfo来获取点击位置的信息,来判断是否在列标题上.GridHitInfo根据鼠标点击的x.y坐标获取该点的相关信息, ...

  3. 关于常用meta的总结

    入行也半年了,无数次的想过写博客也无数次的想过第一篇会写什么,一直没有落实.今天心血来潮把博客开了,那就写点东西吧.第一篇就写一写看似简单但又经常不注意到的meta标签吧.(博主经验尚浅,有许多理解不 ...

  4. 前端自动化学习笔记(一)——Yeoman,bower,Grunt的安装

    最近看视频学习了前端自动化的一些知识,确实让我大开眼界.感觉前端越来越神器了.同时跟着视频自己也尝试运用了一些工具去构建前端项目,但是中间遇见了很多坑,磕磕绊绊的才实现了一点功能,所以打算记录一下学习 ...

  5. SQL中数据类型转换

    CAST 和 CONVERT 将某种数据类型的表达式显式转换为另一种数据类型.CAST 和 CONVERT 提供相似的功能. 语法 使用 CAST: CAST ( expression AS data ...

  6. jQuery渐隐渐出的文字提示

    <html> <head> <title>jquery渐隐渐出的文字提示</title> <style type="text/css&q ...

  7. DataTable中执行DataTable.Select("条件"),

    我们在使用Sql ******这些数据库时,可以轻松的通过Sum.Aver.Count等统计出相关结果,那么,在已经把数据检索出来的DataSet(DataTable)中呢?特别是通过Web Serv ...

  8. title:EL表达式获取Map里面的数值失败的问题

    在控制器中定义了一个Map<Integer,String>集合,看似没有问题,将这个集合的对象map传递到一个JSP页面中,我们都知道,用EL表达式 ${map[key]}就可以取得key ...

  9. EventBus通信

    需求: 1.ActivityA打开ActivityB 2.在B中执行某操作后,同时执行A中的方法 lib下载:eventbus-2.4.0.jar  jmmy 1.在EventBusTestActiv ...

  10. 轻松搞定javascript原型链 _proto_

    //如有错误或不同观点,欢迎批评与讨论! 首先,prototype出现的目的,是为了解决 代码重用 的问题 , prototype 相当于是在内存上划分出一个公共的区域, 专用于存放 实例化对象 的相 ...