CF 1070A番号を検索(思考+ bfs)

タイトル:https : //codeforces.com/problemset/problem/1070/A

桁数の合計がbである最小の正の整数aを除算するように求めます。

アイデア:

私は初めてデジタルdpをしていて、何か問題を見つけました

後で、私はまだ解を読んでいますが、これは実際には非常に簡単です。答えは2次元の行列である必要があります(iは合計、jは剰余です)。

最初は[0] [0]しかありませんでした。最後に番号を追加して転送を続行しました。転送された状態は転送する必要はありません。明らかにキューでした。新しく転送された位置をそこにプッシュし、古い位置をポップします。

各状態は最大で1回トラバースされます。この方法で最高の状態が保証されます(もちろん、0から9までのbfsを実行する必要があります)。

#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> // ペア 
#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 < 文字列 > 
#include <time.h> // srand((((unsigned)time(NULL))); シードn = rand()%10-0〜9; 
#include <cmath> 
#include <deque> 
#include <queue> // priority_queue <int、vector <int>、greater <int>> q; // less 
#include <vector> // emplace_back
 // #include <math.h> 
#include <cassert> 
#include <iomanip>
 // #include <windows.h> // reverse(a、a + len); // 〜!〜!floor 
#include <アルゴリズム> //ソート+一意:sz = unique(b + 1、b + n + 1)-(b + 1); + nth_element(first、nth、last、compare)
using  namespace std; // next_permutation(a + 1、a + 1 + n); // prev_permutation
 // ****************** 
clock_t __START、__ END;
double __TOTALTIME;
void _MS(){__ START = clock();}
 void _ME(){__ END = clock(); __ TOTALTIME =(double)(__ END -__ START)/ CLOCKS_PER_SEC; cout << " 時間:" << __ TOTALTIME << " s " << endl;} // *********************** 
#define rint
 register int
 #definefo(a、b、c)for(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 < intint > PII; 
typedefベクトル < int > VI; 
typedef unsigned long  long ull; 
typedef long  long 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 ; 

構造体ノード
{ 
    int px、py、num; 
} dp [ 5005 ] [ 505 ];
bool vis [ 5005 ] [ 505 ]; 

キュー <ノード> q;
void Init(int mod) 
{ 
    forint 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())
    { 
        node now = q.front(); q.pop();
        forint i = 0 ; i <= 9 ; ++ i)
        { 
            if(now.px + i <= 5000 
            { 
                if(!vis [now.px + i] [(now.py * 10 + i) %mod])
                {
                    q.push({now.px + i、(now.py * 10+ 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 " );
    そうしないと
    { 
        node now = dp [n] [ 0 ];
        while1 
        { 
            ans [ ++ cnt] = now.num;
            if(now.px ==- 1 ブレーク;
            他の = DP [now.px] [now.py]。
        } 
        forint i = cnt; i> = 1 ; -i)
            pr(" %d " 、ans [i]); 
    } 
    0を返し ます
} 

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

 

おすすめ

転載: www.cnblogs.com/--HPY-7m/p/12684019.html