快速幂的求解-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: ...
随机推荐
- 配置Jenkins 实现自动发布maven项目至weblogic(svn+maven+weblogic12c)
Jenkins安装完成之后,需要我们对其配置,然后才可以实现自动部署项目. 前提 防火墙开放weblogic的7001端口 Linux(CentOS):firewall-cmd --zone=publ ...
- Bootstrap3基础 img-thumbnail 给图片加一个圆角的边框
内容 参数 OS Windows 10 x64 browser Firefox 65.0.2 framework Bootstrap 3.3.7 editor ...
- 《AngularJS开发下一代Web应用》读书笔记与感想
该书一共130页打算四天读完,边读边记录. 1. 2.学习MogoDB 3. 4. 5. 创建标识符的一段简单伪码模板: var myModule = angular.module(...); myM ...
- for和while循环的区别
区别:for循环,就是遍历某一对象,通俗说就是根据循环次数限制做多少次重复操作.while循环,是当满足什么条件的时候,才做某种操作. for为遍历循环 while为直到循环
- 【分库、分表】MySQL分库分表方案
一.Mysql分库分表方案 1.为什么要分表: 当一张表的数据达到几千万时,你查询一次所花的时间会变多,如果有联合查询的话,我想有可能会死在那儿了.分表的目的就在于此,减小数据库的负担,缩短查询时间. ...
- Graph Convolutional Networks (GCNs) 简介
Graph Convolutional Networks 2018-01-16 19:35:17 this Tutorial comes from YouTube Video:https://www ...
- 4-Three-Matterhorn man
What was the main objective of early mountain climbers? ①Modern alpinists try to climb mountains b ...
- LOJ 534 花团(线段树+dfs栈)
题意 https://loj.ac/problem/534 思路 又是复杂度错误的一题,\(O(n^2\log n)\) 能过 \(15000\) . 虽然看起来强制在线,其实是一道假的在线题.首先按 ...
- EPPlus实战篇——Excel写入
.net core 项目 可以向excel写入任何类型(T)的数据,只要T中的field的[Display(Name = "1233", Description = "# ...
- CAS实现单点登录SSO执行原理探究超详细
一.不落俗套的开始 1.背景介绍 单点登录:Single Sign On,简称SSO,SSO使得在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统. CAS框架:CAS(Centra ...