zoukankan      html  css  js  c++  java
  • hdu1395 2^x mod n = 1(欧拉函数)

    2^x mod n = 1

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 20133    Accepted Submission(s): 6321


    Problem Description
    Give a number n, find the minimum x(x>0) that satisfies 2^x mod n = 1.
     
    Input
    One positive integer on each line, the value of n.
     
    Output
    If the minimum x exists, print a line with 2^x mod n = 1.

    Print 2^? mod n = 1 otherwise.

    You should replace x and n with specific numbers.
     
    Sample Input
    2
    5
     
    Sample Output
    2^? mod 2 = 1
    2^4 mod 5 = 1
     
    Author
    MA, Xiao
     
    题意:求满足2^x mod n = 1的最小x的值
     
    直接暴力:
    #include<iostream>
    #include<math.h>
    #define ll long long
    using namespace std;
    int main()
    {
      ll n;
      while(~scanf("%lld",&n))
      {
        if(n==1||n%2==0)
          printf("2^? mod %d = 1
    ",n );
        else
        {
          ll s=2;
          for(int i=2;;i++)
          {
            s=s*2%n;
            if(s==1)
            {
              printf("2^%d mod %d = 1
    ",i,n );
              break;
            }
          }
          
        }
      }
      return 0;
    
    }

    欧拉函数:

    先求欧拉函数的值phi(n),在对phi(n)进行因数分解,把phi(n)的因数存在数组e[i]里面

    然后依次枚举e[i]的每一个数,同时判断这个数e[i]是否满足2e[i]%m==1,不断更新一个最小值,最后得到答案

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define LL __int64
    LL t,e[1000];
    LL mod;
    LL euler_phi(LL n)//欧拉函数
    {
        LL m=sqrt(n+0.5);
        LL ans=n,i;
        for(i=2;i<=m;i++)
        {
            if(n%i==0)
            {
                ans=ans/i*(i-1);
                while(n%i==0)n=n/i;
            }
        }
        if(n>1)ans=ans/n*(n-1);
        return ans;
    }
    void find(LL n)//找出m的所有因子
    {
        LL i;
        e[t++]=n;
        for(i=2;i*i<=n;i++)
        {
            if(n%i==0)
            {
                if(i*i==n)
                    e[t++]=i;
                else
                {
                    e[t++]=i;
                    e[t++]=n/i;
                }
            }
        }
    }
    LL pows(LL a,LL b)
    {
        LL s=1;
        while(b)
        {
            if(b&1)
                s=(s*a)%mod;
            a=(a*a)%mod;
            b=b>>1;
        }
        return s;
    }
    int main()
    {
        LL n;
        while(cin>>n)
        {
            if(n%2==0||n==1)
                cout<<"2^? mod "<<n<<" = 1"<<endl;
            else
            {
                LL m,ans,i;
                m=euler_phi(n);
                t=0;
                find(m);
                sort(e,e+t);
                mod=n;
                for(i=0;i<t;i++)
                {
                    if(pows(2,e[i])==1)
                    {
                        ans=e[i];
                        break;
                    }
                }
                cout<<"2^"<<ans<<" mod "<<n<<" = 1"<<endl;
            }
        }
        return 0;
    }
  • 相关阅读:
    RecyclerView 数据刷新的几种方式 局部刷新 notify MD
    【图片】批量获取几万张图片
    RV BaseRecyclerViewAdapterHelper 总结 MD
    RecyclerView.ItemDecoration 间隔线
    Kotlin【简介】Android开发 配置 扩展
    Kotlin 特性 语法糖 优势 扩展 高阶 MD
    一个十分简洁实用的MD风格的UI主框架
    折叠伸缩工具栏 CollapsingToolbarLayout
    FloatingActionButton FAB 悬浮按钮
    Glide Picasso Fresco UIL 图片框架 缓存 MD
  • 原文地址:https://www.cnblogs.com/-citywall123/p/10084259.html
Copyright © 2011-2022 走看看