题目链接: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. poj 3134 Power Calculus(IDA*)

    题目大意: 用最小的步数算出  x^n 思路: 直接枚举有限步数可以出现的所有情况. 然后加一个A*   就是如果这个数一直平方  所需要的步骤数都不能达到最优   就剪掉 #include < ...

  2. C# 使用ping命令

    方法一:调用cmd 的ping命令 private static string CmdPing(string strIp) { Process p = new Process(); p.StartIn ...

  3. OC:继承、初始化方法、便利构造器

    A继承 的作用就是为了,节省代码.     继承 :子类会继承父类 里面所有的内容     思想 :凡是继承过来的实例变量和方法 都是自己的(是从父类遗传过来的)     继承的使用场景 : 当我们多 ...

  4. as自定义菜单。

    与菜单相关的类一共有3个 ContextMenu类 ContextMenuBuiltInItems类 //与系统内置菜单相关的类 ContextMenuItem类 //与用户自定义菜单相关的类

  5. LVS 单独完成--负载均衡

    原文地址:http://blog.sina.com.cn/s/blog_5f54f0be0101eyiu.html LVS 是通过 IPVS 模块来实现的.IPVS是LVS集群的核心,主要用于完成用户 ...

  6. Hadoop on Mac with IntelliJ IDEA - 8 单表关联NullPointerException

    简化陆喜恒. Hadoop实战(第2版)5.4单表关联的代码时遇到空指向异常,经分析是逻辑问题,在此做个记录. 环境:Mac OS X 10.9.5, IntelliJ IDEA 13.1.5, Ha ...

  7. Freescale OSBDM JM60仿真器

    OSBDM-JM60 - 9S08JM60 Based OSBDM — It includes interfaces and firmware applied to all the targets s ...

  8. J2534 Pass-Thru Vehicle Programming ( SAE J1962 connector and Protocol )

    SAE J1962—Diagnostic Connector SAE J1850—Class B Data Communications Network Interface SAE J1939—Tru ...

  9. C#读写者线程(用AutoResetEvent实现同步)

    转载自 http://blog.csdn.net/livelylittlefish/article/details/2735440 本博客(http://blog.csdn.net/livelylit ...

  10. Linux基础教程之/dev/null和/dev/zero的区别及其用法

    在Linux操作系统中/dev/null和/dev/zero是两个相似却又很特殊的文件,特别是在shell脚本开发和系统运维过程中会经常用这两个文件,因此作为Linux系统工程师,必须了解这两个文件的 ...