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!

题意:转自 http://www.cnblogs.com/wuminye/p/3500422.html

【题目大意】

告诉你初始字符串S1、S2的长度和递推次数k, 使用类似斐波纳契数列的字符串合并的递推操作,使得最后得到的字符串中刚好含有x个"AC",现在要你构造出满足条件的S1和S2。

【分析】

最终结果中有些"AC"可能是应为在合并时一个字符串的尾部和另一个字符串的首部合并而成,这就跟原始字符串的首尾字符有关,不同的情况在K次递推后多产生的"AC"数是不同的,所以这里既要枚举下初始串的首尾字符,计算出因合并产生的"AC"数sum有多少。

现在可以忽略合并产生的"AC"了,假设S1中有a个"AC",S2中有b个"AC",则经过k次递推由这些"AC"组合成的"AC"数量为:fib[k-2]*a+fib[k-1]*b。

所以最终的结果为:

f[k]=fib[k-2]*a+fib[k-1]*b+sum;

f[k]=x 已知,sum可以枚举获得 ,于是只需枚举a 即可知道 a和b 的值,对于一组 a,b值看能否构造出符合条件的字符串就好了。

其实可以不用枚举a,用不定方程来解就好了,当a,b很大时速度更快。

 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 105
#define M 15
#define mod 1000000007
#define mod2 100000000
#define ll long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int k,n,m;
ll f[N];
int l[N];
int r[N];
ll z;
ll x;
void ini()
{
//k=0;
//memset(next,0,sizeof(next));
// memset(nexta,0,sizeof(nexta));
} void solve()
{ } int main()
{
int i,j;
int a,b,c,d;
int o,p;
//freopen("data.in","r",stdin);
// scanf("%d",&T);
// for(int cnt=1;cnt<=T;cnt++)
//while(T--)
while(scanf("%d%I64d%d%d",&k,&x,&n,&m)!=EOF)
{
for(i=;i<=n/;i++)
{
for(j=;j<=m/;j++)
{
for(a=;a<;a++)
for(b=;b<;b++)
for(c=;c<;c++)
for(d=;d<;d++)
{
if (i * + a + b > n||j * + c + d > m)continue;
f[]=i;f[]=j;f[]=i+j;l[]=c;r[]=d;
if(b== && c==) f[]++;
l[]=a;r[]=d;
for(z=;z<=k;z++){
f[z]=f[z-]+f[z-];
l[z]=l[z-];
r[z]=r[z-];
if(r[z-]== && l[z-]==) f[z]++;
} // printf(" i=%d %d %d %d %d d=%d f=%I64d\n",i,j,a,b,c,d,f[k]);
if(f[k]==x){
if(a==) printf("C");
for(o=;o<=i;o++){
printf("AC");
}
for(p=a+i*;p<n-b;p++){
printf("M");
}
if(b==) printf("A");
printf("\n"); if(c==) printf("C");
for(o=;o<=j;o++){
printf("AC");
}
for(p=c+j*;p<m-d;p++){
printf("M");
}
if(d==) printf("A");
printf("\n");
return ;
}
}
}
}
printf("Happy new year!\n");
//ini();
// solve();
// printf("%I64d\n",ans);
} return ;
}

CodeForces 379D 暴力 枚举的更多相关文章

  1. Codeforces Round #349 (Div. 1) B. World Tour 最短路+暴力枚举

    题目链接: http://www.codeforces.com/contest/666/problem/B 题意: 给你n个城市,m条单向边,求通过最短路径访问四个不同的点能获得的最大距离,答案输出一 ...

  2. Codeforces Round #298 (Div. 2) B. Covered Path 物理题/暴力枚举

    B. Covered Path Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/534/probl ...

  3. Codeforces 425A Sereja and Swaps(暴力枚举)

    题目链接:A. Sereja and Swaps 题意:给定一个序列,能够交换k次,问交换完后的子序列最大值的最大值是多少 思路:暴力枚举每一个区间,然后每一个区间[l,r]之内的值先存在优先队列内, ...

  4. CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)

    题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000 ...

  5. D. Diverse Garland Codeforces Round #535 (Div. 3) 暴力枚举+贪心

    D. Diverse Garland time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  6. Codeforces Round #266 (Div. 2)B(暴力枚举)

    很简单的暴力枚举,却卡了我那么长时间,可见我的基本功不够扎实. 两个数相乘等于一个数6*n,那么我枚举其中一个乘数就行了,而且枚举到sqrt(6*n)就行了,这个是暴力法解题中很常用的性质. 这道题找 ...

  7. Codeforces Round #253 (Div. 2)B(暴力枚举)

    就暴力枚举所有起点和终点就行了. 我做这题时想的太多了,最简单的暴力枚举起始点却没想到...应该先想最简单的方法,层层深入. #include<iostream> #include< ...

  8. Codeforces Round #325 (Div. 2) B. Laurenty and Shop 有规律的图 暴力枚举

    B. Laurenty and Shoptime limit per test1 secondmemory limit per test256 megabytesinputstandard input ...

  9. Gym 101194L / UVALive 7908 - World Cup - [三进制状压暴力枚举][2016 EC-Final Problem L]

    题目链接: http://codeforces.com/gym/101194/attachments https://icpcarchive.ecs.baylor.edu/index.php?opti ...

随机推荐

  1. iOS 随机数(Fixed)

    ios 有如下三种随机数方法: 1.    srand((unsigned)time(0));  //不加这句每次产生的随机数不变         int i = rand() % 5; 2.     ...

  2. JS 、JQ 获取宽高总结 & JS中getBoundingClientRect的作用及兼容方案

    1.getBoundingClientRect的作用 getBoundingClientRect用于获取某个html元素相对于视窗的位置集合.   执行 object.getBoundingClien ...

  3. 八 个优秀的 jQuery Mobile 教程

    jQuery Mobile 是 jQuery 在手机上和平板设备上的版本.jQuery Mobile不仅会给主流移动平台带来jQuery核心库,而且会发布一个完整统一的jQuery移动UI框架.虽然j ...

  4. 电商技术中企业数据总线ESB和注册服务管理的区别

    一.概述 1.什么是ESB 就是企业数据总线的意思,他的核心功能就是兼容各种协议接口,可以将数据在各种协议之间进行流转,并且可以针对数据格式进行编排转换. 异构系统,功能繁多,复杂 代表性的项目有:J ...

  5. windows2012安装sql2012报错

    一. 处理

  6. Re:从零开始的Linux之路(基础篇)

    基于 Red Hat Enterprise Linux 7.5 或者 CentOS 7.4 Linux的命令一定遵循以下格式:command指令  [-options]选项  parameter1参数 ...

  7. Ajax跨域问题---jsonp

    跨域:跨域名  一个域名下的文件去请求了和他不一样的域名下资源文件,那么就会产生跨域请求 解决跨域问题办法: 1.将要访问的外部资源存到本域名下的一个php文件 2.用flash方式 3.JSONP: ...

  8. Python中怎么进行单元测试

    既然是测试,那我们得有被测试的代码,我们先定义一个简单的函数,这个函数的功能就是接收一个姓名,并返回一句问候语句. say_hello_function.py def hello_name(name) ...

  9. PAT Basic 1062

    1062 最简分数 一个分数一般写成两个整数相除的形式:N/M,其中 M 不为0.最简分数是指分子和分母没有公约数的分数表示形式. 现给定两个不相等的正分数 N​1​​/M​1​​ 和 N​2​​/M ...

  10. yum安装的java配置

    vim /etc/profile JAVA_HOME=/usr/lib/jvm/javaJRE_HOME=$JAVA_HOME/jreCLASS_PATH=.:$JAVA_HOME/lib/dt.ja ...