Java实现 LeetCode 372 超级次方
372. 超级次方
你的任务是计算 ab 对 1337 取模,a 是一个正整数,b 是一个非常大的正整数且会以数组形式给出。
示例 1:
输入: a = 2, b = [3]
输出: 8
示例 2:
输入: a = 2, b = [1,0]
输出: 1024
PS:
我现在饱受数学的折磨,欧拉筛,欧拉函数,
(((φ(◎ロ◎;)φ)))
class Solution {
//372超级次方
public int superPow(int a ,int[] b){
int c = 1337;
int exp = 0;
int phi = euler(c);
//同余
for(int i=0;i<b.length;i++) exp = (exp*10+b[i])%1140;
return qucikPow(a%1337,exp,1337);
}
//快速幂计算
public int qucikPow(int a,int b,int c){
int ans =1;
while(b>0){
if((b&1)==1){
ans = (ans*a)%c;
}
b >>=1;
a = (a*a)%c;
}
return ans;
}
//欧拉函数
public int euler(int n){
int ret= n;
for(int i=2;i*i<n;i++){
if(n%i==0){//n的质因数
ret = ret/n*(n-1); //欧拉函数公式
while(n%i==0){//去掉质因数i
n/=i;
}
}
}
if(n>1){//n本来就是质数 f(n) = n-1;
ret = ret/n*(n-1);
}
return ret;
}
}
Java实现 LeetCode 372 超级次方的更多相关文章
- C#版(击败100.00%的提交) - Leetcode 372. 超级次方 - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. Leetcod ...
- Leetcode 372.超级次方
超级次方 你的任务是计算 ab 对 1337 取模,a 是一个正整数,b 是一个非常大的正整数且会以数组形式给出. 示例 1: 输入: a = 2, b = [3] 输出: 8 示例 2: 输入: a ...
- Java实现 LeetCode 517 超级洗衣机
517. 超级洗衣机 假设有 n 台超级洗衣机放在同一排上.开始的时候,每台洗衣机内可能有一定量的衣服,也可能是空的. 在每一步操作中,你可以选择任意 m (1 ≤ m ≤ n) 台洗衣机,与此同时将 ...
- Java实现 LeetCode 313 超级丑数
313. 超级丑数 编写一段程序来查找第 n 个超级丑数. 超级丑数是指其所有质因数都是长度为 k 的质数列表 primes 中的正整数. 示例: 输入: n = 12, primes = [2,7, ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 214 Shortest Palindrome
Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...
- Java for LeetCode 212 Word Search II
Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...
- Java for LeetCode 211 Add and Search Word - Data structure design
Design a data structure that supports the following two operations: void addWord(word)bool search(wo ...
- Java for LeetCode 210 Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1. Some courses may have prer ...
随机推荐
- Linux Charger IC 驱动移植总结
Linux Charger IC 驱动移植总结 文章目录 Linux Charger IC 驱动移植总结 1 设备树的基本知识 设备树的概念 设备树的基本结构 compatible属性 举个栗子 2 ...
- hadoop问题
如果启动hadoop集群时,无法启动datanode,则可以集群所有节点下dfs.datanode.data.dir的配置值所指示的路径下清空所有文件(夹),然后 hadoop namenode -f ...
- mysql连表查空,查询第二张表中没有第一张表中的数据
select consumer_id,user_name,mobile,invite_code from csr_consumer where invite_count<(select coun ...
- 缓冲 buffer 和缓存 cache 的区别
缓存(cache)是在读取硬盘中的数据时,把最常用的数据保存在内存的缓存区中,再次读取该数据时,就不去硬盘中读取了,而在缓存中读取. 缓冲(buffer)是在向硬盘写入数据时,先把数据放入缓冲区,然后 ...
- angular 实现依赖注入
1:首先获取module对象var myAppModule = angular.module('myApp', []); 2:定义对象(类似spring中xml声明bean对象<bean id= ...
- vue组件中的“:”、“@”、“.”属性
冒号属性 :是指令 v-bind 的缩写,是为了动态绑定数据,用于响应式地更新 HTML 特性. 加了冒号,后面是变量或表达式:不加冒号的是字符串. 如图:将Home组件中的 probe-type 的 ...
- vue 兄弟组件之间的传值
一. 子传父,父传子. 二. 1.兄弟之间传递数据需要借助于事件车,通过事件车的方式传递数据 2.创建一个Vue的实例,让各个兄弟共用同一个事件机制. 3.传递数据方,通过一个事件触发bus.$emi ...
- 关于SpringMVC乱码问题
关于SpringMVC运行Tomcat控制台出现乱码的情况(在网上找到一种方法亲测有效) 找到tomcat文件夹中的conf包下的logging.properties中找到 java.util.log ...
- flask之Flask特殊装饰器
flask_decorators.py ''' Flask中的特殊装饰器: (1)@app.before_request 请求到达视图函数之前,进行自定义操作,类似django中间件中的process ...
- 王艳 201771010127《面向对象程序设计(java)》第一周学习总结
王艳 201771010127<面向对象程序设计(java)>第一周学习总结 第一部分:课程准备部分 填写课程学习 平台注册账号, 平台名称 注册账号 博客园:www.cnblogs.co ...