Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

贴个c++单序列递推代码,N=1000,K=100时答案是15658181104580771094597751280645

Posted by a280920481 at 2018-11-21 23:15:43 on Problem 3181
#include <iostream>
using namespace std;

const int S = 1000000000;//需要实现高精度,每个数表示 10 ^ 9 位
const int NumSize = 4;


struct Num
{
	int integer[NumSize];
}a[1001];

void add(Num &x, Num &y);//将 (x + y) 赋给 x

int main()
{
	int N, K;

	cin >> N >> K;

	for (int i = 0; i <= N; i++)
	{
		a[i].integer[0] = 1;
	}
	
	for (int i = 2; i <= K; i++)
	{
		for (int j = i; j <= N; j++)
		{
			add(a[j], a[j - i]);
		}
	}

	int up = 3;

	while (!a[N].integer[up])
	{
		up--;
	}

	for (; up >= 0; up--)
	{
		cout << a[N].integer[up];
	}
	return 0;

}

void add(Num &x, Num &y)
{
	for (int i = 0; i < NumSize; i++)
	{
		x.integer[i] += y.integer[i];
	}

	for (int i = 1; i < NumSize; i++)
	{
		x.integer[i] += x.integer[i - 1] / S;
	}

	for (int i = 0; i < NumSize; i++)
	{
		x.integer[i] %= S;
	}

	return;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator