結果

問題 No.864 四方演算
ユーザー lavoxlavox
提出日時 2021-08-31 19:14:50
言語 Java21
(openjdk 21)
結果
AC  
実行時間 94 ms / 1,000 ms
コード長 3,298 bytes
コンパイル時間 2,738 ms
コンパイル使用メモリ 74,984 KB
実行使用メモリ 52,508 KB
最終ジャッジ日時 2023-08-17 07:39:27
合計ジャッジ時間 6,920 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
51,576 KB
testcase_01 AC 80 ms
51,700 KB
testcase_02 AC 91 ms
52,112 KB
testcase_03 AC 89 ms
52,116 KB
testcase_04 AC 89 ms
51,628 KB
testcase_05 AC 82 ms
52,316 KB
testcase_06 AC 90 ms
52,380 KB
testcase_07 AC 85 ms
52,352 KB
testcase_08 AC 91 ms
52,128 KB
testcase_09 AC 84 ms
52,220 KB
testcase_10 AC 83 ms
52,368 KB
testcase_11 AC 83 ms
52,036 KB
testcase_12 AC 93 ms
52,068 KB
testcase_13 AC 89 ms
52,140 KB
testcase_14 AC 94 ms
52,344 KB
testcase_15 AC 84 ms
52,044 KB
testcase_16 AC 82 ms
52,508 KB
testcase_17 AC 74 ms
51,520 KB
testcase_18 AC 86 ms
51,612 KB
testcase_19 AC 89 ms
51,688 KB
testcase_20 AC 87 ms
51,596 KB
testcase_21 AC 88 ms
51,604 KB
testcase_22 AC 87 ms
51,840 KB
testcase_23 AC 73 ms
51,712 KB
testcase_24 AC 89 ms
51,548 KB
testcase_25 AC 89 ms
51,468 KB
testcase_26 AC 75 ms
51,616 KB
testcase_27 AC 68 ms
51,720 KB
testcase_28 AC 78 ms
51,536 KB
testcase_29 AC 81 ms
51,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.NoSuchElementException;

public class Main {
	public static void main(String[] args) {
		Main o = new Main();
		o.solve();
	}

	public void solve() {
		FastScanner sc = new FastScanner(System.in);
		long N = sc.nextLong();
		long K = sc.nextLong();

		// (a + c)(b + d) = K

		int MAX = 1000005;
		boolean[] p = new boolean[MAX + 1];
		Arrays.fill(p, true);
		ArrayList<Factor> F = new ArrayList<>();
		long K0 = K;
		for ( int n = 2 ; ((long)n) * (long)n <= K0 ; n++ ) {
			if ( !p[n] ) continue;
			p[n] = true;
			int cur = n;
			while ( (cur += n) <= MAX ) {
				p[cur] = false;
			}
			int cnt = 0;
			while ( K0 % (long)n == 0 ) {
				cnt++;
				K0 = K0 / (long) n;
			}
			if ( cnt > 0 ) {
				F.add(new Factor(n, cnt));
			}
		}

		if ( K0 > 1 ) {
			F.add(new Factor(K0, 1));
		}

		long ans = rec(F, 0, 1, N, K);

		System.out.println(ans);
	}

	long rec(ArrayList<Factor> F, int i, long p, long N, long K) {
		if ( i >= F.size() ) {
			long q = K / p;
			return count(p, N) * count(q, N);
		}
		long ret = 0;
		long P = p;
		Factor f = F.get(i);
		for ( int j = 0 ; j <= f.pow ; j++ ) {
			ret += rec(F, i + 1, P, N, K);
			P = P * f.p;
		}
		return ret;
	}

	long count(long n, long N) {
		long min = Math.max(1, n - N);
		long max = Math.min(n - 1, N);
		return Math.max(max - min + 1, 0);
	}

	class Factor {
		long p = 0;
		int pow = 0;
		Factor( long p, int pow ) {
			this.p = p;
			this.pow = pow;
		}
	}

	class FastScanner {
		private final InputStream in;
		private final byte[] buf = new byte[1024];
		private int ptr = 0;
		private int buflen = 0;
		FastScanner( InputStream source ) { this.in = source; }
		private boolean hasNextByte() {
			if ( ptr < buflen ) return true;
			else {
				ptr = 0;
				try { buflen = in.read(buf); } catch (IOException e) { e.printStackTrace(); }
				if ( buflen <= 0 ) return false;
			}
			return true;
		} 
		private int readByte() { if ( hasNextByte() ) return buf[ptr++]; else return -1; } 
		private boolean isPrintableChar( int c ) { return 33 <= c && c <= 126; }
		private boolean isNumeric( int c ) { return '0' <= c && c <= '9'; }
		private void skipToNextPrintableChar() { while ( hasNextByte() && !isPrintableChar(buf[ptr]) ) ptr++; }
		public boolean hasNext() { skipToNextPrintableChar(); return hasNextByte(); }
		public String next() {
			if ( !hasNext() ) throw new NoSuchElementException();
			StringBuilder ret = new StringBuilder();
			int b = readByte();
			while ( isPrintableChar(b) ) { ret.appendCodePoint(b); b = readByte(); }
			return ret.toString();
		}
		public long nextLong() {
			if ( !hasNext() ) throw new NoSuchElementException();
			long ret = 0;
			int b = readByte();
			boolean negative = false;
			if ( b == '-' ) { negative = true; if ( hasNextByte() ) b = readByte(); }
			if ( !isNumeric(b) ) throw new NumberFormatException();
			while ( true ) {
				if ( isNumeric(b) ) ret = ret * 10 + b - '0';
				else if ( b == -1 || !isPrintableChar(b) ) return negative ? -ret : ret;
				else throw new NumberFormatException();
				b = readByte();
			}
		}
		public int nextInt() { return (int)nextLong(); }
		public double nextDouble() { return Double.parseDouble(next()); }
	}
}
0