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): 12605    Accepted Submission(s): 3926

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

题目大意:给你一个数N,判断是否存在x,满足2^x mod N = 1。若

满足,对于满足条件的最小x,输出2^x mod N = 1,否则输出

2^? mod 2 = 1。

思路:用到数论上的乘法逆元的规律了。

乘法逆元:对于整数a、p如果存在整数b,满足a*b mod p = 1,则称

b是a的模p的乘法逆元。a存在模p的乘法逆元的充要条件是gcd(a,p) = 1

此题中,令a = 2^x,b = 1,p = n,则若存在x使得2^x mod N = 1,

则gcd(2^x,N) = 1。

1>.因为N>0,当N为偶数时,gcd(2^x,N) = 2*k(k=1,2,3……),不满足

2>.当N为奇数时,gcd(2^x,N) = 1满足条件。

3>.当N为1时,2^x mod N = 0,不符合条件

所以N为奇数,且不为1,满足2^x mod N = 1,暴力求解。

参考博文:http://blog.csdn.net/mxway/article/details/8954364

#include<stdio.h>

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        if(n==1 || !(n&1))
        {
            printf("2^? mod %d = 1\n",n);
        }
        else
        {
            int ans = 2,num = 1;
            while(ans!=1)
            {
                ans = ans * 2 % n;
                num++;
            }
            printf("2^%d mod %d = 1\n",num,n);
        }
    }

    return 0;
}
时间: 2024-08-25 11:33:40

HDU1395_2^x mod n = 1【数论】【水题】的相关文章

HDU 2674 N!Again (数论-水题)

N!Again Problem Description WhereIsHeroFrom:             Zty, what are you doing ? Zty:                                     I want to calculate N!...... WhereIsHeroFrom:             So easy! How big N is ? Zty:                                    1 <=

BZOJ1968: [Ahoi2005]COMMON 约数研究(数论 水题)

Description Input 只有一行一个整数 N(0 < N < 1000000). Output 只有一行输出,为整数M,即f(1)到f(N)的累加和. Sample Input 3 Sample Output 5 Solve: 数论水题,求因数又不是质因数,所以,只要求出对于[1 , n]有多少个倍数,就表示[1 , n]中以i为因数的是哪些,加起来就可以了 Code: 1 #include <bits/stdc++.h> 2 using namespace std;

HDU 4143 A Simple Problem(数论-水题)

A Simple Problem Problem Description For a given positive integer n, please find the smallest positive integer x that we can find an integer y such that y^2 = n +x^2. Input The first line is an integer T, which is the the number of cases. Then T line

洛谷数论{水题}集合

1. P1327数列排序 题目描述 给定一个数列{an},这个数列满足ai≠aj(i≠j),现在要求你把这个数列从小到大排序,每次允许你交换其中任意一对数,请问最少需要几次交换? 输入输出格式 输入格式: 第一行,正整数n (n<=100,000). 以下若干行,一共n个数,用空格分隔开,表示数列{an},任意-231<ai<231. 输出格式: 只有一行,包含一个数,表示最少的交换次数. 输入输出样例 输入样例#1: 8 8 23 4 16 77 -5 53 100 输出样例#1: 5

HDU 3215 The first place of 2^n (数论-水题)

The first place of 2^n Problem Description LMY and YY are mathematics and number theory lovers. They like to find and solve interesting mathematic problems together. One day LMY calculates 2n one by one, n=0, 1, 2,- and writes the results on a sheet

hdu 2674 N!Again 数论水题啊~~~

N!Again Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3803    Accepted Submission(s): 2036 Problem Description WhereIsHeroFrom:             Zty, what are you doing ? Zty:                     

hdu 1141 Factstone Benchmark 数论水题,,阶乘用斯特林公式

Factstone Benchmark Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1760    Accepted Submission(s): 973 Problem Description Amtel has announced that it will release a 128-bit computer chip by

【数论,水题】UVa 10127 - Ones

题目链接 题意:给你一个数n,问最少有多少个1构成的“1”串(1,11,...)能整除n; 比如:111能被3整除: 111111能被7整除:... 作为水货觉得只要自己能1A的都是水题=. = 1 #include<iostream> 2 #include<cstdio> 3 #include<cstdlib> 4 #include<cmath> 5 using namespace std; 6 const int maxn = 10010; 7 int

UVaLive 6591 &amp;&amp; Gym 100299L Bus (水题)

题意:略. 析:不解释,水题. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set>

codeforces 710A A. King Moves(水题)

题目链接: A. King Moves 题意: 给出king的位置,问有几个可移动的位置; 思路: 水题,没有思路; AC代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <bits/stdc++.h> #include <stack> #include &l