結果

問題 No.444 旨味の相乗効果
ユーザー uwiuwi
提出日時 2016-11-11 23:02:38
言語 Java21
(openjdk 21)
結果
AC  
実行時間 179 ms / 2,500 ms
コード長 7,432 bytes
コンパイル時間 6,376 ms
コンパイル使用メモリ 83,132 KB
実行使用メモリ 55,876 KB
最終ジャッジ日時 2023-09-07 02:12:26
合計ジャッジ時間 9,991 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 57 ms
48,860 KB
testcase_01 AC 57 ms
50,680 KB
testcase_02 AC 58 ms
50,780 KB
testcase_03 AC 58 ms
50,704 KB
testcase_04 AC 176 ms
55,808 KB
testcase_05 AC 58 ms
50,672 KB
testcase_06 AC 59 ms
50,680 KB
testcase_07 AC 127 ms
55,000 KB
testcase_08 AC 98 ms
54,152 KB
testcase_09 AC 62 ms
50,756 KB
testcase_10 AC 59 ms
50,748 KB
testcase_11 AC 61 ms
50,760 KB
testcase_12 AC 176 ms
55,828 KB
testcase_13 AC 146 ms
53,264 KB
testcase_14 AC 60 ms
51,112 KB
testcase_15 AC 59 ms
50,720 KB
testcase_16 AC 176 ms
55,756 KB
testcase_17 AC 56 ms
50,504 KB
testcase_18 AC 59 ms
50,656 KB
testcase_19 AC 106 ms
53,776 KB
testcase_20 AC 179 ms
55,832 KB
testcase_21 AC 57 ms
51,044 KB
testcase_22 AC 56 ms
50,660 KB
testcase_23 AC 57 ms
50,568 KB
testcase_24 AC 176 ms
55,876 KB
testcase_25 AC 56 ms
50,500 KB
testcase_26 AC 56 ms
50,704 KB
testcase_27 AC 60 ms
50,764 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 D {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		int n = ni();
		long C = nl();
		int[] a = na(n);
		long[] dp = new long[n];
		dp[0] = 1;
		int mod = 1000000007;
		long[] rets = new long[200];
		for(int i = 0;i < 200;i++){
			for(int j = n-1;j >= 0;j--){
				for(int k = n-1;k > j;k--){
					dp[k] += dp[j] * a[k];
					dp[k] %= mod;
				}
				dp[j] = dp[j] * a[j];
				dp[j] %= mod;
			}
			rets[i] = Arrays.stream(dp).sum() % mod;
		}
		long[] lx = modifiedBerlekampMassey(rets, mod);
		for(int i = 0;i < lx.length;i++){
			lx[i] = (mod-lx[i])%mod;
		}
		lx = Arrays.copyOf(lx, lx.length-1);
		long ret = 0;
		if(lx.length > 0){
			long[] u = lrx(lx, C-1, mod);
			for(int i = u.length-1;i >= 0;i--){
				ret = ret + rets[i] * u[i];
				ret %= mod;
			}
		}
		for(int i = 0;i < n;i++){
			ret -= pow(a[i], C, mod);
		}
		ret %= mod;
		if(ret < 0)ret += mod;
		out.println(ret);
	}
	
	public static long[] lrx(long[] co, long n, long mod)
	{
		long BIG = (Long.MAX_VALUE - 2L*mod*mod)/mod*mod;
		int m = co.length;
		if(m == 1){
			long ret = 1;
			long mul = co[0];
			for(;n > 0;n >>>= 1){
				if((n&1)==1){
					ret = ret * mul % mod;
				}
				mul = mul * mul % mod;
			}
			return new long[]{ret};
		}
		
		long[][] m2 = new long[m-1][m];
		for(int j = 0;j < m;j++){
			m2[0][j] = co[j];
		}
		for(int i = 1;i < m-1;i++){
			for(int j = 0;j < m;j++){
				long x = m2[i-1][m-1] * co[j];
				if(j-1 >= 0)x += m2[i-1][j-1];
				m2[i][j] = x % mod;
			}
		}
		
		long[] ret = new long[m];
		ret[0] = 1;
		long[] temp = new long[2*m];
		for(int l = 62;l >= 0;l--){
			if(n<<~l<0){
				for(int i = 0;i < m;i++)temp[i] = ret[m-1] * co[i];
				for(int i = 1;i < m;i++)temp[i] += ret[i-1];
				for(int i = 0;i < m;i++)ret[i] = temp[i] % mod;
			}
			if(l > 0){
				Arrays.fill(temp, 0L);
				for(int i = 0;i < m;i++){
					for(int j = 0;j < m;j++){
						temp[i+j] += ret[i] * ret[j];
						if(temp[i+j] >= BIG)temp[i+j] -= BIG;
					}
				}
				for(int i = 0;i < 2*m-1;i++)temp[i] %= mod;
				for(int i = 0;i < m;i++){
					long s = temp[i];
					for(int j = m;j < 2*m-1;j++){
						s += temp[j] * m2[j-m][i];
						if(s >= BIG)s -= BIG;
					}
					ret[i] = s % 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[] modifiedBerlekampMassey(long[] a, int mod)
	{
		assert a.length % 2 == 0;
		int n = a.length/2;
		int m = 2*n-1;
		long[] R0 = new long[2*n+1]; R0[2*n] = 1;
		long[] R1 = new long[2*n];
		for(int i = 0;i < 2*n;i++)R1[i] = a[m-i];
		R1 = strip(R1);
		long[] v0 = {0L};
		long[] v1 = {1L};
		while(n <= R1.length-1){
			long[][] QR = divmod(R0, R1, mod);
			long[] v = sub(v0, mul(QR[0], v1, mod), mod);
			v0 = v1; v1 = v; R0 = R1; R1 = QR[1];
		}
		// normalize
		long z = invl(v1[v1.length-1], mod);
		for(int i = 0;i < v1.length;i++){
			v1[i] = v1[i] * z % mod;
		}
		return strip(v1);
	}
	
	public static long[] mul(long[] a, long[] b, int mod)
	{
		long[] c = new long[a.length+b.length-1];
		for(int i = 0;i < a.length;i++){
			for(int j = 0;j < b.length;j++){
				c[i+j] += a[i] * b[j];
				c[i+j] %= mod;
			}
		}
		return strip(c);
	}
	
	public static long[] sub(long[] a, long[] b, int mod)
	{
		long[] c = Arrays.copyOf(a, Math.max(a.length, b.length));
		for(int i = 0;i < c.length;i++){
			c[i] -= b[i];
			if(c[i] < 0)c[i] += mod;
		}
		return strip(c);
	}
	
	public static long[] strip(long[] a)
	{
		int i;
		for(i = a.length-1;i > 0 && a[i] == 0;i--);
		if(i + 1 == a.length)return a;
		return Arrays.copyOf(a, i+1);
	}

	public static long[][] divmod(long[] A, long[] B, int mod)
	{
		assert B[B.length-1] != 0;
		long[] R = Arrays.copyOf(A, A.length);
		long[] Q = new long[Math.max(1, A.length-B.length+1)];
		long ib = invl(B[B.length-1], mod);
		for(int i = A.length-1, t = Q.length-1;i >= B.length-1;i--,t--){
			long m = R[i] * ib % mod;
			Q[t] = m;
			for(int j = 0, k = i-B.length+1+j;j < B.length;j++,k++){
				R[k] -= B[j] * m;
				R[k] %= mod;
				if(R[k] < 0)R[k] += mod;
			}
			assert R[i] == 0;
		}
		return new long[][]{Q, strip(R)};
	}

	
	
	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;
	}

	
	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 D().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)); }
}
0