思想就是,将十进制数化成二进制数。其它就是很简单了。

如: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范围之内)的更多相关文章

  1. 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+...+ ...

  2. 快速乘+快速幂(用于模数超过int范围)

    一般的快速幂并不适合模数大于int范围的情况,因为在乘法运算的过程可能会出现超出long long的情况出现.这个时候可以利用快速幂的思想使用快速乘,原理就是模拟乘法运算,将乘法运算分解成加法运算,再 ...

  3. Colossal Fibonacci Numbers! UVA - 11582(快速幂,求解)

    Problem Description The i’th Fibonacci number f(i) is recursively defined in the following way: •f(0 ...

  4. HDU--杭电--4506--小明系列故事——师兄帮帮忙--快速幂取模

    小明系列故事——师兄帮帮忙 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) To ...

  5. 矩阵快速幂在ACM中的应用

    矩阵快速幂在ACM中的应用 16计算机2黄睿博 首发于个人博客http://www.cnblogs.com/BobHuang/ 作为一个acmer,矩阵在这个算法竞赛中还是蛮多的,一个优秀的算法可以影 ...

  6. POJ_Fibonacci POJ_3070(矩阵快速幂入门题,附上自己写的矩阵模板)

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10521   Accepted: 7477 Descri ...

  7. 【递推+矩阵快速幂】【HDU2604】【Queuing】

    Queuing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  8. hdu 6185 递推+【矩阵快速幂】

    <题目链接> <转载于 >>> > 题目大意: 让你用1*2规格的地毯去铺4*n规格的地面,告诉你n,问有多少种不同的方案使得地面恰好被铺满且地毯不重叠.答案 ...

  9. HDU 5434 Peace small elephant 状压dp+矩阵快速幂

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5434 Peace small elephant  Accepts: 38  Submissions: ...

随机推荐

  1. (转)Nginx学习

    (二期)15.负载均衡nginx [课程15]nginx安装.xmind0.2MB [课程15]Nginx能做什么.xmind0.1MB [课程15]负载均衡nginx.xmind96.7KB [课程 ...

  2. 【问题解决:死锁】Lock wait timeout exceeded; try restarting transaction的问题

    执行数据删除操作时一直超时并弹出Lock wait timeout exceeded; try restarting transaction错误 解决办法 1.先查看数据库的事务隔离级别 select ...

  3. 【matlab】笔记_1

    基本操作 ans 最近计算的答案 clc 清除命令行窗口 diary 将命令行窗口文本保存到文件中 矩阵 用逗号 (,) 或空格分隔各行元素. 用分号(;)分隔各列元素. a':装置矩阵. 要执行元素 ...

  4. 【C#】扩展方法浅谈

    C#3 引入的扩展方法这一个理念. 扩展方法最明显的特征是在方法参数中第一个参数有this声明. 其实C#库中有很多已经是扩展方法了.比如linq中对序列使用的查询语句, where, select等 ...

  5. 17秋 SDN课程 第一次上机作业

    第一题 拓扑: 测试连通性: 第二题 拓扑: 测试连通性: 第三题 拓扑: 测试连通性:

  6. 3、My Scripts

    .用for循环批量修改文件扩展名(P240) .使用专业改名命令rename来实现 .通过脚本实现sshd.rsyslog.crond.network.sysstat服务在开机时自动启动(P244) ...

  7. CentOS 7 安装pip2

    使用yum安装python-pip,但是报错,说没有可用的包 安装epel源 [root@sishen yum.repos.d]# yum install -y epel-release 然后再安装 ...

  8. 浅谈Linux文件系统

    Linux 与其他类 UNIX 系统一样并不区分文件与目录:目录是记录了其他文件名的文件.使用命令 mkdir 创建目录时,若期望创建的目录的名称与现有的文件名(或目录名)重复,则会创建失败. Lin ...

  9. Python缩进与if语句 空格的魅力

    缩进 Python最具特色的是用缩进来标明成块的代码.我下面以if选择结构来举例.if后面跟随条件,如果条件成立,则执行归属于if的一个代码块. 先看C语言的表达方式(注意,这是C,不是Python! ...

  10. ORA-03113: end-of-file on communication channel(归档满处理方法)

    归档放在flash_recovery目录,由于归档占满了闪回目录,数据库启动报错ORA-03113: end-of-file on communication channel tail -1200f ...