codeforces Good Bye 2013 379D New Year Letter
题目链接: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的更多相关文章
- Good Bye 2013 A
A. New Year Candles time limit per test 1 second memory limit per test 256 megabytes input standard ...
- codeforces Good bye 2016 E 线段树维护dp区间合并
codeforces Good bye 2016 E 线段树维护dp区间合并 题目大意:给你一个字符串,范围为‘0’~'9',定义一个ugly的串,即串中的子串不能有2016,但是一定要有2017,问 ...
- Codeforces 379D - New Year Letter
原题地址:http://codeforces.com/contest/379/problem/D 题目大意:给出一种生成字符串的方法 s[k] = s[k - 2] + s[k - 1],其中加法意为 ...
- 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. ...
- Codeforces:Good Bye 2018(题解)
Good Bye 2018! 题目链接:https://codeforces.com/contest/1091 A. New Year and the Christmas Ornament 题意: 给 ...
- codeforces Good Bye 2015 B. New Year and Old Property
题目链接:http://codeforces.com/problemset/problem/611/B 题目意思:就是在 [a, b] 这个范围内(1 ≤ a ≤ b ≤ 10^18)统计出符合二进制 ...
- Hello world,Hello 2014,Bye 2013
序 想要写点什么的时候发现不知道写什么了,用一句话来总结2013的话,就是2013是既熟悉又陌生的一年,熟悉是同样的开发工作,陌生是从河南到北京的环境改变,当时大学的卧谈会,谈论毕业了要做什么,想要在 ...
- 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 ...
- 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 ...
随机推荐
- [Netbeans]为面板设置背景图片
与AndroidStudio等类似IDE不同,在Netbeans开发桌面程序时,不可以直接通过src=@drawable 等方法为窗口设置背景图片.这里介绍一种简便的方法: 1:首先,拖动一个面板到f ...
- flex中DataGrid里使用itemRenderer后数据无法绑定到数据源的问题
<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="ht ...
- Hadoop on Mac with IntelliJ IDEA - 6 解决KeyValueTextInputFormat读取时只有key值问题
本文讲述使用KeyValueTextInputFormat在Hadoop 0.x正常工作.Hadoop 1.2.1失效的解决过程. 环境:Mac OS X 10.9.5, IntelliJ IDEA ...
- 神秘常量复出!用0x077CB531计算末尾0的个数 -- De Bruijn 序列
http://www.matrix67.com/blog/archives/3985 神秘常量复出!用0x077CB531计算末尾0的个数 大家或许还记得 Quake III 里面的一段有如天书般的代 ...
- FluorineFx 播放FLV 时堆棧溢出解决 FluorineFx NetStream.play 并发时,无法全部连接成功的解决办法
http://25swf.blogbus.com/tag/FluorineFx/ http://www.doc88.com/p-7002019966618.html 基于Red5的视频监控系统的研究 ...
- 【JavaScript】常用的JS
JQuery.UnderScoreJS.Zepto 原生JS等
- NBUT 1225 NEW RDSP MODE I
找出循环周期即可了 #include<bits/stdc++.h> using namespace std; int N,M,X; int time(int x,int y,int z) ...
- 一行命令搞定VS2012无法安装cocos2d-x-2.1.4及创建跨平台项目(二)
转自:http://blog.csdn.net/yangjingui/article/details/9418843 由于上次发了一个比较二的方法来解决VS2012无法安装cocos2d-x-2.1. ...
- 终端I/O之stty命令
所有的终端选项标志,在程序中都可用tcgetattr和tcsetattr函数(http://www.cnblogs.com/nufangrensheng/p/3576682.html)进行检查和更改. ...
- PHP.12-PHP-设计文件上传类
设计文件上传类 [PHP参数详解]{文件上传} ********************** *#构造方法编写 ********************** 此种传参方法规定必须用户必须按响应位置输入 ...