[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 ...
随机推荐
- android之文件存储和读取
一.权限问题 手机中存储空间分为ROM和SDcard,ROM中放着操作系统以及我们安装的APP,而sdcard中一般放置着我们APP产生的数据.当然,Android也为每个APP在ROM中创建一个数据 ...
- js中的DOM操作(2)
1.表格的更加与删除 <!DOCTYPE html> <html> <head> <title>表格操作</title> <style ...
- Linux中使用crontab命令定时执行shell脚本或其他Linux命令
使用crontab你可以在指定的时间执行一个shell脚本或者一系列Linux命令.例如系统管理员安排一个备份任务使其每天都运行 如何往 cron 中添加一个作业? # crontab –e0 5 * ...
- iOS开发中的错误整理,关于用绑定Tag取控件的注意事项,有时候不绑定也是个错!
如图:红色框中是个自定义的导航工具条titlesView(没有绑定Tag),工具条中有五个按钮(按钮绑定了Tag)以及一个红色的指示器indicatorView(没有绑定Tag),下面的蓝色是可以滚动 ...
- 【转】Dubbo_与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
原文链接:http://blog.csdn.net/congcong68/article/details/41113239 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服 ...
- Spring-事物的隔离级别
Spring中定义了5中不同的事务隔离级别: 1. ISOLATION_DEFAULT(一般情况下使用这种配置既可) ; 这是一个PlatfromTransactionManager默认的隔离级别,使 ...
- Java编程思想学习(十六) 并发编程
线程是进程中一个任务控制流序列,由于进程的创建和销毁需要销毁大量的资源,而多个线程之间可以共享进程数据,因此多线程是并发编程的基础. 多核心CPU可以真正实现多个任务并行执行,单核心CPU程序其实不是 ...
- Android Studio学习笔记
转:http://stormzhang.com/devtools/2014/11/25/android-studio-tutorial1 背景 相信大家对Android Studio已经不陌生了,An ...
- HackerRank Ice Cream Parlor
传送门 Ice Cream Parlor Authored by dheeraj on Mar 21 2013 Problem Statement Sunny and Johnny together ...
- jquery------隐式迭代
其中Jq方法遍历内部dom数组的过程就叫做[隐式迭代] my.js $(document).ready(function(){ (function($){ $.fn.swapClass=functio ...