The 2018 ACM-ICPC China JiangSu Provincial Programming Contest - J. Set

本文探讨了一种特殊的子集——Meo Set,并提供了解决方案。Meo Set 定义为从集合 A={1,2,...,N}

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Let's consider some math problems.

JSZKC has a set A={1,2,...,N}. He defines a subset of AA 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.

Input Format

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 100100 test cases.

Output Format

One line per case, an integer indicates the answer.

样例输入

3

样例输出

23

 

解题思路:

题目定义了 “Meo set” ,为A的子集,其中任意两个元素至少差2。同时定义了计算方法,即分别计算集合中元素乘积,平方后想加。

N=1时,结果为1;N=2时,结果为5,;N=3时结果为23;N=4时结果为119。

5-1=4=2*1+2;23-5=18=3*5+3;119-23=96=4*23+4

不难发现规律,ans[i] = ans[i-1]+i*ans[i-1]+i = (i+1)ans[i-1]+i

数据较大,需要借助string实现大数加法、大数乘法。

 

AC代码:

#include <bits/stdc++.h>
using namespace std;

string sum(string s1,string s2)
{
    if(s1.length()<s2.length())
    {
        string temp=s1;
        s1=s2;
        s2=temp;
    }
    int i,j;
    for(i=s1.length()-1,j=s2.length()-1;i>=0;i--,j--)
    {
        s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0));   //注意细节
        if(s1[i]-'0'>=10)
        {
            s1[i]=char((s1[i]-'0')%10+'0');
            if(i) s1[i-1]++;
            else s1='1'+s1;
        }
    }
    return s1;
}

string Multiply(string s,int x)  //大数乘以整形数
{
    reverse(s.begin(),s.end());
    int cmp=0;
    for(int i=0;i<s.size();i++)
    {
        cmp=(s[i]-'0')*x+cmp;
        s[i]=(cmp%10+'0');
        cmp/=10;
    }
    while(cmp)
    {
        s+=(cmp%10+'0');
        cmp/=10;
    }
    reverse(s.begin(),s.end());
    return s;
}

int main()
{
    int n;
    while(~scanf("%d",&n))
    { 
	string result;
        result = "1";
	int i;
	for(i=2;i<=n;i++)
	{
            result=Multiply(result,i+1);
            result=sum(result,to_string(i));
	}
	cout<<result<<endl;
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值