結果

問題 No.502 階乗を計算するだけ
ユーザー uwiuwi
提出日時 2017-04-08 15:50:35
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 18,414 bytes
コンパイル時間 5,203 ms
コンパイル使用メモリ 90,552 KB
実行使用メモリ 77,792 KB
最終ジャッジ日時 2024-07-17 23:49:49
合計ジャッジ時間 15,192 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
52,568 KB
testcase_01 AC 78 ms
56,108 KB
testcase_02 AC 89 ms
57,024 KB
testcase_03 AC 81 ms
55,896 KB
testcase_04 AC 76 ms
56,320 KB
testcase_05 AC 78 ms
56,564 KB
testcase_06 AC 79 ms
56,264 KB
testcase_07 AC 72 ms
55,916 KB
testcase_08 AC 73 ms
56,892 KB
testcase_09 AC 90 ms
56,804 KB
testcase_10 AC 74 ms
56,224 KB
testcase_11 AC 86 ms
56,796 KB
testcase_12 AC 85 ms
56,964 KB
testcase_13 AC 89 ms
56,936 KB
testcase_14 AC 73 ms
56,020 KB
testcase_15 AC 70 ms
56,160 KB
testcase_16 AC 80 ms
56,072 KB
testcase_17 AC 79 ms
56,016 KB
testcase_18 AC 89 ms
56,992 KB
testcase_19 AC 75 ms
56,248 KB
testcase_20 AC 86 ms
56,596 KB
testcase_21 AC 65 ms
56,076 KB
testcase_22 AC 140 ms
56,884 KB
testcase_23 AC 106 ms
56,976 KB
testcase_24 AC 141 ms
58,900 KB
testcase_25 AC 107 ms
57,472 KB
testcase_26 AC 119 ms
57,232 KB
testcase_27 AC 104 ms
57,856 KB
testcase_28 AC 122 ms
56,836 KB
testcase_29 AC 116 ms
57,720 KB
testcase_30 AC 155 ms
58,936 KB
testcase_31 AC 126 ms
58,792 KB
testcase_32 TLE -
testcase_33 WA -
testcase_34 TLE -
testcase_35 TLE -
testcase_36 TLE -
testcase_37 TLE -
testcase_38 WA -
testcase_39 TLE -
testcase_40 AC 979 ms
70,188 KB
testcase_41 AC 71 ms
56,012 KB
testcase_42 AC 51 ms
52,584 KB
testcase_43 AC 50 ms
52,508 KB
testcase_44 AC 52 ms
52,336 KB
testcase_45 AC 50 ms
52,628 KB
testcase_46 AC 50 ms
52,488 KB
testcase_47 AC 52 ms
52,704 KB
testcase_48 AC 50 ms
52,556 KB
testcase_49 AC 52 ms
52,300 KB
testcase_50 AC 51 ms
52,568 KB
testcase_51 AC 52 ms
52,524 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package contest;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Factorial {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		int mod = 1000000007;
		long nlong = nl();
		if(nlong >= mod){
			out.println(0);
			return;
		}
		if(nlong == 0){
			out.println(1);
			return;
		}
		superPrepare();
		int n = (int)nlong;
		boolean inv = false;
		if(n > mod-n){
			n = mod - n;
			inv = true;
		}
		int s = (int)Math.sqrt(n);
		long[] vs = new long[n/s];
		for(int i = 0;i < n/s;i++){
			vs[i] = 1 + i * s;
		}
		long[] f = buildProductPlus(vs);
		
		long[] cs = new long[s];
		for(int i = 0;i < s;i++)cs[i] = i;
		long[] res = substitute(f, cs);
		
		long ret = 1;
		for(long v : res)ret = ret * v % mod;
		for(int i = s*(n/s)+1;i <= n;i++){
			ret = ret * i % mod;
		}
		if(inv){
			ret = mod-invl(ret, mod);
		}
		out.println(ret);
	}
	
	public static long[] buildProductPlus(long[] vs)
	{
		return dfsBPP(0, vs.length, vs);
	}
	
	private static long[] dfsBPP(int l, int r, long[] vs)
	{
		if(r-l == 1){
			return new long[]{vs[l], 1};
		}else{
			int h = l+r>>1;
			return mul(dfsBPP(l, h, vs), dfsBPP(h, r, vs));
		}
	}
	
	public static int mod = 1000000007;
	public static long[] mul(long[] a, long[] b)
	{
		if(Math.max(a.length, b.length) >= 1500){
			return Arrays.copyOf(convolute3(a, b, mod, null), a.length+b.length-1);
		}else{
			return mulnaive(a, b);
		}
	}
	
	public static long[] mul(long[] a, long[] b, int lim)
	{
		if(Math.max(a.length, b.length) >= 1500){
			return Arrays.copyOf(convolute3(a, b, mod, null), lim);
		}else{
			return mulnaive(a, b, lim);
		}
	}
	
	public static long[] mulnaive(long[] a, long[] b)
	{
		long[] c = new long[a.length+b.length-1];
		long big = 8L*mod*mod;
		for(int i = 0;i < a.length;i++){
			for(int j = 0;j < b.length;j++){
				c[i+j] += a[i]*b[j];
				if(c[i+j] >= big)c[i+j] -= big;
			}
		}
		for(int i = 0;i < c.length;i++)c[i] %= mod;
		return c;
	}
	
	public static long[] mulnaive(long[] a, long[] b, int lim)
	{
		long[] c = new long[lim];
		long big = 8L*mod*mod;
		for(int i = 0;i < a.length;i++){
			for(int j = 0;j < b.length && i+j < lim;j++){
				c[i+j] += a[i]*b[j];
				if(c[i+j] >= big)c[i+j] -= big;
			}
		}
		for(int i = 0;i < c.length;i++)c[i] %= mod;
		return c;
	}
	
	public static long[] add(long[] a, long[] b)
	{
		long[] c = new long[Math.max(a.length, b.length)];
		for(int i = 0;i < a.length;i++)c[i] += a[i];
		for(int i = 0;i < b.length;i++)c[i] += b[i];
		for(int i = 0;i < c.length;i++)if(c[i] >= mod)c[i] -= mod;
		return c;
	}
	
	public static long[] add(long[] a, long[] b, int lim)
	{
		long[] c = new long[lim];
		for(int i = 0;i < a.length && i < lim;i++)c[i] += a[i];
		for(int i = 0;i < b.length && i < lim;i++)c[i] += b[i];
		for(int i = 0;i < c.length;i++)if(c[i] >= mod)c[i] -= mod;
		return c;
	}
	
	public static long[] sub(long[] a, long[] b)
	{
		long[] c = new long[Math.max(a.length, b.length)];
		for(int i = 0;i < a.length;i++)c[i] += a[i];
		for(int i = 0;i < b.length;i++)c[i] -= b[i];
		for(int i = 0;i < c.length;i++)if(c[i] < 0)c[i] += mod;
		return c;
	}
	
	public static long[] sub(long[] a, long[] b, int lim)
	{
		long[] c = new long[lim];
		for(int i = 0;i < a.length && i < lim;i++)c[i] += a[i];
		for(int i = 0;i < b.length && i < lim;i++)c[i] -= b[i];
		for(int i = 0;i < c.length;i++)if(c[i] < 0)c[i] += mod;
		return c;
	}
	
	// F_{t+1}(x) = -F_t(x)^2*P(x) + 2F_t(x)
	// if want p-destructive, comment out flipping p just before returning.
	public static long[] inv(long[] p)
	{
		int n = p.length;
		long[] f = {invl(p[0], mod)};
		for(int i = 0;i < p.length;i++){
			if(p[i] == 0)continue;
			p[i] = mod-p[i];
		}
		for(int i = 1;i < 2*n;i*=2){
			long[] f2 = mul(f, f, Math.min(n, 2*i));
			long[] f2p = mul(f2, Arrays.copyOf(p, i), Math.min(n, 2*i));
			for(int j = 0;j < f.length;j++){
				f2p[j] += 2L*f[j];
				if(f2p[j] >= mod)f2p[j] -= mod;
				if(f2p[j] >= mod)f2p[j] -= mod;
			}
			f = f2p;
		}
		for(int i = 0;i < p.length;i++){
			if(p[i] == 0)continue;
			p[i] = mod-p[i];
		}
		return f;
	}
	
	// differentiate	
	public static long[] d(long[] p)
	{
		long[] q = new long[p.length];
		for(int i = 0;i < p.length-1;i++){
			q[i] = p[i+1] * (i+1) % mod;
		}
		return q;
	}
	
	// integrate
	public static long[] i(long[] p)
	{
		long[] q = new long[p.length];
		for(int i = 0;i < p.length-1;i++){
			q[i+1] = p[i] * invl(i+1, mod) % mod;
		}
		return q;
	}
	
	// F_{t+1}(x) = F_t(x)-(ln F_t(x) - P(x)) * F_t(x)
	public static long[] exp(long[] p)
	{
		int n = p.length;
		long[] f = {p[0]};
		for(int i = 1;i < 2*n;i*=2){
			long[] ii = ln(f);
			long[] sub = sub(ii, p, Math.min(n, 2*i));
			if(--sub[0] < 0)sub[0] += mod;
			for(int j = 0;j < 2*i && j < n;j++){
				sub[j] = mod-sub[j];
				if(sub[j] == mod)sub[j] = 0;
			}
			f = mul(sub, f, Math.min(n, 2*i));
//			f = sub(f, mul(sub(ii, p, 2*i), f, 2*i));
		}
		return f;
	}
	
	// \int f'(x)/f(x) dx
	public static long[] ln(long[] f)
	{
		long[] ret = i(mul(d(f), inv(f)));
		ret[0] = f[0];
		return ret;
	}
	
	// destructive
	public static long[] divf(long[] a, int[][] fif)
	{
		for(int i = 0;i < a.length;i++)a[i] = a[i] * fif[1][i] % mod;
		return a;
	}
	
	// destructive
	public static long[] mulf(long[] a, int[][] fif)
	{
		for(int i = 0;i < a.length;i++)a[i] = a[i] * fif[0][i] % mod;
		return a;
	}
	
	public static long[] transformExponentially(long[] a, int[][] fif)
	{
		return mulf(exp(divf(Arrays.copyOf(a, a.length), fif)), fif);
	}
	
	public static long[] transformLogarithmically(long[] a, int[][] fif)
	{
		return mulf(Arrays.copyOf(ln(divf(Arrays.copyOf(a, a.length), fif)), a.length), fif);
	}
	
	public static long pow(long a, long n, long mod) {
		//		a %= mod;
		long ret = 1;
		int x = 63 - Long.numberOfLeadingZeros(n);
		for (; x >= 0; x--) {
			ret = ret * ret % mod;
			if (n << 63 - x < 0)
				ret = ret * a % mod;
		}
		return ret;
	}
	
	public static long invl(long a, long mod) {
		long b = mod;
		long p = 1, q = 0;
		while (b > 0) {
			long c = a / b;
			long d;
			d = a;
			a = b;
			b = d % b;
			d = p;
			p = q;
			q = d - c * q;
		}
		return p < 0 ? p + mod : p;
	}
	
	public static long[] reverse(long[] p)
	{
		long[] ret = new long[p.length];
		for(int i = 0;i < p.length;i++){
			ret[i] = p[p.length-1-i];
		}
		return ret;
	}
	
	public static long[] reverse(long[] p, int lim)
	{
		long[] ret = new long[lim];
		for(int i = 0;i < lim && i < p.length;i++){
			ret[i] = p[p.length-1-i];
		}
		return ret;
	}
	
	// [quotient, remainder]
	// remainder can be empty.
	// @see http://www.dis.uniroma1.it/~sankowski/lecture4.pdf
	public static long[][] div(long[] p, long[] q)
	{
		if(p.length < q.length)return new long[][]{new long[0], Arrays.copyOf(p, p.length)};
		long[] rp = reverse(p, p.length-q.length+1);
		long[] rq = reverse(q, p.length-q.length+1);
		long[] rd = mul(rp, inv(rq), p.length-q.length+1);
		long[] d = reverse(rd, p.length-q.length+1);
		long[] r = sub(p, mul(d, q, q.length-1), q.length-1);
		return new long[][]{d, r};
	}

	public static long[] substitute(long[] p, long[] xs)
	{
		return descendProductTree(p, buildProductTree(xs));
	}
	
	public static long[][] buildProductTree(long[] xs)
	{
		int m = Integer.highestOneBit(xs.length)*4;
		long[][] ms = new long[m][];
		for(int i = 0;i < xs.length;i++){
			ms[m/2+i] = new long[]{mod-xs[i], 1};
		}
		for(int i = m/2-1;i >= 1;i--){
			if(ms[2*i] == null){
				ms[i] = null;
			}else if(ms[2*i+1] == null){
				ms[i] = ms[2*i];
			}else{
				ms[i] = mul(ms[2*i], ms[2*i+1]);
			}
		}
		return ms;
	}
	
	public static long[] descendProductTree(long[] p, long[][] pt)
	{
		long[] rets = new long[pt[1].length-1];
		dfs(p, pt, 1, rets);
		return rets;
	}
	
	private static void dfs(long[] p, long[][] pt, int cur, long[] rets)
	{
		if(pt[cur] == null)return;
		if(cur >= pt.length/2){
			rets[cur-pt.length/2] = p[0];
		}else{
			// F = q1X+r1
			// F = q2Y+r2
			
			if(p.length >= 200){
				if(pt[2*cur+1] != null){
					long[][] qr0 = div(p, pt[2*cur]);
					dfs(qr0[1], pt, cur*2, rets);
					long[][] qr1 = div(p, pt[2*cur+1]);
					dfs(qr1[1], pt, cur*2+1, rets);
				}else if(pt[2*cur] != null){
					long[] nex = cur == 1 ? div(p, pt[2*cur])[1] : p;
					dfs(nex, pt, cur*2, rets);
				}
			}else{
				if(pt[2*cur+1] != null){
					dfs(modnaive(p, pt[2*cur]), pt, cur*2, rets);
					dfs(modnaive(p, pt[2*cur+1]), pt, cur*2+1, rets);
				}else if(pt[2*cur] != null){
					long[] nex = cur == 1 ? modnaive(p, pt[2*cur]) : p;
					dfs(nex, pt, cur*2, rets);
				}
			}
		}
	}
	
	public static long[][] divnaive(long[] a, long[] b)
	{
		int n = a.length, m = b.length;
		if(n-m+1 <= 0)return new long[][]{new long[0], Arrays.copyOf(a, n)};
		long[] r = Arrays.copyOf(a, n);
		long[] q = new long[n-m+1];
		long ib = invl(b[m-1], mod);
		for(int i = n-1;i >= m-1;i--){
			long x = ib * r[i] % mod;
			q[i-(m-1)] = x;
			for(int j = m-1;j >= 0;j--){
				r[i+j-(m-1)] -= b[j]*x;
				r[i+j-(m-1)] %= mod;
				if(r[i+j-(m-1)] < 0)r[i+j-(m-1)] += mod;
//				r[i+j-(m-1)] = modh(r[i+j-(m-1)]+(long)mod*mod - b[j]*x, MM, HH, mod);
			}
		}
		return new long[][]{q, Arrays.copyOf(r, m-1)};
	}
	
	public static final int[] NTTPrimes = {1053818881, 1051721729, 1045430273, 1012924417, 1007681537, 1004535809, 998244353, 985661441, 976224257, 975175681};
	public static final int[] NTTPrimitiveRoots = {7, 6, 3, 5, 3, 3, 3, 3, 3, 17};
	
//	public static long[] convolute(long[] a, long[] b)
//	{
//		int USE = 2;
//		int m = Math.max(2, Integer.highestOneBit(a.length-1)<<2);
//		long[][] fs = new long[USE][];
//		for(int k = 0;k < USE;k++){
//			int P = NTTPrimes[k], g = NTTPrimitiveRoots[k];
//			long[] fa = nttmb(a, m, false, P, g);
//			long[] fb = a == b ? fa : nttmb(b, m, false, P, g);
//			for(int i = 0;i < m;i++){
//				fa[i] = fa[i]*fb[i]%P;
//			}
//			fs[k] = nttmb(fa, m, true, P, g);
//		}
//		
//		int[] mods = Arrays.copyOf(NTTPrimes, USE);
//		long[] gammas = garnerPrepare(mods);
//		int[] buf = new int[USE];
//		for(int i = 0;i < fs[0].length;i++){
//			for(int j = 0;j < USE;j++)buf[j] = (int)fs[j][i];
//			long[] res = garnerBatch(buf, mods, gammas);
//			long ret = 0;
//			for(int j = res.length-1;j >= 0;j--)ret = ret * mods[j] + res[j];
//			fs[0][i] = ret;
//		}
//		return fs[0];
//	}
	
	static int L = 16; // max m = 2^L
	static long[][] cache = new long[6][1<<L];
	
	// using cache, optimize memory.
	public static long[] convolute3(long[] a, long[] b, int mod, long[][] ffb)
	{
		int m = ffb != null ? ffb[0].length : Math.max(2, Integer.highestOneBit(a.length+b.length-1)<<1);
		long[][] fs = new long[3][];
		for(int k = 0;k < 3;k++){
			int P = NTTPrimes[k];
			long[] fa = nttmb(a, m, false, cache[k*2], k);
			long[] fb = ffb != null ? ffb[k] : a == b ? fa : nttmb(b, m, false, cache[k*2+1], k);
			for(int i = 0;i < m;i++){
				fa[i] = fa[i]*fb[i]%P;
			}
			fs[k] = nttmb(fa, m, true, k);
		}
		
		// mods={1053818881, 1051721729, 1045430273
		// gamma=[[0, 525860363, 152479290]]
		long F = 1051721729L * 1053818881L % mod;
		long[] res = new long[a.length+b.length-1];
		for(int i = 0;i < res.length;i++){
			long v0 = fs[0][i];
			long v1 = (fs[1][i] - v0 + 1051721729L + 1051721729L) * 525860363L % 1051721729L;
			long v2 = ((fs[2][i] - v0 + 1045430273L) * 152479290L + v1 * (1045430273L-871191728L)) % 1045430273L;
			long ret = (v2 * F + v1 * 1053818881L + v0) % mod;
			res[i] = ret;
		}
		return res;
	}
	
	// if m is not max size, not using cache will be faster.
	public static long[] convolute3NoCache(long[] a, long[] b, int mod, long[][] ffb)
	{
		int m = ffb != null ? ffb[0].length : Math.max(2, Integer.highestOneBit(a.length+b.length-1)<<1);
		long[][] fs = new long[3][];
		for(int k = 0;k < 3;k++){
			int P = NTTPrimes[k];
			long[] fa = nttmb(a, m, false, null, k);
			long[] fb = ffb != null ? ffb[k] : a == b ? fa : nttmb(b, m, false, null, k);
			for(int i = 0;i < m;i++){
				fa[i] = fa[i]*fb[i]%P;
			}
			fs[k] = nttmb(fa, m, true, k);
		}
		
		// mods={1053818881, 1051721729, 1045430273
		// gamma=[[0, 525860363, 152479290]]
		long F = 1051721729L * 1053818881L % mod;
		long[] res = new long[a.length+b.length-1];
		for(int i = 0;i < res.length;i++){
			long v0 = fs[0][i];
			long v1 = (fs[1][i] - v0 + 1051721729L + 1051721729L) * 525860363L % 1051721729L;
			long v2 = ((fs[2][i] - v0 + 1045430273L) * 152479290L + v1 * (1045430273L-871191728L)) % 1045430273L;
			long ret = (v2 * F + v1 * 1053818881L + v0) % mod;
			res[i] = ret;
		}
		return res;
	}
	
	static int[][][] wws;
	static int[][][] iwws;
	
	static void superPrepare()
	{
		wws = new int[3][][];
		iwws = new int[3][][];
		for(int t = 0;t < 3;t++){
			int P = NTTPrimes[t], g = NTTPrimitiveRoots[t];
			long K = Integer.highestOneBit(P)<<1;
			int H = Long.numberOfTrailingZeros(K)*2;
			long M = K*K/P;
			{
				wws[t] = new int[L+1][];
				long w = (1L<<32)%P;
				long dw = pow(g, P-1>>>L, P);
				wws[t][L] = new int[1<<L-1];
				for(int k = 0;k < 1<<L-1;k++){
					wws[t][L][k] = (int)w;
					w = modh(w*dw, M, H, P);
				}
				for(int i = L-1;i >= 1;i--){
					wws[t][i] = new int[1<<i-1];
					for(int k = 0;k < 1<<i-1;k++)wws[t][i][k] = wws[t][i+1][k*2];
				}
			}
			{
				iwws[t] = new int[L+1][];
				long w = (1L<<32)%P;
				long dw = pow(g, P-1-(P-1>>>L), P);
				iwws[t][L] = new int[1<<L-1];
				for(int k = 0;k < 1<<L-1;k++){
					iwws[t][L][k] = (int)w;
					w = modh(w*dw, M, H, P);
				}
				for(int i = L-1;i >= 1;i--){
					iwws[t][i] = new int[1<<i-1];
					for(int k = 0;k < 1<<i-1;k++)iwws[t][i][k] = iwws[t][i+1][k*2];
				}
			}
		}
	}
	
	private static long[] nttmb(long[] src, int n, boolean inverse, int ind){
		return nttmb(src, n, inverse, new long[n], ind);
	}
	
	// Modifed Montgomery + Barrett
	private static long[] nttmb(long[] src, int n, boolean inverse, long[] dst, int ind)
	{
		int P = NTTPrimes[ind];
		
		System.arraycopy(src, 0, dst, 0, Math.min(n, src.length));
		Arrays.fill(dst, Math.min(n, src.length), n, 0);
		
		int h = Integer.numberOfTrailingZeros(n);
		long J = invl(P, 1L<<32);
		int[][] mul = inverse ? iwws[ind] : wws[ind];
		for(int i = 0;i < h;i++){
			for(int j = 0;j < 1<<i;j++){
				for(int k = 0, s = j<<h-i, t = s|1<<h-i-1;k < 1<<h-i-1;k++,s++,t++){
					long u = (dst[s] - dst[t] + 2*P)*mul[h-i][k];
					dst[s] += dst[t];
					if(dst[s] >= 2*P)dst[s] -= 2*P;
//					long Q = (u&(1L<<32)-1)*J&(1L<<32)-1;
					long Q = (u<<32)*J>>>32;
					dst[t] = (u>>>32)-(Q*P>>>32)+P;
				}
			}
		}
		for(int i = 0;i < n;i++){
			if(dst[i] >= P)dst[i] -= P;
		}
		for(int i = 0;i < n;i++){
			int rev = Integer.reverse(i)>>>-h;
			if(i < rev){
				long d = dst[i]; dst[i] = dst[rev]; dst[rev] = d;
			}
		}
		
		if(inverse){
			long K = Integer.highestOneBit(P)<<1;
			int H = Long.numberOfTrailingZeros(K)*2;
			long M = K*K/P;
			
			long in = invl(n, P);
			for(int i = 0;i < n;i++)dst[i] = modh(dst[i]*in, M, H, P);
		}
		
		return dst;
	}
	
	static final long mask = (1L<<31)-1;
	
	public static long modh(long a, long M, int h, int mod)
	{
		long r = a-((M*(a&mask)>>>31)+M*(a>>>31)>>>h-31)*mod;
		return r < mod ? r : r-mod;
	}
	
	public static long[] modnaive(long[] a, long[] b)
	{
		int n = a.length, m = b.length;
		if(n-m+1 <= 0)return a;
		long[] r = Arrays.copyOf(a, n);
		long ib = invl(b[m-1], mod);
		for(int i = n-1;i >= m-1;i--){
			long x = ib * r[i] % mod;
			for(int j = m-1;j >= 0;j--){
				r[i+j-(m-1)] -= b[j]*x;
				r[i+j-(m-1)] %= mod;
				if(r[i+j-(m-1)] < 0)r[i+j-(m-1)] += mod;
//				r[i+j-(m-1)] = modh(r[i+j-(m-1)]+(long)mod*mod - b[j]*x, MM, HH, mod);
			}
		}
		return Arrays.copyOf(r, m-1);
	}

	
	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 Factorial().run(); }
	
	private byte[] inbuf = new byte[1024];
	private 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