ACdream 1007 a+b 快速幂 java秒啊,快速幂 避免 负数移位出错
( sigma (ai^x) ) % mod
1 import java.util.*;
2 import java.math.*;
3 import java.io.*;
4 public class Main
5 {
6 static BigInteger mod=new BigInteger("10000000007");
7 public static void main(String[] args)
8 {
9 // 对于大量输入,下面方式可能会快一些。
10 Scanner cin=new Scanner(new BufferedInputStream(System.in));
11 int T,n;
12 BigInteger k,sum,x;
13 T=cin.nextInt();
14 while(T-->0)
15 {
16 n=cin.nextInt();
17 k=cin.nextBigInteger();
18 sum=BigInteger.ZERO;
19 for(int i=0;i<n;i++)
20 {
21 x=cin.nextBigInteger();
22 sum=sum.add(x.modPow(k,mod)).mod(mod);
23 }
24 System.out.println(sum);
25 }
26 cin.close();
27 }
28 }
C++ time :192ms
1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <cmath>
5 #include <algorithm>
6 #include <string>
7 #include <vector>
8 #include <set>
9 #include <map>
10 #include <stack>
11 #include <queue>
12 #include <sstream>
13 #include <iomanip>
14 using namespace std;
15 typedef long long LL;
16 const int INF = 0x4fffffff;
17 const double EXP = 1e-5;
18 const int MS = 1005;
19 const int SIZE = 100005;
20 const LL mod=10000000007;
21
22 LL mul_mod(LL x,LL n)
23 {
24 x%=mod;
25 LL res=0;
26 while(n)
27 {
28 if(n&(1LL))
29 {
30 res=(res+x)%mod;
31 }
32 x=((x<<1LL)%mod); // 千万注意,当x为负数的时候,会出错。需要转正
33 n>>=1LL;
34 }
35 return res;
36 }
37
38
39 LL pow_mod(LL x,LL n)
40 {
41 LL res=1LL;
42 x%=mod;
43 while(n)
44 {
45 if(n&(1LL))
46 // res=res*x%mod;
47 res=mul_mod(res,x);
48 // x=x*x%mod;
49 x=mul_mod(x,x);
50 n>>=1LL;
51 }
52 return res;
53 }
54
55 int main()
56 {
57 LL T;
58 LL n,k;
59 scanf("%lld",&T);
60 while(T--)
61 {
62 scanf("%lld%lld",&n,&k);
63 LL ans=0LL,x;
64 for(LL i=0;i<n;i++)
65 {
66 scanf("%lld",&x);
67 if(x>=0)
68 ans=(pow_mod(x,k)+ans)%mod;
69 else
70 {
71 if(k%2==0)
72 ans=(pow_mod(-x,k)+ans)%mod;
73 else
74 ans=(-pow_mod(-x,k)+ans)%mod;
75 }
76 }
77 printf("%lld\n",(ans+mod)%mod);
78 }
79 return 0;
80 }
ACdream 1007 a+b 快速幂 java秒啊,快速幂 避免 负数移位出错的更多相关文章
- hdu 5187 zhx's contest [ 找规律 + 快速幂 + 快速乘法 || Java ]
传送门 zhx's contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 求幂大法,矩阵快速幂,快速幂模板题--hdu4549
hdu-4549 求幂大法.矩阵快速幂.快速幂 题目 M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 ...
- 用于快速排查Java的CPU性能问题(top us值过高)
转载于GIT路径 https://github.com/oldratlee/useful-scripts/blob/master/docs/java.md#beer-show-busy-java-th ...
- 三元组表压缩存储稀疏矩阵实现稀疏矩阵的快速转置(Java语言描述)
三元组表压缩存储稀疏矩阵实现稀疏矩阵的快速转置(Java语言描述) 用经典矩阵转置算法和普通的三元组矩阵转置在时间复杂度上都是不乐观的.快速转置算法在增加适当存储空间后实现快速转置具体原理见代码注释部 ...
- 【Java线程池快速学习教程】
1. Java线程池 线程池:顾名思义,用一个池子装载多个线程,使用池子去管理多个线程. 问题来源:应用大量通过new Thread()方法创建执行时间短的线程,较大的消耗系统资源并且系统的响应速度变 ...
- Java程序员快速入门Go语言
这篇文章帮助Java程序员快速入门Go语言. 转载至 开源中国社区. http://www.oschina.net 本文将以一个有代表性的例子为开始,以此让Java程序员对Go语言有个初步认识,随后将 ...
- 9天快速入门java
Java入门教程[9天快速入门JAVA] §1.3.简单的Java程序 下面我们先介绍两个简单的Java程序,并对其进行分析. 例1.1. public class HelloWorldApp{//a ...
- ACdream 1007
input T <=10 n k n<=1000 k<=10^18 a1,a2,...an | ...
- Java 程序员快速上手 Kotlin 11 招
欢迎大家关注腾讯云技术社区-博客园官方主页,我们将持续在博客园为大家推荐技术精品文章哦~ 作者:霍丙乾 近经常会收到一些 "用 Kotlin 怎么写" 的问题,作为有经验的程序员, ...
随机推荐
- sed -n "29496,29516p" service.log:从29496行开始检索,到29516行结束
在工作中常用的Linux命令 javalinux 发布于 2019-07-24 约 11 分钟 前言 只有光头才能变强. 文本已收录至我的GitHub仓库,欢迎Star:https://gith ...
- 012.Python的字典和集合的相关函数
一 字典的相关函数 1.1 增函数 dictvar = {"a":1,"b":2} dictvar["c"] = 3 print(dictv ...
- Docker-Compose入门-(转载)
Compose 是一个用户定义和运行多个容器的 Docker 应用程序.在 Compose 中你可以使用 YAML 文件来配置你的应用服务.然后,只需要一个简单的命令,就可以创建并启动你配置的所有服务 ...
- svg 飞线,源码
<html> <head> <meta charset="utf-8" /> <meta name="viewport" ...
- Zabbix 5.0:磁盘读写监控
Blog:博客园 个人 自动发现磁盘 配置键值 注意:此键值仅支持Linux平台. 此发现键值返回两个宏 : {#DEVNAME} :设备名 {#DEVTYPE} :设备类型 例如: [ { &quo ...
- Python for循环 - Python零基础入门教程
目录 一.for 循环语法 二.for 循环实战 三.重点总结 四.猜你喜欢 零基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门 在 Python ...
- 使用Typora编写Markdown你真的会了吗
目录 Typora 介绍 使用 常用快捷键 概述 标题 一级标题 二级标题 方式(推荐) 一级标题 二级标题 三级标题 四级标题 五级标题 六级标题 段落 粗体斜体删除线 下划线 注释 分割线 脚注 ...
- 在Spring Bean实例过程中,如何使用反射和递归处理的Bean属性填充?
作者:小傅哥 博客:https://bugstack.cn 沉淀.分享.成长,让自己和他人都能有所收获! <Spring 手撸专栏>目录 [x] 第 1 章:开篇介绍,我要带你撸 Spri ...
- IDEA中怎么创建ini文件
首先博主在这使用的是idea的2019.3.2的版本,不知道的话可以打开help菜单的about查看 第一步: 具体需要在setings安装ini插件 第二步: 在File Types中查看ini,没 ...
- CPU消耗,跟踪定位理论与实践
CPU消耗,跟踪定位理论与实践 一.性能指标之资源指标定位方案 1.打tprof报告方法 抓取perfpmr文件 60秒. perfpmr.sh 60 从结果文件中取出tprof.sum 或直接抓取t ...