Javaメモ（標準入力/関数系）

# Java標準入出力
## 前提
```Java
Scanner sc = new Scanner(System.in);
```
## 整数（Int）の入力
```
a
```
```Java
int a = sc.nextInt();
```
## 整数（Int）の入力
```
b c
```
```Java
int b = sc.nextInt();
int c = sc.nextInt();
```

## 整数（Int）の入力 H×W
```
H W
a1,1 a1,2 a1,3 ... a1,w
.
.
.
ah,1 ah,2 ah,3 ... ah,w

```
```Java
int H = sc.nextInt();
int W = sc.nextInt();
int [][] A = new int[H][W];
for (int i=0;i<H;i++){
    for (int j=0;j<W;j++){
        A[i][j] = Integer.parseInt(sc.next());
    }
}
```


## 整数（Long）の入力
```
A B C
```
```Java
long A = sc.nextLong();
long B = sc.nextLong();
long C = sc.nextLong();
```
## 整数（Long）の3×Q 入力
```
l1 r1 v1
l2 r2 v2
l3 r3 v3
...
```

```Java
long [] L = new long[Q];
long [] R = new long[Q];
long [] V = new long[Q];
        
for (int i=0;i<Q;i++){
    L[i] = Long.parseLong(sc.next());
    R[i] = Long.parseLong(sc.next());
    V[i] = Long.parseLong(sc.next());
}
```

## 整数（Int）縦にN個入力
```
N
a_1　a_2　a_3　a_4
...
```
```Java
int N = sc.nextInt();
int[] a = new int[N];
for (int i=0; i<N; i++){
    a[i] = Integer.parseInt(sc.next());
}
```

## 整数（Int）縦にN個入力
```
N
a_1
a_2
a_3
a_4
...
```
```Java
int N = sc.nextInt();
int[] a = new int[N];
for (int i = 0; i < N; i++) {
  a[i] = sc.nextInt();
}
```

## 文字列の入力
```
hogehoge
```

```Java
String s = sc.next();
```
# 関数
## 最大公約数 gcd
```java
static int gcd(int a, int b){
        if(b == 0){
            return a;
        }
        return gcd(b,a % b);
    }
```

## 最小公倍数 lcm
```java
static int lcm (int a, int b) {
	int temp;
	long c = a;
	c *= b;
	while((temp = a%b)!=0) {
		a = b;
		b = temp;
	}
	return (int)(c/b);
}
```

## ◯進数
```java
// ansを8進数にする
Long.parseLong(ans,8)
```

## Permutation、順列
```java
public static ArrayList<ArrayList<Integer>> permute(int[] arr) {
        ArrayList<ArrayList<Integer>> l = new ArrayList<>();
        permuteHelper(l, new ArrayList<>(), arr);
        return l;
    }
 
private static void permuteHelper(ArrayList<ArrayList<Integer>> list, ArrayList<Integer> resultList, int [] arr){
        // Base case
        if(resultList.size() == arr.length){
            list.add(new ArrayList<>(resultList));
        }
        else{
            for(int i = 0; i < arr.length; i++){
 
                if(resultList.contains(arr[i]))
                {
                    // If element already exists in the list then skip
                    continue;
                }
                // Choose element
                resultList.add(arr[i]);
                // Explore
                permuteHelper(list, resultList, arr);
                // Unchoose element
                resultList.remove(resultList.size() - 1);
            }
        }
    }
```
## a^bの値を、mod(10^9+7)で割った値
```java
final static long mod = 1000000007;
static long binpower(long a, long b){
        long ans = 1;
        while(b!=0){
            if (b%2==1){
                ans = ans * a % mod;
            }
            a = a * a % mod;
            b/=2;
        }
        return ans;
    }
```
参考：https://github.com/E869120/kyopro_educational_90/blob/main/sol/069.cpp

## log2○
```java
public static double log2(int x) {
        return Math.log(x) / Math.log(2);
    }
```
参考：https://www.techiedelight.com/ja/calculate-log-base-2-integer-java/