HDU2044 一只小蜜蜂...

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2044

递推:

到1的方法数为1, 到2的方法数为1

到3的方法数 = 1的方法数 + 2的方法数  (只能有1或2到达3)

递推关系式:f[n] = f[n-1] + f[n-2];

给出a, b 求a到达b的方法数

可以转化为 以1为起点  例如:3到6 可以转化为 1到4。证明比较简单,因为是等价的。

AC代码:

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <bitset>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <algorithm>
#include <sstream>
#include <stack>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define FO freopen("in.txt", "r", stdin);
#define lowbit(x) (x&-x)
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
const int inf = 0x3f3f3f3f;
ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
template <class T>
inline bool scan_d(T &ret)
{
    char c; int sgn;
    if (c = getchar(), c == EOF) return 0; //EOF
    while (c != '-' && (c < '0' || c > '9')) c = getchar();
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return 1;
}
inline void out(ll x)
{
    if (x > 9) out(x / 10);
    putchar(x % 10 + '0');
}

ll f[60];
int main() {
	int _, a, b;
	f[1] = 1;//初始化 
	f[2] = 1;
	rep(i, 3, 60) {//递推 
		f[i] = f[i-1] + f[i-2];
	}
	for(scan_d(_);_;_--) {
		scan_d(a);
		scan_d(b);
		out(f[b-(a-1)]);//转化 
		printf("\n");
	}
} 

 

posted @ 2018-07-28 10:50  Frontierone  阅读(113)  评论(0编辑  收藏  举报