Advertisement
Josif_tepe

Untitled

Dec 25th, 2022
912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <set>
  4. #include <cstring>
  5. #include <queue>
  6. using namespace std;
  7.  
  8. struct node {
  9.     int idx, cost;
  10.     node () {}
  11.     node(int _idx, int _cost) {
  12.         idx = _idx;
  13.         cost = _cost;
  14.     }
  15.     bool operator < (const node &t) const {
  16.         return cost > t.cost;
  17.     }
  18. };
  19. int main() {
  20.     int K;
  21.     cin >> K;
  22.     vector<int> dist(K + 1, 2e9);
  23.    
  24.     priority_queue<node> pq;
  25.     pq.push(node(K, 0));
  26.     while(!pq.empty()) {
  27.         node c = pq.top();
  28.         pq.pop();
  29.        
  30.         for(int i = 0; i <= 9; ++i) {
  31.             if(i == 0 and c.idx == K) continue;
  32.             int next_number = (c.idx * 10 + i) % K;
  33.             if(i + c.cost < dist[next_number]) {
  34.                 dist[next_number] = i + c.cost;
  35.                 pq.push(node(next_number, c.cost + i));
  36.             }
  37.         }
  38.     }
  39.     cout << dist[0] << endl;
  40.     return 0;
  41. }
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement