问题 J: JS Set
问题 J: JS Set
时间限制: 1 Sec 内存限制: 128 MB提交: 3 解决: 2
[提交][状态][讨论版][命题人:admin]
题目描述
Let’s consider some math problems.
JSZKC has a set A={1,2,…,N}. He defines a subset of A as ‘Meo set’ if there doesn’t exist two integers in this subset with difference one. For example, When A={1,2,3}, {1},{2},{3},{1,3} are ‘Meo set’.
For each ‘Meo set’, we can calculate the product of all the integers in it. And then we square this product. At last, we can sum up all the square result of the ‘Meo set’.
So please output the final result.
JSZKC has a set A={1,2,…,N}. He defines a subset of A as ‘Meo set’ if there doesn’t exist two integers in this subset with difference one. For example, When A={1,2,3}, {1},{2},{3},{1,3} are ‘Meo set’.
For each ‘Meo set’, we can calculate the product of all the integers in it. And then we square this product. At last, we can sum up all the square result of the ‘Meo set’.
So please output the final result.
输入
The input file contains several test cases, each of them as described below.
- The first line of the input contains one integers N (1 ≤ N≤ 100), giving the size of the set.
输出
One line per case, an integer indicates the answer
样例输入
3
样例输出
23
提示
江苏邀请赛的J题。题意是给你从1~n,要求形成子集,每个子集中任意两个元素的差不为 1 ,比如集合A={1,2,3},A 的子集为{1},{2},{3},{1,3},求 子集中元素的乘积 的平方和。
思路:写一下1~5的答案,会发现规律
n ans
1 -> 1
2 -> 5
3 -> 23
4 -> 119
5 -> 719
观察这个数,我们得到规律 ans[n] = ans[n-1] *(i+1) +i;
于是写代码就很简单了,但是要注意数据范围会爆long long,所以直接用java的大整数写
import java.math.BigInteger;
import java.util.Scanner;
public class Main{
static Scanner cin = new Scanner(System.in);
static BigInteger[] a = new BigInteger[120];
public static void init(){
a[1]=BigInteger.valueOf(1);
for(int i=2; i<=100; i++){
a[i] = a[i-1].multiply(BigInteger.valueOf(i+1)).add(BigInteger.valueOf(i));
}
}
public static void main(String args[]){
init();
int n;
while(cin.hasNext()){
n = cin.nextInt();
System.out.println(a[n]);
}
}
}