結果
問題 | 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 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 55 ms
50,204 KB |
testcase_01 | AC | 56 ms
49,872 KB |
testcase_02 | AC | 57 ms
50,092 KB |
testcase_03 | AC | 108 ms
52,512 KB |
testcase_04 | AC | 55 ms
49,788 KB |
testcase_05 | AC | 57 ms
50,248 KB |
testcase_06 | AC | 57 ms
50,164 KB |
testcase_07 | AC | 57 ms
50,120 KB |
testcase_08 | AC | 56 ms
49,968 KB |
testcase_09 | AC | 56 ms
50,052 KB |
testcase_10 | AC | 56 ms
49,976 KB |
testcase_11 | AC | 55 ms
50,264 KB |
testcase_12 | AC | 55 ms
50,220 KB |
testcase_13 | AC | 56 ms
50,104 KB |
testcase_14 | AC | 54 ms
50,304 KB |
testcase_15 | AC | 57 ms
50,172 KB |
testcase_16 | AC | 55 ms
50,048 KB |
testcase_17 | AC | 89 ms
51,812 KB |
testcase_18 | AC | 163 ms
56,364 KB |
testcase_19 | AC | 135 ms
56,180 KB |
testcase_20 | AC | 97 ms
51,536 KB |
testcase_21 | AC | 101 ms
51,844 KB |
testcase_22 | AC | 169 ms
56,044 KB |
testcase_23 | AC | 481 ms
59,956 KB |
testcase_24 | AC | 740 ms
61,184 KB |
testcase_25 | AC | 416 ms
57,248 KB |
testcase_26 | AC | 390 ms
59,720 KB |
testcase_27 | AC | 1,097 ms
72,632 KB |
testcase_28 | AC | 800 ms
64,232 KB |
testcase_29 | AC | 57 ms
50,108 KB |
testcase_30 | AC | 54 ms
50,104 KB |
testcase_31 | AC | 1,027 ms
63,912 KB |
testcase_32 | AC | 1,067 ms
63,564 KB |
testcase_33 | AC | 1,080 ms
64,096 KB |
testcase_34 | AC | 1,230 ms
64,068 KB |
testcase_35 | AC | 1,268 ms
64,300 KB |
ソースコード
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 = elong 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 = elong 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 = elong 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)); }}