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

Re:请问怎么才能不TLE??请高手帮忙啊

Posted by einstein17 at 2006-04-26 00:43:03 on Problem 2800
In Reply To:Re:请问怎么才能不TLE??请高手帮忙啊 Posted by:einstein17 at 2006-04-26 00:32:55
我是这样做的;
k mod i=k- floor(k/i)*i,
根据这公式,只要找到一个区间[a,b],使在这个区间内floor(k/i)为一个定值,然后用求和公式,就可以求出在[a,b]上的余数之和。
j为当前的floor(k.i),则a=k/(j+1)=1,b=k/j,然后j++,可以a=k/(j+1)=1,b=k/j得出下一个区间。
我想这样做已经很快了,但是还是TLE,在我机上很快就出答案了(几乎为0s),但是为什么还是TLE!
请高手指点。

#include<stdio.h>
__int64 sum_k(__int64 k,__int64 n)
{
	__int64 sum;
	bool flag;
	__int64 a,b,t,i,m;
	__int64 j;
	j=k/n;
	a=k/(j+1)+1;
	b=k/j;
	if(b>n)b=n;
	sum=0;
	flag=false;
	while(a<b)
	{
		flag=true;
		t=k*(b-a+1)-j*(b+a)*(b-a+1)/2;
		sum+=t;
		j++;
		b=m=a-1;
		a=k/(j+1)+1;
	}
	if(flag)
	{
		for(i=2;i<=m;i++)
			sum+=__int64(k%i);
	}
	else
	{
		for(i=1;i<=n;i++)
			sum+=k%i;
	}
	return sum;
}
int main()
{
	__int64 n,k;
	scanf("%I64d %I64d",&n,&k);
	__int64 sum=0;
	sum=0;
	if(n==1)
		sum=0;
	else if(k==1)
	{
		sum=n-1;
	}
	else
	{
		if(n>k)
		{
			sum+=sum_k(k,k);
			sum+=k*(n-k);
		}
		else
			sum=sum_k(k,n);
	}
	printf("%I64d\n",sum);
	return 1;
}

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