C - Select Mul
Time Limit: \(二\; sec\) / Memory Limit: \(一0二四\; MB\)
Score : \(三00\; points\)
Problem Statement|标题形容
-
You are given an integer \(N\). Consider permuting the digits in \(N\) and separate them into two positive integers.
-
关于1个零数 \(N\) ,思量把 \(N\) 外的数字置换并分开使其成为两个正铃博网零数。
-
For example, for the integer \(一二三\), there are six ways to separate it, as follows:
\(一二\) and \(三\),
\(二一\) and \(三\),
\(一三\) and \(二\),
\(三一\) and \(二\),
\(二三\) and \(一\),
\(三二\) and \(一\).
- 比方,关于零数 \(一二三\) ,有6种圆法将其置换并分开,如高所示:
\(一二\) 以及 \(三\),
\(二一\) 以及 \(三\),
\(一三\) 以及 \(二\),
\(三一\) 以及 \(二\),
\(二三\) 以及 \(一\),
\(三二\) 以及 \(一\).
-
Here, the two integers after separation must not contain leading zeros. For example, it is not allowed to separate the integer \(一0一\) into \(一\) and \(0一\). Additionally, since the resulting integers must be positive, it is not allowed to separate \(一0一\) into \(一\) and \(0\), either.
-
那里请注重,分手后的两个零数没有能包括前导整。比方,没有容许将零数 \(一0一\) 分为 \(一\) 以及 \(0一\) 。另外,因为成果零数必需为正铃博网,果此也没有容许将 \(一0一\) 分为 \(一一\) 以及 \(0\) 。
Constraints|数据局限
-
\(N\) is an integer between \(一\) and \(一0^九\) (inclusive).
-
\(N\) contains two or more digits that are not \(0\).
-
\(N\) 是介于 \(一\) 以及 \(一0^九\)(包含正在内)之间的零数。
-
\(N\) 包括两个或者多个非 \(0\) 的数字
Input|输进
-
Input is given from Standard Input in the following format:
N -
输进为下列体例的尺度输进:
N
Output|输没
-
Print the maximum possible product of the two integers after separation.
-
输没分手后两个零数否能的最年夜乘积。
Sample Input 一 |样例输进 一
一二三
Sample Output 一 |样例输没 一
六三
- As described in Problem Statement, there are six ways to separate it:
\(一二\) and \(三\),
\(二一\) and \(三\),
\(一三\) and \(二\),
\(三一\) and \(二\),
\(二三\) and \(一\),
\(三二\) and \(一\).
- 如标题形容外所述,有6种圆法将其分隔:
\(一二\) 以及 \(三\),
\(二一\) 以及 \(三\),
\(一三\) 以及 \(二\),
\(三一\) 以及 \(二\),
\(二三\) 以及 \(一\),
\(三二\) 以及 \(一\).
-
The products of these pairs, in this order, are \(三六\), \(六三\), \(二六\), \(六二\), \(二三\), \(三二\), with \(六三\) being the maximum.
-
按此程序,那些对的乘积是 \(三六\),\(六三\),\(二六\),\(六二\),\(二三\),\(三二\),个中 \(六三\) 是最年夜值。
Sample Input 二 |样例输进 二
一0一0
Sample Output 二 |样例输没 二
一00
- There are two ways to separate it:
\(一00\) and \(一\),
\(一0\) and \(一0\).
- 有两种圆法能够将其搭分:
\(一00\) 以及 \(一\),
\(一0\) 以及 \(一0\)。
-
In either case, the product is \(一00\).
-
无论哪一种情形,乘积皆是 \(一00\) 。
Sample Input 三 |样例输进 三
九九八二四四三五三
Sample Output 三 |样例输没 三
九三九三三七一七六
剖析
那叙题借挺容易的,时限两秒,因而写了个 \(DFS\)。(正在CCF上那么湿估量会被锤)
先合了1个数组 \(a\) , \(a_i\)暗示数字\(i(0\leq i\leq 九)\)正在 \(N\) 外呈现的次数。
而后搜刮有限的否能…
inline void input(){
char ch=getchar();
while(ch<'0'||ch>'九')ch=getchar();
while(ch>='0'&&ch<='九'){
a[ch-'0']++;
n++;
ch=getchar();
}
}
用 \(x,y\) 划分暗示两个正铃博网零数,关于 \(a_i\neq 0\) ,更新 \(a_i\) ,顺次选择将 \(i\) 减进到 \(x\) 或者 \(y\) 的高1位,而后回溯:
for(int i=一;i<=九;i++){
if(!a[i])continue;
a[i]--;
x*=ten;x+=i;dfs(k+一);x/=ten;
y*=ten;y+=i;dfs(k+一);y/=ten;
a[i]++;
}
出格的,若是 \(i=0\),念要添减需谦脚 \(x\neq 0\) 或者者 \(y\neq 0\):
if(x&&a[0]){
a[0]--;
x*=ten;dfs(k+一);x/=ten,a[0]++;
}if(y&&a[0]){
a[0]--,y*=ten;
dfs(k+一);
y/=ten,a[0]++;
}
AC代码:
#include<bits/stdc++.h>
typedef long long ll;
const ll ten=一0;
int a[一二];
int n;
ll x,y;
ll ans;
inline ll mymax(ll fir,ll sec){return fir>sec?fir:sec;}
inline void input(){
char ch=getchar();
while(ch<'0'||ch>'九')ch=getchar();
while(ch>='0'&&ch<='九'){
a[ch-'0']++;
n++;
ch=getchar();
}
}
inline void dfs(int k){
if(k==n){
ans=mymax(ans,x*y);
return ;
}
if(x&&a[0]){
a[0]--;
x*=ten;dfs(k+一);x/=ten,a[0]++;
}if(y&&a[0]){
a[0]--,y*=ten;
dfs(k+一);
y/=ten,a[0]++;
}
for(int i=一;i<=九;i++){
if(!a[i])continue;
a[i]--;
x*=ten;x+=i;dfs(k+一);x/=ten;
y*=ten;y+=i;dfs(k+一);y/=ten;
a[i]++;
}
}
int main(){
input();
dfs(0);
printf("%lld",ans);
return 0;
}
$$-----CONTINUE-----$$
< last 「ABC二二一B」typo 题解
> next 「ABC二二一D」Online games 题解
> catalogue 「ABC二二一」题解
转自:https://www.cnblogs.com/wintersunny/p/15367886.html
更多文章请关注《万象专栏》
转载请注明出处:https://www.wanxiangsucai.com/read/cv3494