题目链接: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. Eclipse 和 NetBeans 快捷键即其他常用功能比较

    按: 自己用 Eclipse, 常用的也就这些功能, 在用 NetBeans 时, 有些不顺手, 因此列表如下. Eclipse和NetBeans常用快捷键对比:  功能  Eclipse     N ...

  2. 关键词权重计算算法:TF-IDF

    TF-IDF(Term Frequency–Inverse Document Frequency)是一种用于资讯检索与文本挖掘的常用加权技术.TF-IDF是一种统计方法,用以评估一字词对于一个文件集或 ...

  3. C++标准库概述 [转]

    C++标准库的所有头文件都没有扩展名. C++标准库的内容总共在50个标准头文件中定义,其中18个提供了C库的功能.<cname>形式的标准头文件[<complex>例外]其内 ...

  4. VC、MFC中设置控件的背景色、标题、字体颜色、字体要注意的地方[转]

    在MFC中设置控件的背景色.字体.字体颜色.标题等属性主要是利用OnCtlColor函数来实现. 如: HBRUSH CAlarm::OnCtlColor(CDC* pDC, CWnd* pWnd, ...

  5. SQL Server 批量插入数据的两种方法(转)

    此文原创自CSDN TJVictor专栏:http://blog.csdn.net/tjvictor/archive/2009/07/18/4360030.aspx 在SQL Server 中插入一条 ...

  6. spring-flex

    mvn install:install-file -Dpackaging=jar -DgroupId=com.adobe.blazeds -Dversion=4.0.0.14931 -Dartifac ...

  7. Mailbox and Mail

    #ifndef __MAILBOX_H__ #define __MAILBOX_H__ #include <stdint.h> #include <stdlib.h> #inc ...

  8. SVN备份批处理文件

    SVN备份批处理文件,亲测可用 另外,备份文件时获取文件名%%~ni 可改为%%~nxi,以避免文件名中有“.”号时,读取不完成,将.后面的当作后缀名 需要使用hotcopy 时,可以将关键代码进行相 ...

  9. freemarker中的round、floor和ceiling数字的舍入处理

    freemarker中的round.floor和ceiling数字的舍入处理 1.简易说明 (1)round:四舍五入 (2)floor:向下取整 (3)ceiling:向上取整 2.举例说明 < ...

  10. window.onload与$.ready的差别

    在做图书管理系统的时候.实用到window.onload(){}方法.可是遇到了一个问题.就是怎么都不运行,究竟是为什么呢?愁了半天.后来经师姐指点改用了$.ready(){}. 在我的浅浅的了解中觉 ...