2^x mod n = 1 HDU - 1395(欧拉定理 原根)

2^x mod n = 1

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

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

Source

ZOJ Monthly, February 2003

Recommend

Ignatius.L

能不能不用暴力

好的 请转到https://www.cnblogs.com/WTSRUVF/p/10798178.html

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <list>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1100000, INF = 0x7fffffff;
int prime[maxn + 10], phi[maxn + 10];
bool vis[maxn + 10];
int n, ans;

void getphi()
{
    ans = 0;
    phi[1] = 1;
    for(int i = 2; i <= maxn; i++)
    {
        if(!vis[i])
        {
            prime[++ans] = i;
            phi[i] = i - 1;
        }
        for(int j = 1; j <= ans; j++)
        {
            if(i * prime[j] > maxn) break;
            vis[i * prime[j]] = 1;
            if(i % prime[j] == 0)
            {
                phi[i * prime[j]] = phi[i] * prime[j];
                break;
            }
            else
                phi[i * prime[j]] = phi[i] * (prime[j] - 1);

        }
    }
}

int q_pow(int a, int b, int mod)
{
    int res = 1;
    while(b)
    {
        if(b & 1) res = res * a % mod;
        a = a * a % mod;
        b >>= 1;
    }
    return res;
}

vector<int> v;

int main()
{
    getphi();
    while(cin >> n)
    {
        v.clear();
        if(n % 2 == 0 || n == 1)
        {
            printf("2^? mod %d = 1\n", n);
            continue;
        }
        int res = phi[n];
        for(int i = 2; i <= sqrt(phi[n] + 0.5); i++)
        {
            if(res % i == 0)
                v.push_back(i), v.push_back(res / i);
        }
        sort(v.begin(), v.end());
        for(int i = 0; i < v.size(); i++)
        {
            if(q_pow(2, v[i], n) == 1)
            {
                res = v[i];
                break;
            }

        }

                printf("2^%d mod %d = 1\n", res, n);

    }

    return 0;

}

2^x mod n = 1

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

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

Source

ZOJ Monthly, February 2003

Recommend

Ignatius.L

原文地址:https://www.cnblogs.com/WTSRUVF/p/10800970.html

时间: 2024-08-03 02:35:21

2^x mod n = 1 HDU - 1395(欧拉定理 原根)的相关文章

hdu 1395 2^x mod n = 1 (简单数论)

题目大意: 求出一个最小的x 使得 2的x次方对n取模为1 思路分析: 若要 a*b%p=1  要使得b存在 则 gcd (a,p)=1. 那么我们应用到这个题目上来. 当n为偶数 2^x 也是偶数,那么gcd 肯定不是1.故这个是不存在的. 那么n为奇数的时候,也就一定是1了. 所以直接暴力找. #include <iostream> #include <cstdio> using namespace std; int main() { int n; while(scanf(&q

HDU 1395 2^x mod n = 1

/* 中文题意: 中文翻译: 题目大意:求出最小的 n 使得2的 I 次方对 n 的值为1. 解题思路:如下: 难点详解:先用费马小定理了解2的 i 次方对偶数取余都不可能是一,还有就是排除 1 .之后要用中国剩余定理让 t 的值不超出 int 范围.不用这个定理我错了n次,都是超时.我猜测可能是 t 的值超出了int 的范围了,之后的数都是随机的,所以一直运行不出来,才会超时的.(不知道我的猜测对不对,欢迎大家指正) 关键点:理解费马小定理(我到现在还是不理解),只是用到了一点点这个东西.还有

HDU 4704 欧拉定理

题目看了很久没看懂 就是给你数n,一种函数S(k),S(k)代表把数n拆成k个数的不同方案数,注意如n=3,S(2)是算2种的,最后让你求S(1~n)的和模1e9+7,n<=1e100000.那么其实一个S(k)就是把n个小球放到k-1个盒子里的种类数,求和也就是求个$2^{n-1}$. n超大,但是模数只有1e9+7,用欧拉定理就行了. /** @Date : 2017-09-12 18:41:59 * @FileName: HDU 4704 欧拉定理 降幂.cpp * @Platform:

hdu 1395 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): 13341    Accepted Submission(s): 4143 Problem Description Give a number n, find the minimum x(x>0) that satisfies 2^x mod n = 1. In

hdu 1395 2^x mod n = 1(暴力题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1395 2^x mod n = 1 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12146    Accepted Submission(s): 3797 Problem Description Give a number n, find

hdu 1395 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): 18742    Accepted Submission(s): 5860 Problem Description Give a number n, find the minimum x(x>0) that satisfies 2^x mod n = 1. Inp

hdu 1395 Minimum Transport Cost

#include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <map> #include <string> #include <ma

hdu 3307 Description has only two Sentences (欧拉函数+快速幂)

Description has only two SentencesTime Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 852 Accepted Submission(s): 259 Problem Descriptionan = X*an-1 + Y and Y mod (X-1) = 0.Your task is to calculate th

hdu 4578 Transformation

http://acm.hdu.edu.cn/showproblem.php?pid=4578 又做了一道好题.. 有三种操作: 1 a b c [a,b]加上c 2 a b c [a,b]乘上c 3 a b c [a,b]变为c 4 a b c 求[a,b]的c次方和(1<=c<=3) 这题首先需要解决的第一个问题是加上或乘上一个数对这个区间的c次方和分别产生什么改变,很简单,一化简就能得到. 第二个问题是当一段区间上既有乘又有加的lazy时应该怎么向下推送,因为一段区间上只能有一个lazy,