fork download
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3.  
  4. using namespace std;
  5.  
  6. const int MOD = 1e9 + 7;
  7.  
  8. void solve(){
  9. int n, k;
  10. cin >> n >> k;
  11. int g = 0;
  12.  
  13. for(int i = 0; i < n; i++){
  14. int x;
  15. cin >> x;
  16. g = __gcd(g, x);
  17. }
  18. if(n == 1){
  19. cout << k - (k <= g) << "\n";
  20.  
  21. return;
  22. }
  23.  
  24. int cur = 0;
  25. int cnt = 0;
  26.  
  27. for(int i = 1; i < n; i++){
  28. if(cnt + (g - 1) >= k){
  29. cout << cur + (k - cnt) << "\n";
  30. return;
  31. }
  32. cur += g;
  33. cnt += (g - 1);
  34. }
  35. cout << cur + (k - cnt) << "\n";
  36. }
  37.  
  38. int main(){
  39. ios_base::sync_with_stdio(false);
  40. cin.tie(nullptr);
  41.  
  42. int t = 1;
  43. cin >> t;
  44.  
  45. for(int i = 1; i <= t; i++){
  46. solve();
  47. }
  48. return 0;
  49. }
Success #stdin #stdout 0.01s 5276KB
stdin
6
1 3
3
2 10
1 1
3 1
1 2 3
3 2
1 2 4
4 5
2 2 2 16
4 5
2 2 2 3
stdout
2
11
3
4
8
8