列表

详情


NC213987. MagicalNumber

描述

We consider a natural number p withk digits,, is magical only when it satisfies:
Every number composed by leading digits of p can be divisible by the number of its digits.
More formally,∀i∈[1,k].

For example, 123 is magical, because1|1, 2|12, 3|123.

However124 is not magical, because 3∤124.

Every digit can be composed with match sticks in the following ways.

What is the largest posible magical number you can compose with exactly n match sticks?

输入描述

The input contains a integer n(1≤n≤10100), the number of match sticks you have.

输出描述

Print the largest posible magical number x that can be possibly composed with exactly n match sticks.

If the number doesn't exist, print -1.

示例1

输入:

3

输出:

7

示例2

输入:

7

输出:

74

示例3

输入:

10000

输出:

-1

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++(clang++11) 解法, 执行用时: 6ms, 内存消耗: 376K, 提交时间: 2020-11-21 15:44:31

#include<bits/stdc++.h>
using namespace std;
__int128 ans=-1;
int num[]={6,2,5,5,4,5,6,3,7,6};
char s[105];
void dfs(__int128 tmp,int w,int pos){
	if(w==0){
		ans=max(tmp,ans);
		return;
	}
	for(int i=0;i<10;i++){
		if(w-num[i]>=0&&(tmp*10+i)%pos==0)
			dfs(tmp*10+i,w-num[i],pos+1);
	}
}
inline void print(__int128 x){
    if(x<0){
        putchar('-');
        x=-x;
    }
    if(x>9)
        print(x/10);
    putchar(x%10+'0');
}
int main(){
	scanf("%s",s);
	if(strlen(s)>3)	puts("-1");
	else{
		int n=0;
		for(int i=0;i<strlen(s);i++)	n=n*10+s[i]-'0';
		dfs(0,n,1);
		if(ans!=-1)	print(ans);
		else		puts("-1");
	}
}

pypy3(pypy3.6.1) 解法, 执行用时: 118ms, 内存消耗: 33068K, 提交时间: 2021-05-04 16:12:53

a = [6,2,5,5,4,5,6,3,7,6 ]
ans = -1
def dfs(pos, x ,res):
    if x == 0 :
        global ans
        ans = max(ans, res)
    for i in range(10):
        if i==0 and pos==1:
            continue
        if x < a[i]:
            continue
        if (res*10+i)%pos!=0:
            continue
        dfs(pos+1,x-a[i],res*10+i)
n=int(input())
dfs(1,n,0)
print(ans)

Python3 解法, 执行用时: 126ms, 内存消耗: 7044K, 提交时间: 2021-09-27 18:33:45

num = [6,2,5,5,4,5,6,3,7,6]
ans = -1
def dfs(u,sum,n):
    global ans,a
    if n<0:
        return
    if n==0:
        ans=max(ans,sum)
    for i in range(10):
        if(sum*10+i)%u==0:
            dfs(u+1,sum*10+i,n-num[i])
    return
n = input()
if len(n) <= 4:
    dfs(1,0,int(n))
print(ans)

上一题