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

好久没有刷题了!半天才ac!其实就是欧拉函数的模板题目!哎!!!

Posted by chenxuan123456789 at 2012-09-22 20:50:55 on Problem 2478
#include <stdio.h>   
#include <stdlib.h>   
#define N 1000000   
int prime[N + 1];  
__int64 ans[N + 1];  
int main()  
{  
    int i, j, n;  
    prime[0] = prime[1] = 0;  
    for (i=2; i<=N; i++)  
        prime[i] = 1;  
    for (i=2; i*i<=N; i++)  
    {  
        if (prime[i])  
        {  
            for (j=i*i; j<=N; j+=i)  
                prime[j] = 0;  
        }  
    }  
    for (i=1; i<=N; i++)  
        ans[i] = i;  
    for (i=2; i<=N; i++)  
    {  
        if (prime[i])  
        {  
            for (j=i; j<=N; j+=i)  
                ans[j] = ans[j] / i * (i - 1);  
        }  
    }  
    for (i=3; i<=N; i++)  
        ans[i] += ans[i - 1];  
    while(scanf("%d",&n)!=EOF&&n)
        printf("%I64d\n", ans[n]);  
  
    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