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

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.

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

题链接:点这里

题意:给你一个1到n的序列,然后你从中选取一些子序列满足子序列中不存在2个数相差为1。然后我们把满足条件的每个子序列中的元素先乘积后再平方,题目让我们求这些子序列的平方和。

解题思路:我的第一思路是深搜做,但是和队友讨论一下之后觉得这会超时,而且深搜只能求到前30多。然后我就放弃了这个想法,然后我就先求前几项的值,深搜代码不长,很快就ok了,然后求出了前几项的值。

当n=1   ans=1

当n=2   ans=5

当n=3   ans=23

当n=4   ans=119

当n=5   ans=719

看到这的时候你就会觉得这些数很有个共同点,那就是-1

当n=1  ans=1=2-1

当n=2  ans=5=6-1

当n=3  ans=23=24-1

当n=4  ans=119=120-1

当n=5  ans=719=720-1

而且720=120*6=24*5*6=6*4*5*6=2*3*4*5*6

then  我们可以总结出一个公式 ans[i]=ans[i-1]*(i+1);

但是还是超long long 和 unsigned long long这时候队友就想到了大数乘法,我用数组模拟。

最后就是需要注意两种情况:当末尾有1个零的时候,有n个零的时候。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=110;
#define ll long long
int sum[maxn][100010];
ll n,m;
void mul(int pos,int v){
	int len=sum[pos-1][0],i;
	for(i=1;i<=len;i++){
		sum[pos][i]+=sum[pos-1][i]*v;
		if(sum[pos][i]>9){
			sum[pos][i+1]+=(sum[pos][i]/10);
			sum[pos][i]%=10;
		}
	}
	len=i;
	while(sum[pos][len]>=10){
		sum[pos][len+1]+=(sum[pos][len]/10);
		sum[pos][len]%=10;
		len++;
	}
	//printf("%d\n",len);
	for(;sum[pos][len]==0;len--);
	sum[pos][0]=len;
}
int main(){
	int i,j;
	memset(sum,0,sizeof(sum));
	sum[1][1]=2;sum[1][0]=1;
	for(i=2;i<=100;i++){
		mul(i,i+1);
	}
	while(scanf("%lld",&n)!=EOF){
		ll ans=0,te=2;
		if(n==1){
			printf("1\n");
			continue;
		}
		int te1=1,flag=0;
		while(sum[n][te1]==0&&te1<=sum[n][0]){
			te1++;
			flag++;
		}
		sum[n][te1]--;
		for(i=sum[n][0];i>=te1;i--){
			printf("%d",sum[n][i]);
		}
		while(flag){
			printf("9");
			flag--;
		}
		printf("\n");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/bao___zi/article/details/81189773