1. 程式人生 > 其它 >「ABC221C」Select Mul 題解

「ABC221C」Select Mul 題解

C - Select Mul

Time Limit: \(2\; sec\) / Memory Limit: \(1024\; MB\)

Score : \(300\; points\)

Problem Statement|題目描述

  • You are given an integer \(N\). Consider permuting the digits in \(N\) and separate them into two positive integers.

  • 對於一個整數 \(N\) ,考慮把 \(N\) 中的數字置換並分隔使其成為兩個正整數

  • For example, for the integer \(123\)

    , there are six ways to separate it, as follows:

\(12\) and \(3\),
\(21\) and \(3\),
\(13\) and \(2\),
\(31\) and \(2\),
\(23\) and \(1\),
\(32\) and \(1\).

  • 例如,對於整數 \(123\) ,有六種方法將其置換並分隔,如下所示:

\(12\)\(3\),
\(21\)\(3\),
\(13\)\(2\),
\(31\)\(2\),
\(23\)\(1\),
\(32\)\(1\).

  • Here, the two integers after separation must not contain leading zeros. For example, it is not allowed to separate the integer \(101\)

    into \(1\) and \(01\). Additionally, since the resulting integers must be positive, it is not allowed to separate \(101\) into \(1\) and \(0\), either.

  • 這裡請注意,分離後的兩個整數不能包含前導零。例如,不允許將整數 \(101\) 分為 \(1\)\(01\) 。此外,由於結果整數必須為正,因此也不允許將 \(101\) 分為 \(11\)\(0\)

Constraints|資料範圍

  • \(N\) is an integer between \(1\)

    and \(10^9\) (inclusive).

  • \(N\) contains two or more digits that are not \(0\).

  • \(N\) 是介於 \(1\)\(10^9\)(包括在內)之間的整數。

  • \(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 1 |樣例輸入 1

123

Sample Output 1 |樣例輸出 1

63

  • As described in Problem Statement, there are six ways to separate it:

\(12\) and \(3\),
\(21\) and \(3\),
\(13\) and \(2\),
\(31\) and \(2\),
\(23\) and \(1\),
\(32\) and \(1\).

  • 如題目描述中所述,有六種方法將其分開:

\(12\)\(3\),
\(21\)\(3\),
\(13\)\(2\),
\(31\)\(2\),
\(23\)\(1\),
\(32\)\(1\).

  • The products of these pairs, in this order, are \(36\), \(63\), \(26\), \(62\), \(23\), \(32\), with \(63\) being the maximum.

  • 按此順序,這些對的乘積是 \(36\)\(63\)\(26\)\(62\)\(23\)\(32\),其中 \(63\) 是最大值。

Sample Input 2 |樣例輸入 2

1010

Sample Output 2 |樣例輸出 2

100

  • There are two ways to separate it:

\(100\) and \(1\),
\(10\) and \(10\).

  • 有兩種方法可以將其拆分:

\(100\)\(1\)
\(10\)\(10\)

  • In either case, the product is \(100\).

  • 無論哪種情況,乘積都是 \(100\)

Sample Input 3 |樣例輸入 3

998244353

Sample Output 3 |樣例輸出 3

939337176


分析

這道題還挺簡單的,時限兩秒,於是寫了個 \(DFS\)(在CCF上這麼幹估計會被錘)

先開了一個數組 \(a\) , \(a_i\)表示數字\(i(0\leq i\leq 9)\)\(N\) 中出現的次數。
然後搜尋無限的可能…

inline void input(){
	char ch=getchar();
	while(ch<'0'||ch>'9')ch=getchar();
	while(ch>='0'&&ch<='9'){
		a[ch-'0']++;
		n++;
		ch=getchar();
	}
}

\(x,y\) 分別表示兩個正整數,對於 \(a_i\neq 0\) ,更新 \(a_i\) ,依次選擇將 \(i\) 加入到 \(x\)\(y\) 的下一位,然後回溯:

for(int i=1;i<=9;i++){
	if(!a[i])continue;
	a[i]--;
	x*=ten;x+=i;dfs(k+1);x/=ten;
	y*=ten;y+=i;dfs(k+1);y/=ten;
	a[i]++;
}

特別的,如果 \(i=0\),想要新增需滿足 \(x\neq 0\) 或者 \(y\neq 0\)

if(x&&a[0]){
	a[0]--;
	x*=ten;dfs(k+1);x/=ten,a[0]++;
}if(y&&a[0]){
	a[0]--,y*=ten;
	dfs(k+1);
	y/=ten,a[0]++;
}

AC程式碼:

#include<bits/stdc++.h>
typedef long long ll;
const ll ten=10;
int a[12];
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>'9')ch=getchar();
	while(ch>='0'&&ch<='9'){
		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+1);x/=ten,a[0]++;
	}if(y&&a[0]){
		a[0]--,y*=ten;
		dfs(k+1);
		y/=ten,a[0]++;
	}
	for(int i=1;i<=9;i++){
		if(!a[i])continue;
		a[i]--;
		x*=ten;x+=i;dfs(k+1);x/=ten;
		y*=ten;y+=i;dfs(k+1);y/=ten;
		a[i]++;
	}
}
int main(){
	input();
	dfs(0);
	printf("%lld",ans);
	return 0;
}