计算(x^y)%N

题目描述

```每个测试用例一行

`输出为单行，为取模后数值`

`1 1 2`

`1`

代码实现

```package kuaishou.demo1;

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long x = sc.nextLong(), y = sc.nextLong(), N = sc.nextLong();
long res = 1;
x = x % N;

while (y > 0) {
if (y % 2 == 1)
res = (res * x) % N;
y /= 2;
x = (x * x) % N;
}
System.out.println(res);
}
}```

二分查找

x = 2 return 0

x = 3 return 0

x = 4 return 1

x = 5 return 1

x = 6 return 2

输入描述

```每个测试用例两行

输出描述

`每行一个证书，对应一个测试用例的结果`

代码实现

```package kuaishou.demo2;

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String inputString = sc.nextLine().toString();
String stringArray[] = inputString.split(" ");
int num[] = new int[stringArray.length];
for (int i = 0; i < stringArray.length; i++) {
num[i] = Integer.parseInt(stringArray[i]);
}
int key = sc.nextInt();
System.out.println(findFirstEqualLarger(num, key));
}

public static int findFirstEqualLarger(int[] array, int key) {
int left = 0;
int right = array.length - 1;
while (left <= right) {
int mid = (left + right) / 2;
if (array[mid] >= key) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return left;
}
}```

126 篇文章39 人订阅

0 条评论