为什么高精度加上会超时(仅65pts)
查看原帖
为什么高精度加上会超时(仅65pts)
365777
halehu楼主2024/11/21 23:00

rt,还是说dp不够优秀(或是高精写太烂了)

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 705;
struct edge{
	int nxt,to;
}e[N * 2];
int n,tot,head[N],siz[N],tmp[N],num1[N],num2[N];
string dp[N][N],ans;
void add(int u,int v){
	e[tot].to = v,e[tot].nxt = head[u],head[u] = tot ++;
}
string operator *(const string a,const string b){
	string res = "";
	int len1 = a.size(),len2 = b.size();
	for(int i=1;i<=len1;i++) num1[i] = a[len1 - i] - '0';
	for(int i=1;i<=len2;i++) num2[i] = b[len2 - i] - '0';
	for(int i=1;i<=len1+len2;i++) tmp[i] = 0;
	for(int i=1;i<=len1;i++)
	    for(int j=1;j<=len2;j++)
	        tmp[i + j - 1] += num1[i] * num2[j];
	for(int i=1;i<=len1+len2;i++) tmp[i+1] += tmp[i] / 10,tmp[i] %= 10;
	int pos = 0;
	for(int i=len1+len2;i>=1;i--) if(tmp[i]){
		pos = i; break;
	}
	for(int i=pos;i>=1;i--) res = res + char(tmp[i] + '0');
	return res;
}
string getmax(string a,string b){
	if(a.size() > b.size()) return a;
	else if(a.size() < b.size()) return b;
	else{
		if(a > b) return a;
		else return b;
	}
}
string getstring(int x){
	string res = "";
	while(x){
		res = char(x % 10 + '0') + res;
		x /= 10;
	}
	return res;
}
void dfs(int u,int fa){
	siz[u] = 1;
	for(int i=head[u];i!=-1;i=e[i].nxt){
		int v = e[i].to; if(v == fa) continue;
		dfs(v,u); siz[u] += siz[v];
	}
	dp[u][1] = "1";
	for(int k=head[u];k!=-1;k=e[k].nxt){
		int v = e[k].to; if(v == fa) continue;
		for(int i=siz[u];i>=1;i--){
			string tmp = dp[u][i];
			for(int j=1;j<=siz[v];j++){
				string s = getstring(j);
				dp[u][i] = getmax(dp[u][i],tmp * dp[v][j] * s);
			}
			for(int j=1;j<=min(siz[v],i);j++)
				dp[u][i] = getmax(dp[u][i],dp[u][i-j] * dp[v][j]);
		}
	}
}
void print(int x){
	if(!x) return;
	print(x / 10); putchar(x % 10 + '0');
}
int read(){
	char c; c = getchar();
	int res = 0;
	while(c < '0' || c > '9') c = getchar();
	while(c >= '0' && c <= '9'){
		res = res * 10 + c - '0';
		c = getchar();
	}
	return res;
}
signed main(){
	memset(head,-1,sizeof head);
	n = read();
	for(int i=1;i<n;i++){
		int a,b; a = read(),b = read();
		add(a,b),add(b,a);
	}
	dfs(1,0); ans = "";
	for(int i=1;i<=n;i++){
		string s = getstring(i);
		ans = getmax(ans,dp[1][i] * s);
	}
	cout<<ans<<endl;
	return 0;
}
2024/11/21 23:00
加载中...