UVA 11000- Bee 递推
In Africa there is a very special species of bee. Every year, the female bees of such species give birth
to one male bee, while the male bees give birth to one male bee and one female bee, and then they die!
Now scientists have accidentally found one “magical female bee” of such special species to the effect
that she is immortal, but still able to give birth once a year as all the other female bees. The scientists
would like to know how many bees there will be after N years. Please write a program that helps them
find the number of male bees and the total number of all bees after N years.
Input
Each line of input contains an integer N (≥ 0). Input ends with a case where N = −1. (This case
should NOT be processed.)
Output
Each line of output should have two numbers, the first one being the number of male bees after N
years, and the second one being the total number of bees after N years. (The two numbers will not
exceed 232.)
Sample Input
1
3
-1
Sample Output
1 2
4 7
题意: 蜜蜂,每年每只雄蜂产下一只雌蜂和一只雄蜂,每只雌蜂产下一只雄蜂,然后就死去, 现在发现了一只不会死的雌蜂,问以她为起始点,第N年有多少雄蜂和一共多少蜜蜂。
题解: 设第i年的雄蜂和雌蜂分别为a(i)与s(i),则有如下递推关系:
1 s(i)= a(i-1)+ 1
2 a(i)= s(i-1)+ a(i-1)
整理得:a(i)=a(i-1)+ a(i-2)+ 1;
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include<vector>
#include <map>
using namespace std ;
typedef long long ll; const int N=+;
const int maxn = 1e9 + ; ll a[N];
void init() {
a[] = ;
a[] = ;
for(int i=;i<=N;i++) a[i] = a[i-] + a[i-] +1ll;
}
int main() {
init();
int n;
while(~scanf("%d",&n)) {
if(n == -) break;
printf("%lld %lld\n", a[n] , a[n+]);
}
return ;
}
代码
UVA 11000- Bee 递推的更多相关文章
- UVa 10520【递推 搜索】
UVa 10520 哇!简直恶心的递推,生推了半天..感觉题不难,但是恶心,不推出来又难受..一不小心还A了[]~( ̄▽ ̄)~*,AC的猝不及防... 先递推求出f[i][1](1<=i< ...
- Uva 10446【递推,dp】
UVa 10446 求(n,bcak)递归次数.自己推出来了一个式子: 其实就是这个式子,但是不知道该怎么写,怕递归写法超时.其实直接递推就好,边界条件易得C(0,back)=1.C(1,back)= ...
- UVa 10943 (数学 递推) How do you add?
将K个不超过N的非负整数加起来,使它们的和为N,一共有多少种方法. 设d(i, j)表示j个不超过i的非负整数之和为i的方法数. d(i, j) = sum{ d(k, j-1) | 0 ≤ k ≤ ...
- UVa 557 (概率 递推) Burger
题意: 有两种汉堡给2n个孩子吃,每个孩子在吃之前要抛硬币决定吃哪一种汉堡.如果只剩一种汉堡,就不用抛硬币了. 求最后两个孩子吃到同一种汉堡的概率. 分析: 可以从反面思考,求最后两个孩子吃到不同汉堡 ...
- UVa 1645 Count (递推,数论)
题意:给定一棵 n 个结点的有根树,使得每个深度中所有结点的子结点数相同.求多棵这样的树. 析:首先这棵树是有根的,那么肯定有一个根结点,然后剩下的再看能不能再分成深度相同的子树,也就是说是不是它的约 ...
- Coin Toss(uva 10328,动态规划递推,限制条件,至少转至多,高精度)
有n张牌,求出至少有k张牌连续是正面的排列的种数.(1=<k<=n<=100) Toss is an important part of any event. When everyt ...
- UVA - 11021 - Tribles 递推概率
GRAVITATION, n.“The tendency of all bodies to approach one another with a strengthproportion to the ...
- 紫书 习题 10-10 UVa 1645(递推)
除了根节点以外,有n-1个节点,然后就看n-1的因数有那些,所有因数加起来(递推)就好了. #include<cstdio> #define REP(i, a, b) for(int i ...
- 紫书 例题 9-4 UVa 116 ( 字典序递推顺序)
这道题在递推方式和那个数字三角形有一点相像,很容易推出来 但是这道题要求的是字典序,这里就有一个递推顺序的问题 这里用逆推,顺推会很麻烦,为什么呢? 如果顺推的话,最后一行假设有种情况是最小值,那么你 ...
- Uva 10074【递推dp】
UVa 10074 题意:求01矩阵的最大子0矩阵. http://www.csie.ntnu.edu.tw/~u91029/MaximumSubarray.html#2 这里说的很清楚.先求Larg ...
随机推荐
- linux下使用bcp备份sybase数据
命令|数据库名|表名|导出到|文件名|格式|sybase服务名|数据库登录名 |字符集 bcp dbname..tablename out filename -n -Sgfdmhk -UISISMan ...
- 全局变量变为局部变量 & MVC思想
1 函数中的全局变量如何变成局部变量? 全局变量之间会相互骚扰.所以在代码中不要用全局变量.ES6之前只有函数里面有全局变量. 全局变成局部变量怎么变? 把代-放在一个函数如中,再.call()执行一 ...
- Java数组操作工具
原文地址:http://blog.csdn.net/qq446282412/article/details/8913690 2013-05-11 10:27 看到网上的一段关于对数组操作的代码,觉 ...
- boost的单例模式
template <typename T> struct singleton_default { private: struct object_creator { ...
- DDoS攻击与防范策略
DDoS(Distributed Denial of Service,分布式拒绝服务)攻击的主要目的是让指定目标无法提供正常服务,甚至从互联网上消失,是目前最强大.最难防御的攻击之一. 按照发起的方式 ...
- 页面footer在底部
页脚动态贴在底部需要满足以下两个条件: 当主体的内容高度不超过可视区域高度的时候,页脚贴在页面底部. 当主体的内容高度超过可视区域高度的时候,页脚将按正常布局. 方法一:footer高度固定+绝对定位 ...
- 03--(二)编程实践中C语言的一些常见细节
编程实践中C语言的一些常见细节(转载) 对于C语言,不同的编译器采用了不同的实现,并且在不同平台上表现也不同.脱离具体环境探讨C的细节行为是没有意义的,以下是我所使用的环境,大部分内容都经过测试,且所 ...
- PhotoZoom Classic 7有什么用?高品质的放大模糊图片!
PhotoZoom Classic 7专门用于放大照片,同时保持质量.该软件配备了BenVista独特的S-Spline技术,可轻松超越Photoshop的双三次插值等替代解决方案. PhotoZoo ...
- Pyhton学习——Day1
1.什么是机器码?什么是字节码?机器码(machine code),学名机器语言指令,有时也被称为原生码(Native Code),是电脑的CPU可直接解读的数据. 通常意义上来理解的话,机器码就是计 ...
- WTM
WTM的由来 WalkingTec.Mvvm框架(简称WTM)最早开发与2013年,基于Asp.net MVC3 和 最早的Entity Framework, 当初主要是为了解决公司内部开发效率低,代 ...