ABC 020 D: LCM Rush

解法

// 最大公約数
static long gcd(long a, long b) {
    return b == 0 ? a : gcd(b, a % b);
}
 
// 最小公倍数
static long lcm(long a, long b) {
    return a * b / gcd(a, b);
}

最大公約数と最小公倍数は上記の手順で求められる。だがこの問題では使わない。

Kの約数xと、Kと互いに素な自然数aについて、LCM(ax, K)=aKとなる事を利用する。

  • Kの約数を大きい方から取り出す。取り出した約数をxとする。
  • 自然数aについて ax = x, 2x, 3x, ... nx (ただしnは nx <= N となる最大の自然数)を列挙する。
  • xの倍数bxが既に取り出されたことがあれば、 x, 2x, 3x, ... nx からbxの倍数を取り除く。
  • x を大きい方から取り出していることから、x
  • よって残ったaxとKの最小公倍数はaKである。これを回答に足しておく。

上記の手順において、ax = x, 2x, 3x, ... nx から前に出てきたものを取り除いて足し合わせる部分は等差数列の和なので計算量は少なくできる。

コード

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

public class Main {

	public static final long MOD = 1000000007;

	public static void main(String[] args) {
		int N = nextInt();
		int K = nextInt();

		ArrayList<Long> divisors = divisors(K);
		long ans = 0;
		long[] sum = new long[divisors.size()];
		for (int i = divisors.size() - 1; i >= 0; i--) {
			// 約数を大きい方から取り出していく
			long x = divisors.get(i);
			long max = N / x * x;// N以下の最大のxの倍数
			sum[i] = (long) (x + max) * (N / x) / 2;// N以下のxの倍数の和

			for (int j = i + 1; j < divisors.size(); j++) {
				// 今まで取り出した約数の中にxの倍数があったら引いておく
				// こうすることでa*xについてaはKと互いに素になる
				if (divisors.get(j) % x == 0) {
					sum[i] -= sum[j];
				}
			}
			// xで割ってから足しておく
			ans = (ans + sum[i] / x) % MOD;
		}
		ans = (ans * K) % MOD;
		System.out.println(ans);
	}

	static ArrayList<Long> divisors(long K) {
		// nの約数を全列挙する
		ArrayList<Long> ret = new ArrayList<>();
		for (long i = 1; i * i <= K; i++) {
			if (K % i != 0) {
				continue;
			}
			ret.add(i);
			if (i * i != K) {
				ret.add(K / i);
			}
		}
		Collections.sort(ret);
		return ret;
	}

	static int nextInt() {
		int c;
		try {
			c = System.in.read();
			while (c != '-' && (c < '0' || c > '9'))
				c = System.in.read();
			if (c == '-')
				return -nextInt();
			int res = 0;
			while (c >= '0' && c <= '9') {
				res = res * 10 + c - '0';
				c = System.in.read();
			}
			return res;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}

}