2^x mod n = 1

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

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
题目不难,就是要知道取模运算的基本法则这题主要是(a*b)%c=(a%c * b%c)%c.
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(void)
{
int n;
while (cin>>n)
{
if(n%2==0||n==1)//n为1或者偶数一定无解
printf("2^? mod %d = 1\n",n);
else//奇数一定有解
{
int ans=1,t=2;
while (t%n!=1)
{
t=(t*2)%n;//每次都求余数据就不会溢出
ans++;
}
printf("2^%d mod %d = 1\n",ans,n);
}
}
return 0;
}

HDU——1395 2^x mod n = 1(取模运算法则)的更多相关文章

  1. 题解报告:hdu 1061 Rightmost Digit(快速幂取模)

    Problem Description Given a positive integer N, you should output the most right digit of N^N. Input ...

  2. a ^ b mod c 取模运算优化反思(老物)

    这是一篇嘲讽我之前的自己采用笨重愚蠢思想去解决问题的日志. RSA 加密与解密涉及到 a ^ b mod c 的问题,如何计算这个值呢? 我会选择 pow(a, b) % c, 事实上在写RSA的时候 ...

  3. poj 3980 取模运算

    取模运算 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10931   Accepted: 6618 Description ...

  4. java 取模运算% 实则取余 简述 例子 应用在数据库分库分表

    java 取模运算%  实则取余 简述 例子 应用在数据库分库分表 取模运算 求模运算与求余运算不同.“模”是“Mod”的音译,模运算多应用于程序编写中. Mod的含义为求余.模运算在数论和程序设计中 ...

  5. 二分求幂/快速幂取模运算——root(N,k)

    二分求幂 int getMi(int a,int b) { ; ) { //当二进制位k位为1时,需要累乘a的2^k次方,然后用ans保存 == ) { ans *= a; } a *= a; b / ...

  6. PHP中关于取模运算及符号

    执行程序段<?php  echo 8%(-2) ?>,输出结果是: %为取模运算,以上程序将输出0 $a%$b,其结果的正负取决于$a的符号. echo ((-8)%3);     //将 ...

  7. Divide two numbers,两数相除求商,不能用乘法,除法,取模运算

    问题描述:求商,不能用乘法,除法,取模运算. 算法思路:不能用除法,那只能用减法,但是用减法,超时.可以用位移运算,每次除数左移,相当于2倍. public class DividTwoInteger ...

  8. javascript取模运算是怎么算的?其实是取余数

    问到是否整除,这里记录下取模 比如120分钟是不是整点?120%60 === 0 为整点 javascript取模运算是一个表达式的值除以另一个表达式的值,并返回余数. 取模在js里就是取余数的意思. ...

  9. 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/Oth ...

随机推荐

  1. 数据分析R&Python-Rpy2包环境配置

    Rpy2环境配置 最近想将R整合到以flask为后端框架的web系统中,在服务器端做数据统计分析.需要将R语言整合到Python中,发现Python中的Rpy2可以调用R语言,所以花了一些时间配置了一 ...

  2. rhythmbox插件开发笔记3:安装 makefile && schema && po

    本篇主要讲通过makefile方式来安装rhythmbox插件的相关知识. makefile 如果makefile是什么,请自行谷歌 参考了pandasunny同学的rhythmbox-baidu-m ...

  3. no pointer in java

    Why there are no pointers in Java? In Java there are references instead of pointers. These reference ...

  4. Java的日期类和日期格式化类

    日期类: Date date = new Date(); // 获取当前的系统时间 2 System.out.println("年份:"+ date.getYear()); Cal ...

  5. sql快速删除所用表,视图,存储过程

    [http://www.th7.cn/db/mssql/2011-07-07/10127.shtml#userconsent#] 删除用户表 .select 'DROP TABLE '+name fr ...

  6. Java 练习:字符串反转

    package com.swift; public class String_Reverse_Test { public static void main(String[] args) { /* * ...

  7. css文件和js文件后面带一个问号----2015-1103

    经常看一些网站页面源代码中的css文件和js文件后面带一个问号,后面跟着一连串数字或字符,这是干什么用的? 这个方法我也用过,而且很好用?,它的作用有两个:1.作为版本号,让自己方便记忆.查找:2.作 ...

  8. 【markdown】 markdown 语法

    介绍几个 markdown 语法学习地址和相关工具 参考链接 coding gitlab markdown offical markdown editor markdown editor2

  9. 14-15.Yii2.0模型的创建/读取数据使用,框架防止sql注入

    目录 创建数据库 表article 配置 db.php 连接数据库 创建控制器 HomeController.php 创建models 创建数据库 表article 1.创建库表 CREATE TAB ...

  10. python3 兔子繁殖问题

    题目 有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? 代码: month = int(input("繁殖 ...