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