UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#213535#2715. 8.4t4cheatme100229ms21720kbC++115.2kb2024-11-12 20:23:312024-11-12 23:29:00

answer

#include <bits/stdc++.h>
#define file(x) {freopen(x".in", "r", stdin); freopen(x".out", "w", stdout);}
namespace Fastio{struct Reader{template<typename T>Reader&operator>>(T&x){x=0;short f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar();x*=f;return*this;}Reader&operator>>(double&x){x=0;double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}Reader&operator>>(long double&x){x=0;long double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}Reader&operator>>(__float128&x){x=0;__float128 t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}Reader&operator>>(char&c){c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();return*this;}Reader&operator>>(char*str){int len=0;char c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();while(c!=' '&&c!='\n'&&c!='\r')str[len++]=c,c=getchar();str[len]='\0';return*this;}Reader&operator>>(std::string&str){str.clear();char c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();while(c!=' '&&c!='\n'&&c!='\r')str.push_back(c),c=getchar();return*this;}Reader(){}}cin;const char endl='\n';struct Writer{const int Setprecision=6;typedef int mxdouble;template<typename T>Writer&operator<<(T x){if(x==0){putchar('0');return*this;}if(x<0)putchar('-'),x=-x;static short sta[40];short top=0;while(x>0)sta[++top]=x%10,x/=10;while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(double x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(long double x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(long double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(__float128 x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(__float128)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(char c){putchar(c);return*this;}Writer&operator<<(char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}Writer&operator<<(const char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}Writer&operator<<(std::string str){int st=0,ed=str.size();while(st<ed)putchar(str[st++]);return*this;}Writer(){}}cout;}using namespace Fastio;
#define io Fastio::cin
#define IO Fastio::cout
int T = 1;
typedef uint64_t u64;
const int N = 5010, mod = 1e9 + 7;
int n, u, v, h[N], tot, fa[N], f[N][N], siz[N], fac[N], ifac[N], ans;
std::vector<int>G[N];
int qpow(int a, int b = mod - 2) {
    int ans = 1;
    while(b) {
        if(b & 1) ans = (u64)ans * a % mod;
        a = (u64)a * a % mod; b >>= 1;
    }
    return ans;
}
void init(int n) {
    fac[0] = 1;
    for(int i = 1; i <= n; i++) fac[i] = (u64)fac[i - 1] * i % mod;
    ifac[n] = qpow(fac[n]);
    for(int i = n; i >= 1; i--) ifac[i - 1] = (u64)ifac[i] * i % mod;
}
inline int C(int n, int m) {return (u64)fac[n] * ifac[n - m] % mod * ifac[m] % mod;}
inline void inc(int &a, int b) {if((a += b) >= mod) a -= mod;}
void dfs(int u) {
    siz[u] = 1; f[u][1] = 1;
    for(int v : G[u]) {
        if(v == fa[u]) continue;
        fa[v] = u; dfs(v); 
        for(int j = siz[v]; j >= 0; j--) inc(f[v][j], f[v][j + 1]);
        for(int j = siz[u]; j >= 1; j--) {
            if(f[u][j]) {
                int z = f[u][j]; f[u][j] = 0;
                for(int k = 0; k <= siz[v]; k++) 
                    f[u][j + k] = (f[u][j + k] + (u64)z * f[v][k] % mod * C(j + k - 1, k)) % mod;
            }
        }
        siz[u] += siz[v];
    }
}
void solve(){
	Fastio::cin >> n;
	init(n);
    for(int i = 1; i < n; i++) {
        Fastio::cin >> u >> v;
		G[u].push_back(v);
		G[v].push_back(u);
    }
    dfs(1);
    for(int i = 1; i <= n; i++) ans = (ans + f[1][i]) % mod;
    Fastio::cout << ans << '\n';
}
int32_t main(){
//	file("A");
//	Fastio::cin >> T;
	while(T--){
		solve();
	}
	return 0;
}

详细

小提示:点击横条可展开更详细的信息

Test #1:

score: 10
Accepted
time: 0ms
memory: 1348kb

input:

10
1 2
1 3
3 4
3 5
3 6
3 7
1 8
5 9
5 10

output:

227506

result:

ok found '227506.00000', expected '227506.00000', error '0.00000'

Test #2:

score: 10
Accepted
time: 0ms
memory: 1344kb

input:

10
1 2
1 3
3 4
4 5
5 6
4 7
5 8
8 9
8 10

output:

70140

result:

ok found '70140.00000', expected '70140.00000', error '0.00000'

Test #3:

score: 10
Accepted
time: 0ms
memory: 1712kb

input:

100
1 2
1 3
1 4
3 5
5 6
4 7
7 8
8 9
8 10
3 11
7 12
1 13
5 14
8 15
10 16
7 17
1 18
6 19
10 20
15 21
7...

output:

875072841

result:

ok found '875072841.00000', expected '875072841.00000', error '0.00000'

Test #4:

score: 10
Accepted
time: 1ms
memory: 1716kb

input:

100
1 2
2 3
3 4
4 5
4 6
6 7
5 8
8 9
8 10
1 11
6 12
4 13
9 14
13 15
7 16
3 17
15 18
17 19
9 20
8 21
4...

output:

706867541

result:

ok found '706867541.00000', expected '706867541.00000', error '0.00000'

Test #5:

score: 10
Accepted
time: 0ms
memory: 1716kb

input:

100
1 2
2 3
3 4
2 5
2 6
5 7
2 8
4 9
7 10
5 11
11 12
6 13
8 14
13 15
5 16
11 17
16 18
11 19
7 20
19 2...

output:

515055734

result:

ok found '515055734.00000', expected '515055734.00000', error '0.00000'

Test #6:

score: 10
Accepted
time: 3ms
memory: 3344kb

input:

500
1 2
1 3
3 4
3 5
5 6
1 7
3 8
8 9
4 10
6 11
7 12
2 13
11 14
8 15
7 16
9 17
2 18
3 19
15 20
2 21
20...

output:

28964814

result:

ok found '28964814.00000', expected '28964814.00000', error '0.00000'

Test #7:

score: 10
Accepted
time: 0ms
memory: 3340kb

input:

500
1 2
1 3
1 4
2 5
5 6
5 7
4 8
8 9
1 10
4 11
8 12
6 13
13 14
13 15
12 16
1 17
10 18
13 19
1 20
1 21...

output:

900482908

result:

ok found '900482908.00000', expected '900482908.00000', error '0.00000'

Test #8:

score: 10
Accepted
time: 75ms
memory: 21704kb

input:

5000
1 2
1 3
1 4
3 5
3 6
5 7
4 8
4 9
1 10
5 11
6 12
10 13
12 14
14 15
12 16
8 17
14 18
9 19
8 20
20 ...

output:

255910011

result:

ok found '255910011.00000', expected '255910011.00000', error '0.00000'

Test #9:

score: 10
Accepted
time: 78ms
memory: 21720kb

input:

5000
1 2
1 3
1 4
2 5
1 6
5 7
2 8
5 9
4 10
9 11
10 12
8 13
7 14
13 15
12 16
5 17
9 18
8 19
3 20
4 21
...

output:

506490313

result:

ok found '506490313.00000', expected '506490313.00000', error '0.00000'

Test #10:

score: 10
Accepted
time: 72ms
memory: 21688kb

input:

5000
1 2
1 3
1 4
4 5
4 6
3 7
2 8
8 9
6 10
9 11
3 12
3 13
7 14
13 15
4 16
10 17
9 18
10 19
5 20
17 21...

output:

738067707

result:

ok found '738067707.00000', expected '738067707.00000', error '0.00000'