HDU3003 Pupu,快速幂取模
快速幂取模就是在O(logn)内求出a^n mod b的值。算法的原理是(a*b) mod c=(a mod c)*(b mod c)mod c
/*******************************************************************************# Author : Neo Fung# Email : neosfung@gmail.com# Last modified: 2012-07-05 18:57# Filename: HDU3003 Pupu.cpp# Description : ******************************************************************************/
#ifdef _MSC_VER
#define DEBUG
#define _CRT_SECURE_NO_DEPRECATE
#endif#include <fstream>
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <string>
#include <limits.h>
#include <algorithm>
#include <math.h>
#include <numeric>
#include <functional>
#include <ctype.h>
using namespace std;const int kMAX=10010;
const double kEPS=10E-6;// 快速幂取模
// 快速幂取模就是在O(logn)内求出a^n mod b的值。算法的原理是(a*b) mod c=(a mod c)*(b mod c)mod c
long long exp_mod(const long long &base,const long long &exp,const long long &mod)
{if(exp==0ll) return 1ll%mod;if(exp==1ll) return base%mod;long long tmp=exp_mod(base,exp/2,mod);tmp=tmp*tmp%mod;if(exp&1) tmp=tmp*base%mod;//if n is odd;return tmp;
}int main(void)
{
#ifdef DEBUG freopen("../stdin.txt","r",stdin);freopen("../stdout.txt","w",stdout);
#endif long long n;while(~scanf("%lld",&n) && n){long long ans=0;if(n>1)ans=exp_mod(2,n-1,n)+1;printf("%lld\n",ans%n);}return 0;
}
HDU3003 Pupu,快速幂取模相关推荐
- 快速幂取模——Pupu(HDU 3003)
题目: 由题目推出计算公式: ans = (2^(n-1) + 1) % n 因为n取值很大,所以需要用到快速幂取模: int multi(int a,int b) { int ret; ret=1; ...
- C语言快速幂取模算法小结
资料链接:http://www.jb51.net/article/54947.htm C语言实现的快速幂取模算法,是比较常见的算法.分享给大家供大家参考之用.具体如下: 首先,所谓的快速幂,实际上是快 ...
- 数学--数论--HDU 4675 GCD of Sequence(莫比乌斯反演+卢卡斯定理求组合数+乘法逆元+快速幂取模)
先放知识点: 莫比乌斯反演 卢卡斯定理求组合数 乘法逆元 快速幂取模 GCD of Sequence Alice is playing a game with Bob. Alice shows N i ...
- 【算法分析与设计】快速幂算法与快速幂取模算法
文章目录 快速幂算法 算法分析 算法实现 位运算优化 BigInteger支持 快速幂取模算法 算法优点 算法推导 算法实现 BigInteger支持 本文完整代码实现(Java语言描述) 快速幂算法 ...
- CodeForces Round #191 (327C) - Magic Five 等比数列求和的快速幂取模
很久以前做过此类问题..就因为太久了..这题想了很久想不出..卡在推出等比的求和公式,有除法运算,无法快速幂取模... 看到了 http://blog.csdn.net/yangshuolll/art ...
- 【快速幂取模】NOI 7833:幂的末尾
NOI 7833:幂的末尾 点击打开链接 总时间限制: 1000ms 内存限制: 65536kB 描述 幂ab的末3位数是多少? 输入 两个正整数a,b.1 <= a <= 100 ...
- 【算法】求n的m次方(快速幂取模)
题目 求n的m次方,n,m均为自然数. 解析 看似简单的题目,但是要想写的高效还不是那么容易想出来. 实现 unsigned int power(unsigned int a, unsigned in ...
- 大数取模运算,快速幂取模运算
1.快速幂取模 http://www.cnblogs.com/yinger/archive/2011/06/08/2075043.html 快速幂取模就是在O(logn)内求出a^n mod b的值. ...
- A hard puzzle(HDU1097)(快速幂取模)
题目链接: HDU1097 题面: 翻译: 问题描述 lcy给了feng5166,lwg,JGShining和Ignatius一个难题:给了a和b,如何知道a^b.每个人都反对这个BT问题,所以lcy ...
最新文章
- 独家 | 深度学习基础——激活函数以及什么时候使用它们?(附代码)
- 一个虚拟键盘的简单思路
- Activity生命周期的学习和验证
- 三种常见的SQL分页语句
- linux putty 字体,putty修改字体配色
- 寻找链表倒数第k个节点
- Jumpserver web界面跳板机
- ps aux 查看进程信息
- H3C 典型数据链路层标准
- phpwind不支持php7,phpwind8.7 phpwind v8.7 新功能曝光
- 计算机网络考研知识点史上最全背诵笔记
- Filter中获取传递参数(解决post请求参数问题)
- 大华海康萤石云摄像头Http接口云台控制开发
- android menu 键值,Android KeyCode安卓手机按键对应键码键值
- a different object with the same identifier value was already associated whith
- STM32使用库函数点灯实验
- 谷歌pay 手续费_您可以使用Google Pay进行的所有操作
- SpringMVC整合mybatis(终结版)
- 我的世界mod整合包java_[1.7.10]我的世界多MOD整合包-糖果领域
- 我把废旧Android手机如何改造成Linux服务器