1. 程式人生 > >Push Button II

Push Button II

題目1 : Push Button II

時間限制:10000ms

單點時限:1000ms

記憶體限制:256MB

描述

There are N buttons on the console. Each button needs to be pushed exactly once. Each time you may push several buttons simultaneously.

Assume there are 4 buttons. You can first push button 1 and button 3 at one time, then push button 2 and button 4 at one time. It can be represented as a string "13-24". Other pushing way may be "1-2-4-3", "23-14" or "1234". Note that "23-41" is the same as "23-14".

Given the number N your task is to find the number of different valid pushing ways.

輸入

An integer N. (1 <= N <= 1000)

輸出

Output the number of different pushing ways. The answer would be very large so you only need to output the answer modulo 1000000007.

樣例輸入

3

樣例輸出

13

f[i][j]表示1~i分j組按完,總共有多少種方案

import java.util.Scanner;

public class Main {
    final static long mod = 1000000007;

    public static void main (String[] arg){
        Scanner in = new Scanner(System.in);
        while (in.hasNext()){
            int num = in.nextInt();
            if (num >= 1 && num <= 1000) {
                long count = 0;
                long[][] step = new long[num][num];
                for (int i = 0;i < num;i++){
                    for (int j = 0; j < num; j++){
                        if (i < j) {
                            step[i][j] = 0;
                        } else if (j == 0) {
                            step[i][j] = 1;
                        } else if (i == 1 && j == 1) {
                            step[i][j] = 2;
                        } else  {
                            step[i][j] = (step[i - 1][j] * (j + 1) + step[i - 1][j - 1] * (j + 1)) % mod;
                        }
                        if (i == num - 1) {
                            count = (count + step[i][j]) % mod;
                        }
                    }
                }

                System.out.println(count);
            }

        }
    }
}
import java.util.*;


public class Main{
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();
        long[] dp=new long[n+1];
        dp[1]=1;
        for (int i = 2; i <=n ; i++) {
            for (int j = i; j >0 ; j--) {
                dp[j]=(dp[j]*j+dp[j-1]*j)%1000000007;
            }
        }
        long sum=0;
        for (int i = 1; i <=n ; i++) {
            sum=(sum+dp[i])%1000000007;
        }
        System.out.println(sum);
    }
}
#include <iostream>
#include <stdio.h>
using namespace  std;
#define LL long long
const int mod = 1e9+7;
LL dp[1111][1111];

int main()
{
	int n;
	cin>>n;
	dp[0][0]=1;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=i;j++){
			dp[i][j]=dp[i-1][j]*j+dp[i-1][j-1]*j;
			dp[i][j]%=mod;
		}
	}
	LL ans=0;
	for(int i=1;i<=n;i++)
		ans=(ans+dp[n][i])%mod;
	cout<<ans<<endl;
}