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

Hash 用静态链表TLE 求大牛知道该怎么办!!!

Posted by jackcheng_ht at 2011-11-22 23:40:48 on Problem 1186
#include<iostream>
#include<stdio.h>
#include<cmath>
using namespace std;
struct D
{
	int ele,next;
};
D Link[3375000];
int head;
int Hash[4000037];
int k[6],p[6];
int n,M;
int ans;
void init_Link()
{
	head = 0;
	for(int i = 0 ; i < 3374999 ; i++)Link[i].next = i+1;
	Link[3374999].next = -1;
}

void init_Hash()
{
	for(int i = 0 ; i < 4000037 ; i++)Hash[i] = -1;
}

int Malloc()
{
	int H = head ;
	head = Link[head].next ;
	return H;
}

inline int Pow(int xi,int pi)
{
	return (int)ceil(pow(1.0*xi,1.0*pi));
}

void make_Hash(int d,int sum)
{
	if(d == (n+1)/2)
	{
		int H = Malloc();
		int temp = sum;
		temp = temp<0?-temp:temp;
		Link[H].next = Hash[temp%4000037];
		Hash[temp%4000037] = H;
		Link[H].ele = sum ;
		return ;
	}
	else 
	{
		for(int i = 1 ; i <= M ; i++)
		{
			make_Hash(d+1,sum+k[d]*Pow(i,p[d]));
		}
	}
	
}

void get_ans(int d,int sum)
{
	if(d == n )
	{
		int temp = sum;
		temp = temp<0?-temp:temp;
		int p = Hash[temp%4000037];
		while(p!=-1)
		{
			if(Link[p].ele + sum == 0)ans++;
			p = Link[p].next;
		}
		return ;
	}
	else 
	{
		for(int i = 1 ; i <= M ; i++)
		{
			get_ans(d+1,sum+k[d]*Pow(i,p[d]));
		}
	}	
}
int main()
{
	while(scanf("%d",&n)!=EOF)
	{
		ans = 0;
		scanf("%d",&M);
		for(int i = 0 ; i < n ; i++)scanf("%d%d",&k[i],&p[i]);
		init_Link();
		init_Hash();
		make_Hash(0,0);
		get_ans((n+1)/2,0);
		printf("%d\n",ans);
		
	}
	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