結果

問題 No.420 mod2漸化式
ユーザー GrenacheGrenache
提出日時 2016-09-09 23:46:40
言語 Java19
(openjdk 21)
結果
AC  
実行時間 128 ms / 1,000 ms
コード長 3,627 bytes
コンパイル時間 3,419 ms
コンパイル使用メモリ 75,820 KB
実行使用メモリ 57,572 KB
最終ジャッジ日時 2023-08-25 23:24:02
合計ジャッジ時間 9,207 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
55,896 KB
testcase_01 AC 123 ms
56,040 KB
testcase_02 AC 123 ms
55,720 KB
testcase_03 AC 123 ms
55,608 KB
testcase_04 AC 124 ms
56,044 KB
testcase_05 AC 125 ms
55,588 KB
testcase_06 AC 124 ms
55,492 KB
testcase_07 AC 123 ms
55,400 KB
testcase_08 AC 124 ms
55,636 KB
testcase_09 AC 124 ms
55,876 KB
testcase_10 AC 124 ms
55,704 KB
testcase_11 AC 124 ms
55,788 KB
testcase_12 AC 120 ms
53,948 KB
testcase_13 AC 128 ms
56,196 KB
testcase_14 AC 122 ms
56,056 KB
testcase_15 AC 123 ms
55,832 KB
testcase_16 AC 122 ms
55,968 KB
testcase_17 AC 125 ms
55,964 KB
testcase_18 AC 125 ms
56,056 KB
testcase_19 AC 123 ms
55,828 KB
testcase_20 AC 124 ms
55,900 KB
testcase_21 AC 123 ms
55,832 KB
testcase_22 AC 124 ms
56,144 KB
testcase_23 AC 124 ms
56,104 KB
testcase_24 AC 123 ms
55,732 KB
testcase_25 AC 122 ms
55,868 KB
testcase_26 AC 122 ms
56,116 KB
testcase_27 AC 123 ms
56,144 KB
testcase_28 AC 121 ms
55,968 KB
testcase_29 AC 121 ms
56,136 KB
testcase_30 AC 123 ms
55,980 KB
testcase_31 AC 124 ms
56,276 KB
testcase_32 AC 124 ms
56,056 KB
testcase_33 AC 124 ms
56,064 KB
testcase_34 AC 123 ms
57,572 KB
testcase_35 AC 122 ms
56,068 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


public class Main_yukicoder420 {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		int x = sc.nextInt();

		PC pc = new PC(31, Integer.MAX_VALUE);

		if (x > 31) {
			pr.printf("%d %d\n", 0, 0);
		} else if (x == 0) {
			pr.printf("%d %d\n", 1, 0);
		} else if (x == 31) {
			long ret = (0x1L << 31) - 1;
			pr.printf("%d %d\n", 1, ret);
		} else {
			long ret = 0;
			long tmp = pc.C(31, x) / 31;
//			for (int i = 0; i < 31; i++) {
//				ret += (0x1L << i) * tmp;
//			}
			ret = ((0x1L << 31) - 1) * tmp * x;

			pr.printf("%d %d\n", pc.C(31, x), ret);
		}
	}

	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		pr = new Printer(System.out);

		solve();

		pr.close();
		sc.close();
	}

	@SuppressWarnings("unused")
    private static class PC {
    	// MOD must be prime number.
    	int MOD;
    	// fact[i] : i! % MOD
    	long[] fact;
    	// ifact[i] : 1/i! % MOD
    	long[] ifact;

    	PC(int size, int MOD) {
    		// O(size)
    		// n=sizeまでのnCrを求める。
    		// nHrはn+r-1Crになってしまうので注意

    		this.MOD = MOD;

    		fact = new long[size + 1];
    		fact[0] = 1;
    		for (int i = 1; i <= size; i++) {
    			fact[i] = fact[i - 1] * i % MOD;
    		}

    		ifact = new long[size + 1];

    		int loop = MOD - 2;
    		long x = fact[size];
    		ifact[size] = 1;
    		while (loop > 0) {
    			if (loop % 2 == 1) {
    				ifact[size] = ifact[size] * x % MOD;
    			}
    			x = x * x % MOD;
    			loop /= 2;
    		}

    		for (int i = size - 1; i >= 0; i--) {
    			ifact[i] = ifact[i + 1] * (i + 1) % MOD;
    		}

    	}

    	// 組合せの数
    	int C(int n, int r) {
    		if (r > n) {
    			return 0;
    		}

    		return (int)(((fact[n] * ifact[n - r]) % MOD) * ifact[r] % MOD);
    	}

    	// 順列
    	int P(int n, int r) {
    		if (r > n) {
    			return 0;
    		}

    		return (int)((fact[n] * ifact[n -r]) % MOD);
    	}

    	// 重複組み合わせ
    	// 異なるn種のものから重複を許してr個を選ぶ場合の数
    	// 0個の種類もあり得る
    	int H(int n, int r) {
    		if (n == 0 && r == 0) {
    			return 1;
    		}

    		return C(n + r - 1, r);
    	}

    	// 組合せの数(nが大きいとき)
    	//   O(r)で求めることができる。rはsizeの大きさまで
    	int C2(long n, int r) {
    		long ret = ifact[r];
    		for (int i = 1; i <= r; i++) {
    			long tmp = (n - r + i) % MOD;
    			ret = (ret * tmp) % MOD;
    		}

    		return (int)ret;
    	}

    	// 第2種スターリング数
    	// n人をちょうどr個のグループに分ける(グループの区別はなし)
    	// グループの区別をする場合はr!S(n,r)。全射の場合の数と同義
    	// O(r log n)
    	int S(long n, int r) {
    		//全射の場合の数を包除原理を使って求めて、1/r!をかける。
    		long ret = 0;
    		for (int i = 1; i <= r; i++) {
    			long tmp = (r - i) % 2 == 0 ? 1 : -1;
    			tmp *= pow(i, n) * C(r, i) % MOD;
    			ret = (ret + tmp + MOD) % MOD;
    		}
    		ret = ret * ifact[r] % MOD;

    		return (int)ret;
    	}

    	long pow(int a, long n) {
    		long loop = n;
    		long ret = 1;
    		long x = a;
    		while (loop > 0) {
    			if (loop % 2 == 1) {
    				ret = ret * x % MOD;
    			}
    			x = x * x % MOD;
    			loop /= 2;
    		}

    		return ret;
    	}
    }

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0