Hướng dẫn giải của Trò chơi thỏ


Chỉ dùng lời giải này khi không có ý tưởng, và đừng copy-paste code từ lời giải này. Hãy tôn trọng người ra đề và người viết lời giải.
Nộp một lời giải chính thức trước khi tự giải là một hành động có thể bị ban.

Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này

Code mẫu của flashmt

import java.util.*;
import java.math.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();

        BigInteger[][] f = new BigInteger[555][555];
        f[0][0] = BigInteger.ONE;

        for (int i = 1; i <= n; i++)
            for (int j = 0; j <= i; j++)
            {
                f[i][j] = BigInteger.ZERO;
                if (j > 0 && j <= n - i) f[i][j] = f[i][j].add(f[i - 1][j - 1]);
                if (j < i) f[i][j] = f[i][j].add(f[i - 1][j]);
                if (j < i - 1)  f[i][j] = f[i][j].add(f[i - 1][j + 1].multiply(BigInteger.valueOf(j + 1)));
            }

        System.out.println(f[n][0]);
    }
}

Code mẫu của happyboy99x

import java.math.BigInteger;
import java.util.Scanner;

class Main {
    public static void main(String argv[]) throws Exception {
        int n = new Scanner(System.in).nextInt();
        BigInteger f[] = new BigInteger[n+1];
        f[0] = f[1] = BigInteger.ONE;
        for(int i = 2; i <= n; ++i)
            f[i] = f[i-1].add(f[i-2].multiply(BigInteger.valueOf(i-1)));
        System.out.println(f[n]);
    }
}

Code mẫu của ladpro98

program RABGAME;
uses    sysutils;
type    big=ansistring;
const   fi='';
        maxn=505;
var     f:array[0..maxn] of big;
        n,i:longint;
        inp:text;

function cong(a,b:big):big;
var     c:big;
        nho,s,i:longint;
begin
        nho:=0;c:='';
        while length(a)<length(b) do a:='0'+a;
        while length(b)<length(a) do b:='0'+b;
        for i:=length(a) downto 1 do begin
                s:=ord(a[i])+ord(b[i])+nho-96;
                c:=chr(s mod 10+48)+c;
                nho:=s div 10;
        end;
        if nho>0 then c:='1'+c;
        exit(c);
end;

function nhan(a:big; b:longint):big;
var     c:big;
        nho,i,s:longint;
begin
        c:='';nho:=0;
        for i:=length(a) downto 1 do begin
                s:=(ord(a[i])-48)*b+nho;
                nho:=s div 10;
                c:=chr(s mod 10+48)+c;
        end;
        if nho>0 then c:=inttostr(nho)+c;
        exit(c);
end;

begin
        assign(inp,fi);reset(inp);
        readln(inp,n);close(inp);
        f[0]:='1';f[1]:='1';
        for i:=2 to n do
        f[i]:=cong(nhan(f[i-2],i-1),f[i-1]);
        write(f[n]);
end.

Code mẫu của RR

import java.io.*;
import java.util.*;
import java.math.*;

public class Main
{
    public static void main(String[] args) {
        BigInteger[][] f = new BigInteger[511][511];
        Scanner sc = new Scanner(System.in);

        while (sc.hasNext()) {
            int n = sc.nextInt();
            for(int i = 0; i <= n+1; ++i)
                for(int j = 0; j <= n+1; ++j)
                    f[i][j] = BigInteger.ZERO;

            f[0][0] = BigInteger.ONE;

            for(int i = 1; i <= n; ++i) {
                f[i][0] = f[i-1][1];
                for(int j = 1; j <= i; ++j) {
                    f[i][j] = f[i-1][j-1].add(f[i-1][j+1].multiply(BigInteger.valueOf(j+1)));
                }
            }

            BigInteger res = BigInteger.ZERO;
            for(int j = 0; j <= n; ++j)
                res = res.add(f[n][j]);
            System.out.println(res);
        }
    }
}

Code mẫu của hieult

import java.util.*;
import java.math.*;
import java.io.*;

public class Main {

    static BigInteger f[] = new BigInteger[505];
    static BigInteger C[][] = new BigInteger[505][505];
    static int n;

    public static void init(){
        f[0] = BigInteger.ONE;
        for(int i = 2; i <= 500; i += 2){
            f[i] = f[i - 2].multiply(BigInteger.valueOf(i - 1));
        }

        for(int i = 0; i <= 500; i++) for(int j = 0; j <= 500; j ++) C[i][j] = BigInteger.ZERO;
        for(int i = 0; i <= 500; i++) C[0][i] = BigInteger.ONE;
        for(int i = 1; i <= 500; i++) for(int j = 1; j <= i; j++) C[j][i] = C[j - 1][i - 1].add(C[j][i - 1]);
    }

    public static void main(String args[]) throws Exception{
        Scanner sc = new Scanner(System.in);
        init();
        n = sc.nextInt();
        BigInteger res = BigInteger.ZERO;
        for(int i = 0; i <= n; i++) if((n - i) % 2 == 0){
            res = res.add(C[i][n].multiply(f[n - i]));
        }
        System.out.println(res);
    }
}

Code mẫu của skyvn97

#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
typedef vector<int> vi;
struct bignum
{
       vi d;
       bignum()
       {
        d.clear();
       }
       bignum(int x)
       {
        bignum();
        if (x==0) d.push_back(0);
        while (x>0)
              {
               d.push_back(x%10);
               x=x/10;
              }           
       }      
       bignum(const bignum &x)
       {
        d=x.d;
       } 
       bignum operator + (const bignum &x)
       {
        bignum res=bignum();
        int n=max(d.size(),x.d.size());        
        int i,s,c,a,b;
        s=0;
        c=0;
        for (i=1;i<=n;i=i+1)
            {
             if (i>d.size()) a=0; else a=d[i-1];
             if (i>x.d.size()) b=0; else b=x.d[i-1];
             s=a+b+c;
             if (s>9) c=1; else c=0;
             res.d.push_back(s%10);
            }
        if (c>0) res.d.push_back(1);           
        while (res.d[res.d.size()-1]==0) res.d.pop_back();
        return (res);
       }
       bignum operator * (int x)
       {
        if (x==0) return (bignum(0));
        if ((d.size()==1) && (d[0]==0)) return (bignum(0));
        bignum res=bignum();
        int i,s,c;
        s=0;
        c=0;
        for (i=1;i<=d.size();i=i+1)
            {
             s=d[i-1]*x+c;
             c=s/10;
             res.d.push_back(s%10);
            }
        while (c>0)
              {
               res.d.push_back(c%10);
               c=c/10;
              }
        while (res.d[res.d.size()-1]==0) res.d.pop_back();
        return (res);
       }
       void print(void)
       {
            int i;
            for (i=d.size();i>=1;i=i-1) printf("%d",d[i-1]);
       }
};
bignum f[4];
int n,i;
int main(void)
{
    scanf("%d",&n);
    f[1]=bignum(1);
    f[2]=bignum(2);
    for (i=3;i<=n;i=i+1)
        f[i%3]=f[(i-1)%3]+f[(i-2)%3]*(i-1);
    f[n%3].print();
   return 0;
}

Bình luận

Hãy đọc nội quy trước khi bình luận.


Không có bình luận tại thời điểm này.