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

想了好久终于不TLE啦!!!

Posted by Desertangle at 2010-09-19 09:46:57 on Problem 2853 and last updated at 2010-09-19 09:49:33
做了个质因数分解来着,直接分的话会TLE,只要算到sqrt(double(num)) + 1就好了~
#include <cstdio>
#include <cstring>
#include <map>
#include <cmath>
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdlib>

using namespace std;

int main()
{
	int n;
	scanf("%d", &n);
	int pnum;
	int res;
	int num;
	while(n--)
	{
		res = 1;
		scanf("%d%d", &pnum, &num);
		while(num % 2 == 0)
			num /= 2;
		int cnt = 1;
		int end = sqrt(double(num)) + 1;
		for(int k = 3; k <= end && num >= k;)
			if(num % k == 0)
			{
				++cnt;
				num /= k;
			}
			else
			{
				k += 2;
				res *= cnt;
				cnt = 1;
			}
		res *= cnt;
		if(num > 1) res *= 2;    //最后一个质因数
		printf("%d %d\n", pnum, res-1);
	}
	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