• ZOJ 1489 HDU1395 2^x mod n = 1 数学


    2^x mod n = 1

    Time Limit: 2 Seconds      Memory Limit:65536 KB

    Give a number n, find the minimum x 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


    题目大意:

    给你正整数n,求最小的x使得2^x mod n = 1。


    1:n=1无解,%后为0。

    2:n为偶数无解,(偶%偶)==偶!=1。

    3:n为奇数一定有解,对于乘法逆元:在a mod n的操作下,a存在乘法逆元当且仅当a与n互质。(a为偶数,n为奇数,互质)

    http://blog.csdn.net/adjky/article/details/70341771 ,大师的代码先阁起,看得懂了再来看。


    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    int n;
    int main()
    {
    	while(cin>>n)
    	{
    		int k=1,t=2;
    		if(n==1||n%2==0) printf("2^? mod %d = 1
    ",n);	
    		else{ 
    		  while(t%n!=1){
    		    t*=2;
    	        k++;
    	        t%=n;
    		  }
    		  printf("2^%d mod %d = 1
    ",k,n);
    	    } 
    	}
    	return 0;
    } 

  • 相关阅读:
    最后一次不用看脸的日子
    经典算法_指针
    #include <process.h>
    经典算法_文件
    stdin、stdout、stderr
    经典算法_位运算
    经典算法_结构体
    经典算法_字符串
    #include <string.h>
    #include <stdio.h>
  • 原文地址:https://www.cnblogs.com/hua-dong/p/7603963.html
Copyright © 2020-2023  润新知