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 ...
随机推荐
- scala 隐式参数
def test(implicit name: String = "susu"): Unit ={ println(name);} 三种调用方法如下:test("dudu ...
- 初探Redis-基础类型Hash
Redis存在五种基础类型:字符串(String).队列(List).哈希(Hash).集合(Set).有序集合(Sorted Set).本次列举出Hash的常用操作. Redis官网:https:/ ...
- 目前校园百晓生APP与CSDN软件的对比
不忘初心.注入灵魂 设计之初 在做校园百晓生该APP时,初心是为校园的同学提供便捷的查询校园信息的服务,确实目前的软件已经实现了该功能,但是总感觉缺少一些灵魂,感觉大部分人渴望的重点在于信息而不是自己 ...
- [hdu5448 Marisa’s Cake]多边形面积,公式化简
题意:给一个凸多边形,求任选若干点形成的多边形的面积和. 思路: 按一定方向(顺时针或逆时针)对多边形的顶点进行编号,则多边形的面积计算公式为:f1 x f2 + f2 x f3 + ... fn-1 ...
- [hdu4292]最大流,拆点
题意:给定每个人所喜欢的食物和饮料种类以及每种食物和饮料的数量,一个人需要一种食物和一种饮料(数量为1即可),问最多满足多少人的需要 思路:由于食物和饮料对于人来说需要同时满足,它们是“与”的关系,所 ...
- TP5 order排序
order方法属于模型的连贯操作方法之一,用于对操作的结果排序. ->order('sort desc,id desc') 用法如下: Db::table('think_user')->w ...
- JMeter基于HTML测试报告的生成
1第一步: 下载ant,搭建ant环境(path的环境变量) 在path中添加ANT_HOME\bin 验证是否搭建成功Lcmd中输入ant 提示这个说明搭建成功. 第二步: 1.jmerer的目录C ...
- Arthas 使用(二) —— 应用场景
1. ognl获取bean SpringContextUtil,通常代码中会有类似这样的工具类用来获取 bean 实例 @Component public class SpringContextUti ...
- SpringBoot+SpringCloud面试题整理
什么是SpringBoot?1.用来简化spring初始搭建和开发过程使用特定的方式进行配置(properties或者yml文件)2.创建独立的spring引用程序main方法运行3.嵌入Tomcat ...
- 2.Scrapy基本命令介绍
1.安装scrapy框架 a.安装wheel pip install wheel -i https://pypi.douban.com/simple/ b.安装twisted pip install ...