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

i*i<n就TLE,i<sqrt(n)就AC!!!!!!!!!!!!!!!!!!!

Posted by Xu_dafang at 2016-03-30 01:06:17 on Problem 2480
代码如下:
#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<cstring>
#include<cmath>

#include<algorithm>
#define LL long long 
using namespace std;
const int maxn=1000010;
int pri[maxn];
int num=0;
int n;
bool flag[maxn];
void getprime(){  
	for(int i=2;i<=maxn;i++){
		if(!flag[i])pri[num++]=i;
		for(int j=0;j<num&&i*pri[j]<=maxn;j++){
			flag[pri[j]*i]=true; 
			if(i%pri[j]==0) break;
		}
	}
} 
LL phi(LL n){
	LL ans=n; 
	for(int i=0;i<num&&pri[i]*pri[i]<=n;i++){
		if(n%pri[i]==0){
			ans=ans/pri[i]*(pri[i]-1);
			while(n%pri[i]==0)n/=pri[i];
		}
	}
	if(n>1)ans=ans/n*(n-1);
	return ans;
}

int main(){
	freopen("i.txt","r",stdin); 
	getprime(); 
	while(scanf("%d",&n)!=EOF){
		LL ans=0;
		for(int i=1;i<=sqrt(n*1.0);i++){
		    if(i*i==n)ans+=i*phi(n/i);
			else if(n%i==0){
				ans=ans+i*phi(n/i);
				LL tmp=n/i;
				ans+=tmp*phi(i);
			}
		}
		printf("%I64d\n",ans);
	}
	return 0;
} 
话说这样卡数据,真的好吗???????我花了4个小时来找这个答案啊!!!!


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