[codeforces 516]A. Drazil and Factorial

试题描述

Drazil is playing a math game with Varda.

Let's define  for positive integer x as a product of factorials of its digits. For example, .

First, they choose a decimal number a consisting of n digits that contains at least one digit larger than 1. This number may possibly start with leading zeroes. Then they should find maximum positive number x satisfying following two conditions:

1. x doesn't contain neither digit 0 nor digit 1.

2.  = .

Help friends find such number.

输入

The first line contains an integer n (1 ≤ n ≤ 15) — the number of digits in a.

The second line contains n digits of a. There is at least one digit in a that is larger than 1. Number a may possibly contain leading zeroes.

输出

Output a maximum possible integer satisfying the conditions above. There should be no zeroes and ones in this number decimal representation.

输入示例


输出示例


数据规模及约定

见“输入

题解

这个题相当有意思,我看 n 那么小,那一定是 dp 了,然而写完了才发现 F(a) 会爆 long long,所以只好另辟蹊径。后来发现一个性质:只要把 a 的每一位数都尽量的分出最多数出来,然后再拼到一起就好了,这个不难证明,就是个贪心,若将两个数的阶乘合并成一个数的阶乘,则答案会减少 1,一定不优。

现在的任务是把 2, 3, 4, ... , 9 这些 1 位数尽量多地分解,我发现刚刚的 dp 没有白写:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 6000010
#define maxk 20
int n, sum, fact[maxk], F[maxn], f[maxn][11];
char num[maxk];
bool vis[maxn]; bool Less(int* a, int* b) {
for(int i = 9; i >= 2; i--) if(a[i] != b[i])
return a[i] < b[i];
return 0;
} int main() {
n = read();
scanf("%s", num + 1); fact[0] = 1;
for(int i = 1; i <= 9; i++) fact[i] = fact[i-1] * i;
sum = 1;
for(int i = 1; i <= n; i++) sum *= fact[num[i]-'0'];
vis[1] = 1;
for(int i = 1; i <= sum; i++) if(vis[i]) {
// printf("%d ", i);
for(int k = 2; k <= 9 && fact[k] * i <= sum; k++) {
int t = fact[k] * i;
// printf("[%d]", t);
vis[t] = 1;
if(F[t] < F[i] + 1) {
F[t] = F[i] + 1;
memcpy(f[t], f[i], sizeof(f[i]));
f[t][k]++;
}
if(F[t] > F[i] + 1) continue;
f[i][k]++;
if(Less(f[t], f[i])) memcpy(f[t], f[i], sizeof(f[i]));
f[i][k]--;
}
} for(int i = 9; i >= 2; i--)
for(int j = 1; j <= f[sum][i]; j++)
putchar(i + '0');
putchar('\n'); return 0;
}

就依次输入,结果记一下,在主程序中打个表:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
return x * f;
} #define maxn 20
int n, cnt[maxn];
char num[maxn]; int main() {
n = read();
scanf("%s", num + 1); for(int i = 1; i <= n; i++) {
if(num[i] == '9') {
cnt[7]++; cnt[3]++; cnt[3]++; cnt[2]++;
}
if(num[i] == '8') {
cnt[7]++; cnt[2]++; cnt[2]++; cnt[2]++;
}
if(num[i] == '7') {
cnt[7]++;
}
if(num[i] == '6') {
cnt[5]++; cnt[3]++;
}
if(num[i] == '5') {
cnt[5]++;
}
if(num[i] == '4') {
cnt[3]++; cnt[2]++; cnt[2]++;
}
if(num[i] == '3') {
cnt[3]++;
}
if(num[i] == '2') {
cnt[2]++;
}
} for(int i = 7; i >= 2; i--)
for(int j = 1; j <= cnt[i]; j++) putchar(i + '0');
putchar('\n'); return 0;
}

A 啦!

[codeforces 516]A. Drazil and Factorial的更多相关文章

  1. codeforces 515C C. Drazil and Factorial(水题,贪心)

    题目链接: C. Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  2. 【codeforces 515C】Drazil and Factorial

    [题目链接]:http://codeforces.com/contest/515/problem/C [题意] 定义f(n)=n这个数各个位置上的数的阶乘的乘积; 给你a; 让你另外求一个不含0和1的 ...

  3. Codeforces Round #292 (Div. 1)A. Drazil and Factorial 构造

    A. Drazil and Factorial 题目连接: http://codeforces.com/contest/516/problem/A Description Drazil is play ...

  4. CodeForces 515C. Drazil and Factorial

    C. Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  5. Codeforces Round #292 (Div. 2) C. Drazil and Factorial 515C

    C. Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  6. CF Drazil and Factorial (打表)

    Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. Codeforces Round #292 (Div. 2) C. Drazil and Factorial

    题目链接:http://codeforces.com/contest/515/problem/C 给出一个公式例如:F(135) = 1! * 3! * 5!; 现在给你一个有n位的数字a,让你求这样 ...

  8. codeforces 515C. Drazil and Factorial 解题报告

    题目链接:http://codeforces.com/problemset/problem/515/C 题目意思:给出含有 n 个只有阿拉伯数字的字符串a(可能会有前导0),设定函数F(a) = 每个 ...

  9. CodeForces 516A Drazil and Factorial 动态规划

    原文链接http://www.cnblogs.com/zhouzhendong/p/8990592.html 题目传送门 - CodeForces 516A 题意 对于一个正整数$x$,$f(x)=x ...

随机推荐

  1. [USACO2003][poj2187]Beauty Contest(凸包+旋转卡壳)

    http://poj.org/problem?id=2187 题意:老题了,求平面内最远点对(让本渣默默想到了悲剧的AHOI2012……) 分析: nlogn的凸包+旋转卡壳 附:http://www ...

  2. ACL权限的学习

    ACL ACL:访问控制列表,其主要作用是将一些"用户"加到表中,并对这些用户的行为进行控制. 案例: 有个文件夹project是root用户创建,并且关于这个文件夹有以下权限 d ...

  3. Bootstrap3.0学习第十七轮(JavaScript插件——模态框)

    详情请查看http://aehyok.com/Blog/Detail/24.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...

  4. AngularJs-指令1

    前言: 前面写的有些乱,并且有些罗嗦,以后会注意的.希望我写的文章能帮助大家. 1,什么是指令 简单的说,指令是angularjs在html页面中建立一套自己能识别的标签元素.属性.类和注释,用来达到 ...

  5. java定时器的使用(Timer)

    1.在应用开发中,经常需要一些周期性的操作,比如每5分钟执行某一操作等. 对于这样的操作最方便.高效的实现方式就是使用java.util.Timer工具类. private java.util.Tim ...

  6. 【BZOJ 1013】【JSOI2008】球形空间产生器sphere 高斯消元基础题

    最基础的高斯消元了,然而我把j打成i连WA连跪,考场上再犯这种错误就真的得滚粗了. #include<cmath> #include<cstdio> #include<c ...

  7. 【HDU 1445】Ride to School

    题 题意 骑自行车,等0时开始最早出发的人,一起出发,然后被别人超过时,就追上去,终点距离是4.5km,速度单位是km/s,求到达的时间(s). 分析 贪心,找0时开始最早到的即可. 代码 #incl ...

  8. 【CodeForces 611C】New Year and Domino

    题 题意 h行w列的矩形格子,“." 代表空的,"#" 代表满的,多米诺是 1*2 的长方体,现在放进格子,给你子矩形的左上角和右上角,问在子矩形里共有多少种放一块多米诺 ...

  9. configure new linux

    vim   http://www.cnblogs.com/wswang/p/5088078.html zsh  sh -c "$(curl -fsSL https://raw.github. ...

  10. android anr分析方法

    目录(?)[+] 案例1关键词ContentResolver in AsyncTask onPostExecute high iowait 案例2关键词在UI线程进行网络数据的读写   一:什么是AN ...