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 ...
随机推荐
- C++ 中 int,char*,string,CString之间相互转换-整理
<多字符集下> #include <string> //使用C++标准库的string类时, 定义时 std::string str; using namespace std; ...
- Oracle 触发器在日志管理开发中的应用
摘要: 本文讨论了利用数据库中的触发器对日志管理进行设计与实现的方法, 是对原来在客户端软件中编写日志管理方法的一种改进, 并给出了 Oracle9i 中的实例演示.关键词: Oracle; 触发器; ...
- Nginx 的 RTMP 模块的在线统计功能 stat 在 multi-worker 模式下存在 Bug
< 让你的 Nginx 的 RTMP 直播具有统计某频道在线观看用户数量的功能>一文介绍了 Nginx 的在线统计模块. 我们的在线直播服务使用了 Nginx 的 Rtmp ...
- Hadoop ecosystem
How did it all start- huge data on the web! Nutch built to crawl this web data Huge data had to save ...
- Java多线程技术学习笔记(二)
目录: 线程间的通信示例 等待唤醒机制 等待唤醒机制的优化 线程间通信经典问题:多生产者多消费者问题 多生产多消费问题的解决 JDK1.5之后的新加锁方式 多生产多消费问题的新解决办法 sleep和w ...
- 关于form.submit()不能提交表单的错误原因
来源:http://www.ido321.com/948.html 直接上代码把: 1: <div id="register"> 2: <h4>会员注冊&l ...
- javascript实现继承的方式
this this表示当前对象,如果在全局作用范围内使用this,则指代当前页面对象window: 如果在函数中使用this,则this指代什么是根据运行时此函数在什么对象上被调用. 我们还可以使用a ...
- 惊叹jQuery(解决jQuery对象到DOM的转换)
jQuery是一个javascript框架,但绝对不是通常意义上的一些包装,个人感觉是一个改变js控制方式的框架.我们可以像美工通过写css分离页面代码一样,通过jQuery来分离页面与效果..下面转 ...
- 用AJAX技术聚合RSS
有时候,你的Blog可能需要这样的功能: 在自己Blog上聚合并显示朋友Blog的最新文章,这样方便自己及时了解朋友的消息,另外,也方便访问者找到和本Blog相关的blog和文章. 这个功能你可以叫它 ...
- QML学习笔记之二
//必须要导入,否则以下元素将无效 import QtQuick 1.1 //对象一:矩形Rectangle Rectangle { width: 798 height: 111 //整个Rectan ...