• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Hug_Sea
博客园    首页    新随笔    联系   管理    订阅  订阅

POJ-1284 Primitive Roots

题意:给定奇素数N,求N的原根。

思路:定理:奇素数N的原根等于φ(N-1),即直接求N-1的欧拉函数。

题目链接:http://poj.org/problem?id=1284

 

View Code
 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <string>
 6 #include <algorithm>
 7 #include <iostream>
 8 using namespace std;
 9 const int N=65536;
10 
11 int n;
12 
13 int phi(int n){
14     n--;
15     int ans=n;
16     for(int i=2;i*i<=n;i++){
17         if(n%i==0){
18             ans=ans-ans/i;
19             while(n%i==0) n/=i;
20         }
21     }
22     if(n>1) ans=ans-ans/n;
23     return ans;
24 }
25 
26 int main(){
27     
28 //    freopen("data.in","r",stdin);
29 //    freopen("data.out","w",stdout);
30     
31     while(scanf("%d",&n)!=EOF){
32         printf("%d\n",phi(n));
33     }    
34     return 0;
35 }
posted @ 2012-08-07 09:53  Hug_Sea  阅读(164)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3