CF 1070A Encuentra un número (Pensamiento + bfs)

Título: https://codeforces.com/problemset/problem/1070/A

Pedirle que divida a, el entero positivo más pequeño cuya suma de dígitos es b.

Ideas:

He sido dp digital por primera vez y encontré algo mal

Más tarde, sigo leyendo la solución, que en realidad es muy simple. La respuesta debe estar en una matriz bidimensional (i es la suma, j es el resto)

Al principio, solo teníamos [0] [0]. Continuamos transfiriendo agregando números al final. El estado transferido no necesitaría ser transferido. Obviamente era una cola. Empuje la posición recién transferida y la posición anterior emerge

Cada estado se recorre como máximo una vez, nuestro método garantiza lo mejor (por supuesto, debe realizar bfs de 0 a 9)

#define IOS ios_base :: sync_with_stdio (0); cin.tie (0); 
#include <cstdio> // sprintf islower isupper 
#include <cstdlib> // malloc exit strcat itoa system ("cls") 
#include <iostream> // pair 
#include <fstream> // freopen ("C: \\ Users \\ 13606 \\ Desktop \\ Input.txt "," r ", stdin); 
#include <bitset> // #include <map>
 // #include <unordered_map> 
#include <vector> 
#include <stack> 
#include < set > 
#include < string .h>

#include < string > 
#include <time.h> // srand (((unsigned) time (NULL))); Semilla n = rand ()% 10 - 0 ~ 9; 
#include <cmath> 
#include <deque> 
#include <queue> // priority_queue <int, vector <int>, mayor <int>> q; // menos 
#include <vector> // emplace_back
 // #include <math.h> 
#include <cassert> 
#include <iomanip>
 // #include <windows.h> // reverse (a, a + len); // ~! ~! floor 
#include <algorithm> //sort + unique: sz = unique (b + 1, b + n + 1) - (b + 1); + nth_element (first, nth, last, compare) 
usando el  espacio de nombres std; // next_permutation (a + 1, a + 1 + n); // prev_permutation
 // ****************** 
clock_t __START, __ END;
doble __TOTALTIME;
void _MS () {__ START = clock ();}
 void _ME () {__ END = clock (); __ TOTALTIME = ( double ) (__ END -__ START) / CLOCKS_PER_SEC; cout << " Time: " << __ TOTALTIME << " s " << endl;}
 // *********************** 
#define rint register int
 #definefo (a, b, c) para (rint a = b; a <= c; ++ a)
 #define fr (a, b, c) for (rint a = b; a> = c; - a)
 #define mem (a, b) memset (a, b, sizeof (a))
 #define pr printf
 #define sc scanf
 #define ls rt << 1
 #define rs rt << 1 | 1 
typedef pair < int , int > PII; 
typedef vector < int > VI; 
typedef unsigned largo  largo ull; 
typedef largo  largo ll; 
typedef double db;
const db E = 2.718281828 ;
const db PI = acos (- 1.0 );
const ll INF = (1LL << 60 );
const  int inf = ( 1 << 30 );
const db ESP = 1e- 9 ;
const  int N = ( int ) 1e6 + 10 ; 

struct node 
{ 
    int px, py, num; 
} dp [ 5005 ] [ 505 ];
bool vis [ 5005 ] [ 505 ]; 

cola <nodo> q;
nulo Init ( int  mod)
{ 
    para( int i = 1 ; i <= 9 ; ++ i) q.push ({i, i% mod, i}), vis [i] [i% mod] = 1 , dp [i] [i% mod ] = {- 1 , - 1 , i};
    while (! q.empty ()) 
    { 
        nodo ahora = q.front (); q.pop ();
        para ( int i = 0 ; i <= 9 ; ++ i) 
        { 
            if (now.px + i <= 5000 ) 
            { 
                if (! vis [now.px + i] [(now.py * 10 + i) % mod]) 
                {
                    q.push ({now.px 10 + i, (now.py * + i)% mod, i}); 
                    vis [now.px + i] [(now.py * 10 + i)% mod] = 1 ; 
                    dp [now.px + i] [(now.py * 10 + i)% mod] = {now.px, now.py, i}; 
                } 
            } 
        } 
    } 
} 
int ans [N], cnt; 

int main () 
{ 
    int n, mod; 
    sc ( " % d% d " , & mod, & n); 
    Init (mod); 
    if (! vis [n] [ 0 ]) 
        pr ( " -1 \ n " );
    más
    { 
        nodo ahora = dp [n] [ 0 ];
        while ( 1 ) 
        { 
            ans [ ++ cnt] = now.num;
            if (now.px == - 1 )
                 break ;
            más 
                ahora = dp [now.px] [now.py]; 
        } 
        para ( int i = cnt; i> = 1 ; - i) 
            pr ( " % d " , ans [i]); 
    } 
    devuelve  0 ; 
} 

/ * *********************************************** ************************************* * /

 

Supongo que te gusta

Origin www.cnblogs.com/--HPY-7m/p/12684019.html
Recomendado
Clasificación