问题 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. 

输入

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.   
There are no more than 100 test cases. 

输出

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]);
        }
    }
}

转载于:https://www.cnblogs.com/acerkoo/p/9490318.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值