結果

問題 No.886 Direct
ユーザー uwiuwi
提出日時 2019-09-13 22:00:45
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,238 ms / 4,000 ms
コード長 6,789 bytes
コンパイル時間 6,418 ms
コンパイル使用メモリ 80,064 KB
実行使用メモリ 68,092 KB
最終ジャッジ日時 2023-09-17 07:16:17
合計ジャッジ時間 18,103 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
49,308 KB
testcase_01 AC 42 ms
49,376 KB
testcase_02 AC 43 ms
49,208 KB
testcase_03 AC 105 ms
53,872 KB
testcase_04 AC 43 ms
49,468 KB
testcase_05 AC 42 ms
49,248 KB
testcase_06 AC 42 ms
49,188 KB
testcase_07 AC 43 ms
47,404 KB
testcase_08 AC 42 ms
49,204 KB
testcase_09 AC 43 ms
49,520 KB
testcase_10 AC 42 ms
49,328 KB
testcase_11 AC 42 ms
49,220 KB
testcase_12 AC 42 ms
49,228 KB
testcase_13 AC 42 ms
49,180 KB
testcase_14 AC 43 ms
49,420 KB
testcase_15 AC 42 ms
49,200 KB
testcase_16 AC 43 ms
49,396 KB
testcase_17 AC 87 ms
52,904 KB
testcase_18 AC 150 ms
57,036 KB
testcase_19 AC 117 ms
56,276 KB
testcase_20 AC 84 ms
51,652 KB
testcase_21 AC 84 ms
52,452 KB
testcase_22 AC 150 ms
53,908 KB
testcase_23 AC 461 ms
58,188 KB
testcase_24 AC 750 ms
62,576 KB
testcase_25 AC 433 ms
59,608 KB
testcase_26 AC 411 ms
58,268 KB
testcase_27 AC 957 ms
66,720 KB
testcase_28 AC 855 ms
68,092 KB
testcase_29 AC 43 ms
49,424 KB
testcase_30 AC 41 ms
49,420 KB
testcase_31 AC 1,065 ms
64,876 KB
testcase_32 AC 995 ms
62,128 KB
testcase_33 AC 1,033 ms
64,664 KB
testcase_34 AC 1,201 ms
65,420 KB
testcase_35 AC 1,238 ms
65,096 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package contest190913;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.InputMismatchException;
import java.util.Map;

public class E {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		// a, b
		// gcd(a, b) = 1
		// sum (h-a)*(w-b)*2
		// + (h-1)w + w(h-1)
		
		long H = nl(), W = nl();
		int mod = 1000000007;
		cache = new HashMap<>();
		long ab = dfsab(H, W);
		cache = new HashMap<>();
		long a = dfsa(H, W);
		long b = dfsa(W, H);
		cache = new HashMap<>();
		long C = dfs(H, W);
		long ans = (2*H*W%mod*C - 2*W*a - 2*H*b + 2*ab + (H-1)*W + H*(W-1)) % mod;
		if(ans < 0)ans += mod;
		
//		tr(a);
//		long q = 0;
//		long r = 0;
//		long s = 0;
//		for(int i = 1;i <= H;i++) {
//			for(int j = 1;j <= W;j++) {
//				if(gcd(i, j) == 1) {
//					q += (long)i*j;
//					r += (long)i;
//					s += 1;
//				}
//			}
//		}
//		tr(ab, a, C);
//		tr(q%mod, r%mod, s%mod);
		out.println(ans);
	}
	
	public static int gcd(int a, int b) {
		while (b > 0) {
			int c = a;
			a = b;
			b = c % b;
		}
		return a;
	}
	
	
	int mod = 1000000007;
	
	long dfsab(long H, long W)
	{
		if(H == 0 || W == 0)return 0;
		long code = H<<32|W;
		if(cache.containsKey(code))return cache.get(code);
		if(H == 1) {
			return s1(W, mod);
		}
		if(W == 1) {
			return s1(H, mod);
		}
		
		int S = (int)Math.sqrt(Math.max(H, W));
		long all = s1(H, mod) * s1(W, mod) % mod;
		for(int i = 2;i <= S;i++) {
			all -= (long)i*i*dfsab(H/i, W/i);
			all %= mod;
		}
		for(long x = Math.min(H, W), d = H/x, e = W/x;;) {
			// H/x = d, W/x = e
			long ned = H/(d+1);
			long nee = W/(e+1);
			long R = Math.max(ned, nee);
			R = Math.max(R, S);
			// (R, x]
			if(R > x)break;
			all -= dfsab(d, e) * (s2(x, mod) - s2(R, mod));
//			tr(R, x, d, e);
			all %= mod;
			if(R == S)break;
			x = R;
			if(ned > nee) {
				d++;
			}else {
				e++;
			}
		}
		if(all < 0)all += mod;
		cache.put(code, all);
		return all;
	}
	
	long dfsa(long H, long W)
	{
		if(H == 0 || W == 0)return 0;
		long code = H<<32|W;
		if(cache.containsKey(code))return cache.get(code);
		if(H == 1) {
			return W;
		}
		if(W == 1) {
			return s1(H, mod);
		}
		
		int S = (int)Math.sqrt(Math.max(H, W));
		long all = s1(H, mod) * W % mod;
		for(int i = 2;i <= S;i++) {
			all -= (long)i*dfsa(H/i, W/i);
			all %= mod;
		}
		for(long x = Math.min(H, W), d = H/x, e = W/x;;) {
			// H/x = d, W/x = e
			long ned = H/(d+1);
			long nee = W/(e+1);
			long R = Math.max(ned, nee);
			R = Math.max(R, S);
			if(R > x)break;
			// (R, x]
			all -= dfsa(d, e) * (s1(x, mod) - s1(R, mod));
			all %= mod;
			if(R == S)break;
			x = R;
			if(ned > nee) {
				d++;
			}else {
				e++;
			}
		}
		if(all < 0)all += mod;
		cache.put(code, all);
		return all;
	}
	
	long dfs(long H, long W)
	{
		if(H == 0 || W == 0)return 0;
		long code = H<<32|W;
		if(cache.containsKey(code))return cache.get(code);
		if(H == 1) {
			return W;
		}
		if(W == 1) {
			return H;
		}
		
		int S = (int)Math.sqrt(Math.max(H, W));
		long all = H * W % mod;
		for(int i = 2;i <= S;i++) {
			all -= (long)dfs(H/i, W/i);
			all %= mod;
		}
		for(long x = Math.min(H, W), d = H/x, e = W/x;;) {
			// H/x = d, W/x = e
			long ned = H/(d+1);
			long nee = W/(e+1);
			long R = Math.max(ned, nee);
			R = Math.max(R, S);
			if(R > x)break;
			// (R, x]
//			tr(R, x, d, e);
			all -= dfs(d, e) * (x-R);
			all %= mod;
			if(R == S)break;
			x = R;
			if(ned > nee) {
				d++;
			}else {
				e++;
			}
		}
		if(all < 0)all += mod;
		cache.put(code, all);
		return all;
	}
	
	public static long s2(long n, int mod)
	{
		long a = n, b = n+1, c = 2*n+1;
		if(a % 2 == 0){
			a /= 2;
		}else{
			b /= 2;
		}
		if(a % 3 == 0){
			a /= 3;
		}else if(b % 3 == 0){
			b /= 3;
		}else{
			c /= 3;
		}
		return (a%mod)*(b%mod)%mod*(c%mod)%mod;
	}

	
	Map<Long, Long> cache;
	
	public static long s1(long n, int mod)
	{
		if(n % 2 == 0){
			return (n/2%mod)*((n+1)%mod)%mod;
		}else{
			return (n%mod)*((n+1)/2%mod)%mod;
		}
	}

	
	void run() throws Exception
	{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//		Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){
//			@Override
//			public void run() {
//				long s = System.currentTimeMillis();
//				solve();
//				out.flush();
//				if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//			}
//		};
//		t.start();
//		t.join();
	}
	
	public static void main(String[] args) throws Exception { new E().run(); }
	
	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;
	
	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
	
	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private long[] nal(int n)
	{
		long[] a = new long[n];
		for(int i = 0;i < n;i++)a[i] = nl();
		return a;
	}
	
	private char[][] nm(int n, int m) {
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private int[][] nmi(int n, int m) {
		int[][] map = new int[n][];
		for(int i = 0;i < n;i++)map[i] = na(m);
		return map;
	}
	
	private int ni() { return (int)nl(); }
	
	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}
0