【codeforces 515C】Drazil and Factorial
【题目链接】:http://codeforces.com/contest/515/problem/C
【题意】
定义f(n)=n这个数各个位置上的数的阶乘的乘积;
给你a;
让你另外求一个不含0和1的最大的数字b;
使得f(a)==f(b)
【题解】
对a的每一个大于1的数字进行分解;
看看它能够组合成的最多的比它小的数字的阶乘的乘积是哪些;
比如
每个都找最多数目的;数目相同找大的数的组合;
求出2..9!的组合就好;
最后根据每个数字的分解方案;
求出所有的数字;
然后降序排;
顺序输出就好;
【Number Of WA】
0
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 12;
LL fac[N];
vector<int> fenjie[N];
int a[N],ans[N];
int n,temp[20],final_ans[200],num=0;
char s[20];
void dfs(int x, int pos,int num,LL now)
{
if (x > 9)
{
if (now == fac[pos])
{
if (num > ans[pos])
{
ans[pos] = num;
fenjie[pos].clear();
rep1(i, 2, 9)
{
int len = a[i];
rep1(j, 1, len)
fenjie[pos].ps(i);
}
}
}
return;
}
LL temp = 1;
int tot = 0;
while (1LL * temp*fac[x] <= fac[pos])
{
tot++;
temp = 1LL * temp*fac[x];
}
rep2(i, tot, 0)
{
if (now*temp <= fac[pos])
{
a[x] = i;
dfs(x + 1, pos, num + i, now*temp);
a[x] = 0;
}
temp /= fac[x];
}
}
bool cmp(int a, int b)
{
return a > b;
}
int main()
{
//freopen("F:\\rush.txt", "r", stdin);
fac[0] = fac[1] = 1;
rep1(i, 2, 9)
fac[i] = fac[i - 1] * i;
fenjie[2].ps(2);fenjie[3].ps(3);
rep1(i, 4, 9)
{
dfs(2, i,0,1);
}
rei(n);
scanf("%s", s + 1);
rep1(i, 1, n)
temp[i] = s[i] - '0';
rep1(i,1,n)
if (temp[i] > 1)
{
int len = fenjie[temp[i]].size();
rep1(j, 0, len-1)
{
final_ans[++num] = fenjie[temp[i]][j];
}
}
sort(final_ans + 1, final_ans + 1 + num, cmp);
rep1(i, 1, num)
printf("%d", final_ans[i]);
puts("");
//printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
return 0;
}