1681D

Revision en1, by Yuchang_Zhu, 2023-02-18 05:34:30

1681D - Требуемая длина This is the topic of this question.

You are given two integer numbers, n and x. You may perform several operations with the integer x.

Each operation you perform is the following one: choose any digit y that occurs in the decimal representation of x at least once, and replace x by x⋅y.

You want to make the length of decimal representation of x (without leading zeroes) equal to n. What is the minimum number of operations required to do that?

Input The only line of the input contains two integers n and x (2≤n≤19; 1≤x<10n−1).

Output Print one integer — the minimum number of operations required to make the length of decimal representation of x (without leading zeroes) equal to n, or −1 if it is impossible.

This is the solution to this problem.(194061061)

include <bits/stdc++.h>

using namespace std; queue q; map <long long,int> m; int main() { int a; long long b; cin>>a>>b; q.push(b); m[b]=0; while (q.size()!=0) { b=q.front(); q.pop(); string s=to_string(b); if (s.size()==a) { cout<<m[b]; return 0; } for (int i=0;i<s.size();i++) { if (s[i]!='1'&&m[b*(s[i]-'0')]==0) { q.push(b*(s[i]-'0')); m[b*(s[i]-'0')]=m[b]+1; } } } cout<<"-1"<<endl; return 0; }

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en1 English Yuchang_Zhu 2023-02-18 05:34:30 1314 Initial revision (published)