Advertisement
a53

wl

a53
May 26th, 2022
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <queue>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <cstring>
  7.  
  8. using namespace std;
  9. #define Inf 0x3f3f3f3f
  10. #define mod 1e9+7
  11.  
  12. int n;
  13. queue<int> q;
  14. vector<int>minim(100005,Inf);
  15.  
  16. void lee(){
  17.  
  18. while(!q.empty()){
  19.  
  20. int x = q.front();
  21. q.pop();
  22. for(int d = 1 ; d * d <= x ; d += 1 + x % 2){
  23. if(x % d == 0){
  24. if(x + d <= 100001 && minim[x + d] > minim[x] + 1){
  25. minim[x + d] = minim[x] + 1;
  26. q.push(x + d);
  27. }
  28. if(x + (x / d) <= 100001 && minim[x + (x / d)] > minim[x] + 1){
  29. minim[x + (x / d)] = minim[x] + 1;
  30. q.push(x + (x / d));
  31. }
  32. }
  33. }
  34. }
  35. }
  36.  
  37. int main(){
  38.  
  39. cin >> n;
  40. for(int i = 1,x ; i <= n ; ++i){
  41. cin >> x;
  42. q.push(x);
  43. minim[x] = 0;
  44. }
  45. lee();
  46. int q;
  47. cin >> q;
  48. for(int i = 1,x ; i <= q ; ++i){
  49. cin >> x;
  50. if(minim[x] == Inf)
  51. cout << "-1\n";
  52. else
  53. cout << minim[x] << '\n';
  54.  
  55. }
  56.  
  57.  
  58. return 0;
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement