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

这样做超时了,求不超时算法。。。

Posted by dearboy at 2005-02-11 18:35:40 on Problem 2167
//系数有点像杨辉三角,DP 先谢了。。

#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;

const int MAXN=100000;
int c[MAXN+1],d[MAXN+1];
int n,m;

void dp()
{
	for(int i=0;i<=n;i++)
	c[i]=d[i]=0;
	int *now=c,*last=d,*temp;
	now[0]=last[0]=1;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<i;j++)
		now[j]=(last[j]+last[j-1])%m;
		temp=last;
		last=now;
		now=temp;
	}
	int total=0;
	int first;
	for(int i=1;i<n;i++)
	if(last[i]==0)total++,first=i;
	cout<<total<<endl;
	for(int i=1;i<n;i++)
	if(last[i]==0)
	{
		cout<<i+1;
		if(i==first)
	  cout<<endl;
	  else 
	  cout<<" ";
  }
}

int main()
{
	while(cin>>n>>m)
	dp();
	return 0;
}

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