zoukankan      html  css  js  c++  java
  • #YBT整理 递推(一)

    递推例题及答案

    重点题目:

    1、P1197 :山区建小学

    2、P1195 :判断整除

    3、P1192 :放苹果

    4、P1312 :【例3.4】昆虫繁殖

    5、P1313 :【例3.5】位数问题

    6、P1188 :菲波那契数列(2)

    P1312 :【例3.4】昆虫繁殖

    题目描述

    科学家在热带森林中发现了一种特殊的昆虫,这种昆虫的繁殖能力很强。每对成虫过x个月产y对卵,每对卵要过两个月长成成虫。假设每个成虫不死,第一个月只有一对成虫,且卵长成成虫后的第一个月不产卵(过x个月产卵),问过z个月以后,共有成虫多少对? 0≤x≤20,1≤y≤20,X≤z≤50。

    【输入】

    x,y, z的数值。

    【输出】

    过z个月以后,共有成虫对数

    【输入样例】

    1 2 8
    

    【输出样例】

    3 7
    

    思路

    用两个数组a,b分别记录每月新增幼虫、现有成虫的数量,我们可以得到递推式

    [a[i] = b[i-x] * y\ b[i] = b[i-1] + a[i - 2] ]

    代码

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <string>
    #include <cmath>
    #define int long long
    using namespace std;
    int x,y,z;
    int a[100000],b[1000000];
    signed main(){
        cin >> x >> y >> z;
        for(int i = 0;i < x; i++) b[i] = 1;
        for(int i = x;i <= z+1; i++){
            a[i] = b[i-x] * y;
            b[i] = b[i-1] + a[i - 2];
        }
        cout << b[z] << endl;
        return 0;
    }
    

    P1313 :【例3.5】位数问题

    题目描述

    在所有的N位数中,有多少个数中有偶数个数字3?由于结果可能很大,你只需要输出这个答案对12345取余的值。

    【输入】

    读入一个数N(N < 1000)。

    【输出】

    输出有多少个数中有偶数个数字3。

    【输入样例】

    2
    

    【输出样例】

    73
    

    思路

    两个数组,分别记录n位数以内,有奇数个3的数的个数(a),有偶数个3的个数(b)。
    即可得到递推式

    [a[i] = b[i-1] * 9 + a[i-1] \ b[i] = a[i-1] * 9 + b[i-1]; ]

    代码

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #define MOD 12345
    using namespace std;
    int n;
    int a[100000],b[100000];
    int main(){
        cin >> n;
        a[1] = 8;
        b[1] = 1;
        for(int i = 2;i <= n; i++){
            a[i] = (a[i-1] * 9 + b[i-1])% MOD;
            b[i] = (b[i-1] * 9 + a[i-1]) % MOD;
        }
        cout << a[n] << endl;
        return 0;
    }
    

    P1314:【例3.6】过河卒(Noip2002)

    题目描述

    棋盘上A点有一一个过河卒,需要走到目标B点。卒行走的规则:可以向下、或者向右。同时在棋盘上的某一点有一 一个对方的马(如C点), 该马所在的点和所有跳跃一步可达的点称为对方马的控制点,如图3一1中的C点和P1, ..... P8,卒不能通过对方马的控制点。棋盘用坐标表示,A点(0,0)、 B点(n, m) (n,m为不超过20的整数),同样马的位置坐标是需要给出的,C+A且C≠B。现在要求你计算出卒从A点能够到达B点的路径的条数。

    【输入】

    给出n、m和C点的坐标。

    【输出】

    从A点能够到达B点的路径的条数。

    【输入样例】

    8 6 0 4
    

    【输出样例】

    1617
    

    思路

    用一个二维数组存储到任意点的路径条数,只要是和当前点距离(sqrt 5)的点都加上就行。

    当时为什么没ac?因为没开long long。。

    代码

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstdio>
    #include <cmath>
    using namespace std;
    long long n,m;
    long long x,y;
    long long f[100][100];
    bool PD(long long xx,long long yy){
        long long dis = (xx - x)*(xx - x) + (yy - y)*(yy - y);
        if(dis == 5 || dis == 0) return 0;
        else return 1;
    }
    int main(){
        cin >> n >> m;
        f[0][0] = 1;
        cin >> x >> y;
        for(long long i = 0;i <= n; i++){
            for(long long j = 0;j <= m; j++){
                // if(!PD(i,j)) cout << "(" << i << "," << j << ")
    ";
            }
        }
        for(long long i = 1;i <= n; i++) if(PD(i,0))f[i][0] = f[i-1][0];else f[i][0] = 0;
        for(long long i = 1;i <= m; i++) if(PD(0,i))f[0][i] = f[0][i-1];else f[0][i] = 0;
    
        for(long long i = 1;i <= n; i++){
            for(long long j = 1;j <= m; j++){
                if(PD(i,j)){
                    f[i][j] = f[i][j-1] + f[i-1][j];
                }else f[i][j] = 0;
        }}
        for(long long i = 0;i <= n; i++){
            for(long long j = 0;j <= m; j++){
                // cout << f[i][j] << "	";
            }
            // cout << endl;
        }
        cout << f[n][m] << endl;
        return 0;
    }
    

    P1188 :菲波那契数列(2)

    题目描述

    菲波那契数列是指这样的数列:数列的第一一个和第二个数都为1,接下来每个数都等于前面2个数之和。给出一一个正整数a,要求菲波那契数列中第a个数对1000取模的结果是多少。

    【输入】

    第1行是测试数据的组数n,后面跟着n行输入。每组测试数据占1行,包括一个正整数a(1≤a≤100000)。

    【输出】

    n行,每行输出对应一一个输入。输出应是一个正整数,为菲波那契数列中第a个数对1000取模得到的结果。

    【输入样例】

    4
    5
    2
    19
    1
    

    【输出样例】

    5
    1
    181
    1
    

    思路

    这个题我巩固了一下矩阵乘法。

    代码

    #include "iostream"
    #include "cstdio"
    #include "algorithm"
    #include "cstring"
    #include "string"
    #include "cmath"
    #define int long long
    using namespace std;
    
    const int p = 1000;
    int n,a,b;
    
    struct matrix{
    	int a[4][4]={};
    	void print(){
    		for(int i = 0;i < 2; i++){
    			for(int j = 0;j < 2; j++){
    				cout << a[i][j] << ' ';
    			}
    			cout << endl;
    		}
    	} 
    }in,e;
    
    matrix operator *(matrix &a,matrix &b){
    		matrix c;
    		for(int i = 0;i < 2; i++){
    			for(int j = 0;j < 2; j++){
    				for(int k = 0;k < 2; k++){
    					(c.a[i][j] += a.a[i][k] * b.a[k][j] %p) %=p;
    				}
    			}
    		}
    		return c;
    }
    
    matrix Pow(matrix a,int x){
    	matrix tot = e;
    	while(x){
    		if(x & 1) tot = a * tot;
    		a = a * a;
    		x >>= 1;
    	}
    	return tot;
    }
    
    signed main(){
        int t;
        cin >> t;
        while(t--){
            cin >> n ;
            a = 1,b = 1; 
            
            e.a[0][0] = 1;e.a[0][1] = 1;
            
            in.a[0][0] = 0; in.a[0][1] = b;
            in.a[1][0] = 1; in.a[1][1] = a;
    
            matrix ans = Pow(in,n);
            
            int x = ans.a[1][1];
            cout << x << endl;
            }
    	return 0;
    }
    
  • 相关阅读:
    消息队列优缺点及各种MQ对比
    反射详解
    Tomcat线程模型及调优
    Tomcat结构及类加载机制
    Spring AOP
    Spring IOC
    Spring介绍
    SpringMVC介绍
    Mybatis介绍
    Ajax笔记(一)
  • 原文地址:https://www.cnblogs.com/Cao-Yucong/p/12468214.html
Copyright © 2011-2022 走看看