CF 1070A Find a Number (Thinking + bfs)

Title: https://codeforces.com/problemset/problem/1070/A

Ask you to divide a, the smallest positive integer whose sum of digits is b.

Ideas:

I've been digitally dp for the first time and found something wrong

Later, I still read the solution, which is actually very simple. The answer must be in a two-dimensional matrix (i is the sum, j is the remainder)

At the beginning, we only had [0] [0]. We continued to transfer by adding numbers to the end. The transferred state would not need to be transferred. Obviously it was a queue. Push the newly transferred position into it, and the old position pop

Each state is traversed at most once, our method guarantees the best (of course you have to perform bfs from 0 to 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>//strstr substr strcat
#include <string>
#include <time.h>// srand(((unsigned)time(NULL))); Seed 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 <algorithm>//sort + unique : 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<<"Time: "<<__TOTALTIME<<" s"<<endl;}
//***********************
#define rint register int
#define fo(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<int,int> PII;
typedef vector<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;

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

queue<node>q;
void Init(int mod)
{
    for(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())
    {
        node now=q.front();q.pop();
        for(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+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");
    else
    {
        node now=dp[n][0];
        while(1)
        {
            ans[++cnt]=now.num;
            if(now.px==-1)
                break;
            else
                now=dp[now.px][now.py];
        }
        for(int i=cnt;i>=1;--i)
            pr("%d",ans[i]);
    }
    return 0;
}

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

 

Guess you like

Origin www.cnblogs.com/--HPY-7m/p/12684019.html
Recommended