[codeforces 516]A. Drazil and Factorial
[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.
输出
输入示例
输出示例
数据规模及约定
见“输入”
题解
这个题相当有意思,我看 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的更多相关文章
- codeforces 515C C. Drazil and Factorial(水题,贪心)
题目链接: C. Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes inpu ...
- 【codeforces 515C】Drazil and Factorial
[题目链接]:http://codeforces.com/contest/515/problem/C [题意] 定义f(n)=n这个数各个位置上的数的阶乘的乘积; 给你a; 让你另外求一个不含0和1的 ...
- Codeforces Round #292 (Div. 1)A. Drazil and Factorial 构造
A. Drazil and Factorial 题目连接: http://codeforces.com/contest/516/problem/A Description Drazil is play ...
- CodeForces 515C. Drazil and Factorial
C. Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 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 ...
- CF Drazil and Factorial (打表)
Drazil and Factorial time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces Round #292 (Div. 2) C. Drazil and Factorial
题目链接:http://codeforces.com/contest/515/problem/C 给出一个公式例如:F(135) = 1! * 3! * 5!; 现在给你一个有n位的数字a,让你求这样 ...
- codeforces 515C. Drazil and Factorial 解题报告
题目链接:http://codeforces.com/problemset/problem/515/C 题目意思:给出含有 n 个只有阿拉伯数字的字符串a(可能会有前导0),设定函数F(a) = 每个 ...
- CodeForces 516A Drazil and Factorial 动态规划
原文链接http://www.cnblogs.com/zhouzhendong/p/8990592.html 题目传送门 - CodeForces 516A 题意 对于一个正整数$x$,$f(x)=x ...
随机推荐
- #Linux学习笔记# 自定义shell终端提示符
我使用的Linux发行版是LinuxMint 17.2 Rafaela,默认情况下Terminal中的shell提示包括了用户名.主机名.当前目录(绝对路径)和提示符.这样会导致当进入一个比较深的目录 ...
- python3 入门 (二) 列表的使用
列表用于组织其它数值,即写在方括号之间.用逗号分隔开的数值列表.列表内的项目不必全是相同的类型. 列表的定义 student = ['Tom', 'Jack', 'Avril'] 添加元素 将另一个列 ...
- linux中的数值运算
一.declare 作用:声明变量类型,bash默认变量为字符串类型的,并且字符串在拼接时直接拼接,不需要加号 使用方法: 二.数值运算 加法运算 a= b= c=$(($a+$b)) echo $c
- emberJS
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- Yii2登陆添加验证码
models中 LoginForm.php public $verifyCode; public function rules() { return [ …… ['verifyCode', 'capt ...
- HDU1698 Just a Hook
Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of t ...
- groovy-脚本和类
在groovy中定义类和java中是一样的.类的方法可以是static,也可以是非static的. groovy中的方法可以是public, protected, private,同时也支持java中 ...
- .NET/MVC-发布到IIS6.1提示未能加载程序集System.Web.Http.WebHost
http://www.bubuko.com/infodetail-1128065.html vs2013发布后,自己的用iis7.0可以发布这个网站,但是用服务器2008 IIS6.1发布这个网站一直 ...
- Jsonp简单认识(后端使用的是asp.net mvc)
一.Jsonp简介:由于浏览器基于安全有同源策略(同源策略阻止从一个源加载的文档或脚本获取或设置另一个源加载的文档的属性)机制,所以前端无法使用Ajax来获取来获取其他域名下返回的数据,而Jsonp可 ...
- Java Web文件上传
参考资料:http://www.cnblogs.com/xdp-gacl/p/4200090.html 一.问题描述 Java Web文件上传需要借助一些第三方库,常用的是借助Apache的包,有两个 ...