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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章