[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. Fiddler工具的基本功能

    Fiddler是一款用于网页数据分析,抓取的工具,里面集成了对网页强大的功能外,还可以通过设置,使其对手机的数据也可以进行抓取 Fiddler的原理是: 通过在客户端和服务器之间创建一个代理服务器来对 ...

  2. [codevs 1995]黑魔法师之门(并查集)

    题目:http://codevs.cn/problem/1995/ 分析:脑补一下满足题目要求的子图肯定就是环……于是题目就变成了不断加边求环的个数.看起来有点麻烦……但是环的实质是几个小环组合起来的 ...

  3. xml基本操作

    在实际项目中遇到一些关于xml操作的问题,被逼到无路可退的时候终于决定好好研究xml一番.xml是一种可扩展标记语言,可跨平台进行传输,因此xml被广泛的使用在很多地方. 本文由浅入深,首先就xml的 ...

  4. Bootstrap3.0学习第十四轮(分页、徽章)

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

  5. nginx 配置虚拟主机

    文章转载自:http://www.ttlsa.com/html/1571.html 上篇说道我们的nginx是安装在/usr/local/nginx/ cd conf 我们现在把所有的虚拟主机放在一个 ...

  6. Hibernate-二级缓存 sessionFactory

    Hibernate 二级缓存 二级缓存需要sessionFactory来管理,它是进初级的缓存,所有人都可以使用,它是共享的. 当Hibernate根据ID访问数据对象的时候,首先从Session一级 ...

  7. SP*

    1.PS1——默认提示符 root@tcx2250-14:/etc# echo $PS1\u@\h:\w\$ \u是用户名 \h是主机名 \w是当前目录的完整路径.请注意当你在主目录下的时候,如上面所 ...

  8. CodeMap

    CodeMap 这是在博客园看到的一位朋友文章介绍的,很好用的插件,所有的方法,注释块在右边一目了然,找代码方便极了,还能设置代码段的高亮,给代码段设置标识

  9. 5种风格的 jQuery 分页效果【附代码】

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...

  10. Redis 下载

    https://github.com/MSOpenTech/redis/releases下载 Redis伴侣Redis Desktop Manager:http://redisdesktop.com/ ...