快速幂的求解-java方法(int范围之内)
思想就是,将十进制数化成二进制数。其它就是很简单了。
如:2的11次幂,11的二进制位1011,所以2(11) = 2(2(0) + 2(1) + 2(3));
具体实现步骤,看代码比较简单
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner cin = new Scanner(System.in);
//底数
int a = cin.nextInt();
//指数
int b = cin.nextInt();
int sum = 1;
int temp = a;
while(b != 0)
{
//取其末位
if((b & 1) != 0)
{
sum = sum * temp;
}
temp = temp * temp;
//除其末位
b = b>>1;
}
System.out.print(sum);
}
}
1.经典题目:
输入t,mod,n。t表示测试个数,mod需要除以这个数,n表示以下几行
输入n行,每行两个数字,x,y 求这n行里x的y次方的累加和除以mod,得到余数
输出余数。
实现代码如下:
import java.util.Scanner;
public class Main
{
static int m;
public static void main(String []args)
{
Scanner cin = new Scanner(System.in);
int T = cin.nextInt();
for(int i = 0; i < T; i++)
{
m = cin.nextInt();
int n = cin.nextInt();
int output = 0;
for(int j = 0; j < n; j++)
{
int a = cin.nextInt();
int b = cin.nextInt();
output = (output + Mod(a,b))%m;
//在这里要注意:不用
/*
output += Mod(a,b)%m;
output = output%Mod;
*/
}
System.out.println(output);
}
}
static int Mod(int a,int b)
{
int result = 1;
int temp = a;
while(b != 0)
{
temp = temp % m;//这一步不能不写,不写可能爆栈
if((b & 1) != 0)
{
result = (result%m)*(temp%m);//分别除以m,防止爆栈
}
temp = temp*temp%m;//除了m,防止爆栈
b = b>>1;
}
return result;
}
}
import java.util.Scanner;
publicclass Main
{
staticint m;
public static void main(String []args)
{
Scanner cin = new Scanner(System.in);
int T = cin.nextInt();
for; i < T; i++)
{
m = cin.nextInt();
int n = cin.nextInt();
int;
for; j < n; j++)
{
int a = cin.nextInt();
int b = cin.nextInt();
output = (output + Mod(a,b))%m;
}
System.out.println(output);
}
}
static int Mod(int a,int b)
{
int;
int temp = a;
while)
{
temp = temp % m;
if)
{
result = (result%m)*(temp%m);
}
temp = temp*temp%m;
b = b>>1;
}
return result;
}
}
快速幂的求解-java方法(int范围之内)的更多相关文章
- POJ 3233 Matrix Power Series (矩阵快速幂+二分求解)
题意:求S=(A+A^2+A^3+...+A^k)%m的和 方法一:二分求解S=A+A^2+...+A^k若k为奇数:S=(A+A^2+...+A^(k/2))+A^(k/2)*(A+A^2+...+ ...
- 快速乘+快速幂(用于模数超过int范围)
一般的快速幂并不适合模数大于int范围的情况,因为在乘法运算的过程可能会出现超出long long的情况出现.这个时候可以利用快速幂的思想使用快速乘,原理就是模拟乘法运算,将乘法运算分解成加法运算,再 ...
- Colossal Fibonacci Numbers! UVA - 11582(快速幂,求解)
Problem Description The i’th Fibonacci number f(i) is recursively defined in the following way: •f(0 ...
- HDU--杭电--4506--小明系列故事——师兄帮帮忙--快速幂取模
小明系列故事——师兄帮帮忙 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) To ...
- 矩阵快速幂在ACM中的应用
矩阵快速幂在ACM中的应用 16计算机2黄睿博 首发于个人博客http://www.cnblogs.com/BobHuang/ 作为一个acmer,矩阵在这个算法竞赛中还是蛮多的,一个优秀的算法可以影 ...
- POJ_Fibonacci POJ_3070(矩阵快速幂入门题,附上自己写的矩阵模板)
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10521 Accepted: 7477 Descri ...
- 【递推+矩阵快速幂】【HDU2604】【Queuing】
Queuing Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...
- hdu 6185 递推+【矩阵快速幂】
<题目链接> <转载于 >>> > 题目大意: 让你用1*2规格的地毯去铺4*n规格的地面,告诉你n,问有多少种不同的方案使得地面恰好被铺满且地毯不重叠.答案 ...
- HDU 5434 Peace small elephant 状压dp+矩阵快速幂
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant Accepts: 38 Submissions: ...
随机推荐
- OCX ClassId查看
1.源码中查看 2.注册完成后在注册表中查看 a.windows键+R 打开“运行” b.输入“regedit” ,回车 c. d.再输入框中输入ocx名称,点击查找下一个 e.展开,点击CLSI ...
- 运行django新的项目,页面总是显示以前的项目,问题解决
运行django新的项目,页面总是显示以前的项目 只需打开任务管理器,再进程中关闭python.exe 再次重新启动服务,python manage.py runserver.即可
- Python3基础 list append 向尾部添加一个元素
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- Google Protobuf结合Netty实践
1.Win版Protobuf代码生成工具下载: https://github.com/protocolbuffers/protobuf/releases 注意下载protoc-3.6.1-win32. ...
- P4822 [BJWC2012]冻结
思路 和p4568类似的分层图最短路 从上一层向下一层连边权/2的边即可 代码 #include <cstdio> #include <algorithm> #include ...
- Multi-attention Network for One Shot Learning
Multi-attention Network for One Shot Learning 2018-05-15 22:35:50 本文的贡献点在于: 1. 表明类别标签信息对 one shot l ...
- (转载)用C#实现MySQL建库及建表
最近做一个项目,为了方便用户使用,希望可以在系统初始化的时候,自动实现MySQL数据库的建库和建表操作.在网上查了很多资料都没有找到合适的,偶尔在一个国外网站上看到了相关的内容,特把实现方法整理如下: ...
- MongoDB集群配置笔记二(实战)
单台mongodb配置文件: dbpath=/opt/mongodb/data logpath=/opt/mongodb/logs/mongodb.log logappend=true fork=tr ...
- Docker:Deploy your app
Prerequisites Install Docker. Get Docker Compose as described in Part 3 prerequisites. Get Docker Ma ...
- Kubernetes简介
Kubernetes is an open-source platform designed to automate deploying, scaling, and operating applica ...