結果

問題 No.864 四方演算
ユーザー lavoxlavox
提出日時 2021-08-31 19:14:50
言語 Java21
(openjdk 21)
結果
AC  
実行時間 98 ms / 1,000 ms
コード長 3,298 bytes
コンパイル時間 2,114 ms
コンパイル使用メモリ 78,228 KB
実行使用メモリ 51,732 KB
最終ジャッジ日時 2024-11-26 02:34:03
合計ジャッジ時間 6,078 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
51,044 KB
testcase_01 AC 81 ms
51,164 KB
testcase_02 AC 95 ms
51,228 KB
testcase_03 AC 98 ms
51,584 KB
testcase_04 AC 91 ms
51,492 KB
testcase_05 AC 98 ms
51,428 KB
testcase_06 AC 91 ms
51,460 KB
testcase_07 AC 89 ms
51,592 KB
testcase_08 AC 84 ms
51,628 KB
testcase_09 AC 96 ms
50,996 KB
testcase_10 AC 93 ms
51,496 KB
testcase_11 AC 84 ms
51,056 KB
testcase_12 AC 94 ms
51,364 KB
testcase_13 AC 89 ms
51,356 KB
testcase_14 AC 96 ms
51,204 KB
testcase_15 AC 93 ms
51,412 KB
testcase_16 AC 98 ms
51,296 KB
testcase_17 AC 88 ms
51,436 KB
testcase_18 AC 87 ms
51,732 KB
testcase_19 AC 93 ms
51,328 KB
testcase_20 AC 91 ms
51,188 KB
testcase_21 AC 92 ms
51,360 KB
testcase_22 AC 91 ms
51,372 KB
testcase_23 AC 90 ms
51,540 KB
testcase_24 AC 91 ms
51,552 KB
testcase_25 AC 88 ms
51,204 KB
testcase_26 AC 90 ms
50,848 KB
testcase_27 AC 79 ms
51,116 KB
testcase_28 AC 81 ms
50,988 KB
testcase_29 AC 80 ms
50,972 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