码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #292 Div1 A

时间:2015-02-18 06:13:14      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:素数相关

Problem

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. F(x)=F(a) .

Help friends find such number.

Limits

TimeLimit(ms):2000

MemoryLimit(MB):256

n[1,15](the number of digits in a)

There is at least one digit in a that is larger than 1. Number a may possibly contain leading zeroes.

Look up Original Problem From here

Solution

考虑数a的每个digit,0和1忽略,其余若是素数则无法拆分,即2!=2!3!=3!5!=5!7!=7!

非素数可以拆分:

4!=4×3!=2!×2!×3!

6!=6×5!=3!×5!

8!=2!×2!×2!×7!

9!=2!×3!×3!×7!

按此维护答案即可

Complexity

TimeComplexity:O(n)

MemoryComplexity:O(n)

My Code

//Hello. I‘m Peter.
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef unsigned int uin;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define randin srand((unsigned int)time(NULL))
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define gsize(a) (int)a.size()
#define len(a) (int)strlen(a)
#define slen(s) (int)s.length()
#define pb(a) push_back(a)
#define clr(a) memset(a,0,sizeof(a))
#define clr_minus1(a) memset(a,-1,sizeof(a))
#define clr_INT(a) memset(a,INT,sizeof(a))
#define clr_true(a) memset(a,true,sizeof(a))
#define clr_false(a) memset(a,false,sizeof(a))
#define clr_queue(q) while(!q.empty()) q.pop()
#define clr_stack(s) while(!s.empty()) s.pop()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define dep(i, a, b) for (int i = a; i > b; i--)
#define repin(i, a, b) for (int i = a; i <= b; i++)
#define depin(i, a, b) for (int i = a; i >= b; i--)
#define pi 3.1415926535898
#define eps 1e-9
#define MOD 1000000007
#define MAXN (1<<10)+10
#define N 110
#define M
int n;
string s,ans,digit[N];
int main(){
    digit[0]=digit[1]="";
    digit[2]="2";
    digit[3]="3";
    digit[4]="223";
    digit[5]="5";
    digit[6]="35";
    digit[7]="7";
    digit[8]="2227";
    digit[9]="2337";
    cin>>n;
    cin>>s;
    ans.clear();
    rep(i,0,n){
        ans+=digit[s[i]-‘0‘];
    }
    sort(ans.begin(),ans.end());
    depin(i,slen(ans)-1,0){
        cout<<ans[i];
    }
    cout<<endl;
}

Codeforces Round #292 Div1 A

标签:素数相关

原文地址:http://blog.csdn.net/uestc_peterpan/article/details/43875129

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!