• CF 1070A Find a Number(思维+bfs)


    题意:https://codeforces.com/problemset/problem/1070/A

    问你整除a,位上数和为b的最小正整数。

    思路:

    一开始一直数位dp,发现不对劲

    后来还是看了题解,其实很简单,答案肯定是在一个二维矩阵里的(i为和,j为余数)

    一开始我们只有[0][0],我们通过不断向末尾添加数字进行转移,转移过的状态就不用转移了,明显就是一个queue,把新转移出来的位置push进去,旧位置pop

    每个状态最多遍历一次,我们的方法保证了最优(当然你得从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>//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
    ");
        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;
    }
    
    /**************************************************************************************/
  • 相关阅读:
    Git分支管理策略
    嵌入式文件系统构建工具 busybox / buildroot / openwrt
    nodejs与c语言交互应用实例
    python与c语言交互应用实例
    websocket programming base on nodejs
    Using Bluetooth LE with Go
    nodejs
    linux ipc/its
    SAMA5D3 Xplained Board
    BlueZ
  • 原文地址:https://www.cnblogs.com/--HPY-7m/p/12684019.html
Copyright © 2020-2023  润新知