快速幂的求解-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: ...
随机推荐
- Btrfs管理及应用
一.btrfs基本概念 btrfs文件系统是2007年Oracle开发,支持GPL协议,为了取代Linux早期的ext系列文件系统. btrfs核心特性: 多物理卷支持:btrfs可由多个底层物理卷组 ...
- CodeChef - MRO Method Resolution Order(打表)
题意:有一种关系叫继承,那么继承父类的同时也会继承他的一个函数f,能继承任意多个父类或不继承,但不能继承自己的子类.现在规定一个列表,这个列表必须以1~N的顺序排列,并且父类不会排在子类后面,1含有一 ...
- 【做题】agc008f - Black Radius——计数&讨论&思维
原文链接 https://www.cnblogs.com/cly-none/p/9794411.html \[ \newcommand{\stif}[2]{\left[ \begin{matrix} ...
- SQL中的字母的大小写转换
http://blog.csdn.net/dxb601/article/details/52086830 update 表名 set 字段名a= Lower(字段a) 2.将小写字母转化成大写字母 ...
- P4492 [HAOI2018]苹果树
思路 题目要求的其实就是每种方案的权值之和(因为每种方案的概率相等) 所以自然想到要求所有的边对最终答案的贡献次数 考虑这一条边被经过了多少次,有这个子树内的点数*子树外的点数次,即\(k\times ...
- Cisco 2960交换机配置
一. 基本操作 Switch(config)#hostname test01(交换机名称) //全局模式下修改交换机名称 Switch(config)#enable secret 123456 //全 ...
- Optical Flow related Tutorials
Optical Flow related Tutorials 2017-04-01 10:50:55 Reference: 1. http://blog.csdn.net/carson2005/art ...
- SalGAN: Visual saliency prediction with generative adversarial networks
SalGAN: Visual saliency prediction with generative adversarial networks 2017-03-17 摘要:本文引入了对抗网络的对抗训练 ...
- spring配置redis
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- 当图片加载失败时更换图片, Firefox onerror 报错
当图片加载失败时更换图片. <!DOCTYPE html> <meta charset="UTF-8"> <img src="http:// ...