题目链接:http://codeforces.com/problemset/problem/379/D

【题目大意】

告诉你初始字符串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 <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
using namespace std;
long long fib[];
int k,x,n,m,aa,bb;
char ansa[],ansb[];
int calc(int s1,int s2)
{
int a=s1,b=s2,t;
int ca=,cb=,sum=;
for (int i=;i<=k;++i)
{
sum=ca+cb;
if ((a&)&&!(a&)&&(b&)&&(b&)) ++sum;
t=b,b=(a&(<<))|(b&);
a=t,t=cb,cb=sum,ca=t;
}
return sum;
}
bool product(int t,int code,char *p,int len)
{
int pp=;
if (len== && ((code>>)&)!=(code &)) return false;
if (len== && code==) --t;
if ((code>>)==) p[]='C'; else
if ((code>>)==) p[]='A'; else p[]='B';
if ((code>>)== && t && len>) --t,p[pp++]='C';
while (pp<len- && t) p[pp++]='A',p[pp++]='C',--t;
p[len]='\0';
if ((code&)==) p[len-]='C'; else
if ((code&)==) p[len-]='A'; else p[len-]='B';
if (pp<=len- && p[len-]=='C' && t) --t,p[len-]='A',--len;
while (pp<len-) p[pp++]='B';
if (t) return false;
return true;
}
bool calc2(int ret)
{
int p=x-ret;
for (int a=;a<=;++a)
{
int pp=p-a*fib[k-];
if (pp%fib[k-]!=) continue;
if (product(a,aa,ansa,n) && product(pp/fib[k-],bb,ansb,m))
return true;
}
return false;
}
int main()
{
fib[]=;fib[]=fib[]=;
for (int i=;i<=;++i) fib[i]=fib[i-]+fib[i-];
while (~scanf("%d%d%d%d",&k,&x,&n,&m))
{
bool fla=true;
for (aa=;aa< && fla;++aa)
for (bb=;bb<;++bb)
{
if (calc2(calc(aa,bb)))
{printf("%s\n%s\n",ansa,ansb),fla=false;break;}
}
if (fla) puts("Happy new year!");
}
}

【PS】

基本上半年没写博客了,2014的第一篇~

元旦快乐!!

codeforces Good Bye 2013 379D New Year Letter的更多相关文章

  1. Good Bye 2013 A

    A. New Year Candles time limit per test 1 second memory limit per test 256 megabytes input standard ...

  2. codeforces Good bye 2016 E 线段树维护dp区间合并

    codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...

  3. Codeforces 379D - New Year Letter

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

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

  5. Codeforces:Good Bye 2018(题解)

    Good Bye 2018! 题目链接:https://codeforces.com/contest/1091 A. New Year and the Christmas Ornament 题意: 给 ...

  6. codeforces Good Bye 2015 B. New Year and Old Property

    题目链接:http://codeforces.com/problemset/problem/611/B 题目意思:就是在 [a, b] 这个范围内(1 ≤ a ≤ b ≤ 10^18)统计出符合二进制 ...

  7. Hello world,Hello 2014,Bye 2013

    序 想要写点什么的时候发现不知道写什么了,用一句话来总结2013的话,就是2013是既熟悉又陌生的一年,熟悉是同样的开发工作,陌生是从河南到北京的环境改变,当时大学的卧谈会,谈论毕业了要做什么,想要在 ...

  8. Codeforces Good Bye 2015 D. New Year and Ancient Prophecy 后缀数组 树状数组 dp

    D. New Year and Ancient Prophecy 题目连接: http://www.codeforces.com/contest/611/problem/C Description L ...

  9. Codeforces Good Bye 2015 C. New Year and Domino 前缀和

    C. New Year and Domino 题目连接: http://www.codeforces.com/contest/611/problem/C Description They say &q ...

随机推荐

  1. SSH下载的方法2

    ------------------------------下作下载方法二---------------------------------------------------String msg=n ...

  2. Kettle 创建 Transformation

    1.第一步,先准备数据和工具 安装好mysql以及客户端工具 数据: USE `test`; CREATE TABLE `account` (   `id` int(11) NOT NULL AUTO ...

  3. jquery 更换皮肤

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. EXTJS AJAX解析XML数据

    public String getAllAreaInfos() { try { List<Areainfo> list = null; if(areaName!=null&& ...

  5. Android调用相机并将照片存储到sd卡上

    Android中实现拍照有两种方法,一种是调用系统自带的相机,然后使用其返回的照片数据. 还有一种是自己用Camera类和其他相关类实现相机功能,这种方法定制度比较高,洗染也比较复杂,一般平常的应用只 ...

  6. wp8 在OnBackKeyPress事件中调用MessageBox.Show()崩溃

    今天写代码的时候遇到一个问题,在wp8中执行下面的代码后,弹出对话框后,停滞一段时间程序退出. protected override void OnBackKeyPress(CancelEventAr ...

  7. bzoj 2435: [Noi2011]道路修建 树上 dp

    2435: [Noi2011]道路修建 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pr ...

  8. TP复习14

    ## ThinkPHP 3.1.2 控制器的模块和操作#讲师:赵桐正微博:http://weibo.com/zhaotongzheng 本节课大纲:一.空模块和空操作 1.空操作 function _ ...

  9. [每日一题] OCP1z0-047 :2013-08-26 TIMESTAMP WITH LOCAL TIME ZONE....................112

    正确答案:C 使用TIMESTAMP WITH LOCAL TIME ZONE数据类型,用户插入数据时,oracle将会把用户的数据结合用户session的时区信息自动换算成数据库设定的时区的时间进行 ...

  10. 【Java基础】Java面试题目整理与解说(二)

    1.Collection 和 Collections 的差别. Collection 是集合类的上级接口,继承于他的接口主要有 Set 和 List. Collections 是针对集合类的一个帮助类 ...