快速幂的求解-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: ...
随机推荐
- 【Dalston】【第七章】分布式链路跟踪(Sleuth)
当我们进行微服务架构开发时,通常会根据业务来划分微服务,各业务之间通过REST进行调用.一个用户操作,可能需要很多微服务的协同才能完成,如果在业务调用链路上任何一个微服务出现问题或者网络超时,都会导致 ...
- JavaI/O(输入/输出)
File类 通过File类可以在程序中操作文件和目录,File能新建.删除.重命名文件和目录,但是不能访问文件内容本身. 理解I/O流 流(stream)是从起源(source)到接收(sink)的有 ...
- 论文笔记:Multi-Agent Actor-Critic for Mixed Cooperative-Competitive Environments
Multi-Agent Actor-Critic for Mixed Cooperative-Competitive Environments 2017-10-25 16:38:23 [Proj ...
- (转)Introduction to Gradient Descent Algorithm (along with variants) in Machine Learning
Introduction Optimization is always the ultimate goal whether you are dealing with a real life probl ...
- matplotlib python
#导入包 import matplotlib.pyplot as plt import numpy as np # 从[-1,1]中等距去50个数作为x的取值 x = np.linspace(-1, ...
- JavaScript 调试常见报错以及原因
JavaScript 调试常见报错以及原因 测试环境 chrome 版本 66.0.3359.170(正式版本) (64 位) TypeError 类型错误 不是操作符所接受的数据类型. //---- ...
- Leetcode122-Best Time to Buy and Sell Stock II-Easy
Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...
- JMeter 生成精度度为分钟的时间戳文件名
import java.util.Calendar; import java.util.Date; import java.util.TimeZone; import java.text.Simple ...
- jQuery从0到1
jQuery不需要安装,把下载的jquery.js文件放在网站上的一个公共的位置,需要在某个页面使用jQuery时,再相对引用即可.——其中有压缩版和未压缩版,分别用于开发和发布.http://jqu ...
- Echarts 地图上显示数值
Echarts 地图上展示数值,效果如下: 上代码:关键代码用红色 series: [ { //name: '香港18区人口密度', type: 'map', mapType: 'jiangsu', ...