結果

問題 No.886 Direct
ユーザー uwi
提出日時 2019-09-13 22:00:45
言語 Java
(openjdk 23)
結果
AC  
実行時間 1,268 ms / 4,000 ms
コード長 6,789 bytes
コンパイル時間 5,167 ms
コンパイル使用メモリ 87,508 KB
実行使用メモリ 72,632 KB
最終ジャッジ日時 2024-07-04 04:15:15
合計ジャッジ時間 18,575 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

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