Submission #1954419


Source Code Expand

#include <stdio.h>
#include <algorithm>
#include <vector>
using namespace std;

const long long mod = 1000000007;
const long long inv2 = (mod + 1) / 2;
vector<int> G[2020];

vector<long long> mul(vector<long long> &a, vector<long long> &b)
{
	vector<long long> r(a.size()+b.size()-1);
	for (int i=0;i<a.size();i++) for (int j=0;j<b.size();j++){
		r[i+j] = (r[i+j] + a[i] * b[j]) % mod;
	}
	return move(r);
}

vector<long long> add(vector<long long> &a, vector<long long> &b, int sig = 1)
{
	vector<long long> r(max(a.size(),b.size()));
	for (int i=0;i<a.size();i++) r[i] = a[i];
	for (int i=0;i<b.size();i++) r[i] = (r[i] + b[i] * sig % mod + mod) % mod;
	return move(r);
}

vector<vector<long long> > go(int x)
{
	vector<vector<long long> > r(3,vector<long long>(2,0));
	r[0][1] = 1;

	for (auto &y : G[x]){
		auto q = go(y);
		auto s = q[0];
		s = add(s,q[1]);
		s = add(s,q[2]);

		vector<vector<long long> > nr(3);
		for (int k=0;k<3;k++) nr[k] = mul(r[k],s);
		long long coeff[2][2] = {2,1,1,inv2};
		for (int i=0;i<2;i++) for (int j=0;j<2;j++){
			auto t = mul(r[i],q[j]);
			t.erase(t.begin());
			nr[i+1] = add(nr[i+1],t,-coeff[i][j]);
		}
		r = move(nr);
	}

	return move(r);
}

int main()
{
	int N; scanf ("%d",&N);
	for (int i=2;i<=N;i++){
		int p; scanf ("%d",&p);
		G[p].push_back(i);
	}

	auto r = go(1);

	long long ans = 0, f = 1;
	for (int i=1;i<=N;i++){
		f = f * i % mod;
		ans = (ans + (r[0][i] + r[1][i] + r[2][i]) * f) % mod;
	}
	printf ("%lld\n",ans);

	return 0;
}

Submission Info

Submission Time
Task A - Awkward
User august14
Language C++14 (GCC 5.4.1)
Score 1000
Code Size 1557 Byte
Status AC
Exec Time 125 ms
Memory 1408 KB

Compile Error

./Main.cpp: In function ‘int main()’:
./Main.cpp:54:24: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
  int N; scanf ("%d",&N);
                        ^
./Main.cpp:56:25: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   int p; scanf ("%d",&p);
                         ^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1000 / 1000
Status
AC × 4
AC × 55
Set Name Test Cases
Sample a01, a02, a03, a04
All a01, a02, a03, a04, b05, b06, b07, b08, b09, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, b36, b37, b38, b39, b40, b41, b42, b43, b44, b45, b46, b47, b48, b49, b50, b51, b52, b53, b54, b55
Case Name Status Exec Time Memory
a01 AC 1 ms 256 KB
a02 AC 1 ms 256 KB
a03 AC 1 ms 256 KB
a04 AC 1 ms 256 KB
b05 AC 1 ms 256 KB
b06 AC 113 ms 512 KB
b07 AC 125 ms 1408 KB
b08 AC 34 ms 512 KB
b09 AC 34 ms 512 KB
b10 AC 34 ms 484 KB
b11 AC 34 ms 512 KB
b12 AC 38 ms 484 KB
b13 AC 56 ms 512 KB
b14 AC 115 ms 512 KB
b15 AC 34 ms 512 KB
b16 AC 10 ms 384 KB
b17 AC 2 ms 256 KB
b18 AC 1 ms 256 KB
b19 AC 34 ms 512 KB
b20 AC 34 ms 512 KB
b21 AC 34 ms 500 KB
b22 AC 35 ms 512 KB
b23 AC 39 ms 512 KB
b24 AC 36 ms 512 KB
b25 AC 42 ms 512 KB
b26 AC 113 ms 512 KB
b27 AC 64 ms 640 KB
b28 AC 112 ms 512 KB
b29 AC 102 ms 896 KB
b30 AC 102 ms 896 KB
b31 AC 82 ms 768 KB
b32 AC 122 ms 1280 KB
b33 AC 35 ms 512 KB
b34 AC 39 ms 512 KB
b35 AC 38 ms 512 KB
b36 AC 36 ms 512 KB
b37 AC 36 ms 512 KB
b38 AC 35 ms 512 KB
b39 AC 35 ms 512 KB
b40 AC 35 ms 512 KB
b41 AC 35 ms 512 KB
b42 AC 36 ms 512 KB
b43 AC 36 ms 512 KB
b44 AC 36 ms 512 KB
b45 AC 117 ms 1280 KB
b46 AC 122 ms 1408 KB
b47 AC 124 ms 1408 KB
b48 AC 125 ms 1408 KB
b49 AC 125 ms 1408 KB
b50 AC 109 ms 512 KB
b51 AC 112 ms 512 KB
b52 AC 113 ms 512 KB
b53 AC 113 ms 512 KB
b54 AC 113 ms 512 KB
b55 AC 78 ms 896 KB