1070A - Find a Number - CodeForces Solution


dp graphs number theory shortest paths *2200

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;

int d, s;
bool vis[505][5005];

struct state {
  int d, s;
  string cur;
  state (int d = 0, int s = 0, string cur = "") : d(d), s(s), cur(cur) {};
};

void bfs() {
  cin >> d >> s;
  queue <state> q;
  q.push(state(0, 0, ""));
  vis[0][0] = true;
  while(not q.empty()) {
    state val = q.front();
    q.pop();
    if(val.s > s) continue;
    if(val.d == 0 and val.s == s) {
      cout << val.cur << "\n";
      return;
    }
    for(int i = 0; i < 10; i++) {
      int cur_d = (val.d * 10 + i) % d;
      int cur_s = val.s + i;
      if(!vis[cur_d][cur_s]) {
        vis[cur_d][cur_s] = true;
        q.push(state(cur_d, cur_s, val.cur + (char) (i + '0')));
      }
    }
  }
  cout << -1 << "\n";
}

int main() {
  bfs();
  return 0;
}
 	 				 		  	 	 			    				


Comments

Submit
0 Comments
More Questions

129A - Cookies
1367B - Even Array
136A - Presents
1450A - Avoid Trygub
327A - Flipping Game
411A - Password Check
1520C - Not Adjacent Matrix
1538B - Friends and Candies
580A - Kefa and First Steps
1038B - Non-Coprime Partition
43A - Football
50A - Domino piling
479A - Expression
1480A - Yet Another String Game
1216C - White Sheet
1648A - Weird Sum
427A - Police Recruits
535A - Tavas and Nafas
581A - Vasya the Hipster
1537B - Bad Boy
1406B - Maximum Product
507B - Amr and Pins
379A - New Year Candles
1154A - Restoring Three Numbers
750A - New Year and Hurry
705A - Hulk
492B - Vanya and Lanterns
1374C - Move Brackets
1476A - K-divisible Sum
1333A - Little Artem