Promi_38

cf 1471B

Jan 6th, 2021
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.51 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. int main()
  4. {
  5.     int t;
  6.     scanf("%d", &t);
  7.    
  8.     while(t--)
  9.     {
  10.         long long n, x;
  11.         scanf("%lld %lld", &n, &x);
  12.         long long a[n], i;
  13.         for(i = 0; i < n; i++) scanf("%lld", &a[i]);
  14.        
  15.         long long s = 0, c = 1;
  16.         for(i = 0; i < n; i++) s += a[i];
  17.        
  18.         i = 0;
  19.         while(1)
  20.         {
  21.             if(a[i] % x != 0) break;
  22.             else
  23.             {
  24.                 s += a[i] * c;
  25.                 a[i] /= x;
  26.             }
  27.             //printf("s %lld\n", s);
  28.             if(i == n - 1)
  29.             {
  30.                 i = 0;
  31.                 c *= x;
  32.             }
  33.             else i++;
  34.         }
  35.         printf("%lld\n", s);
  36.     }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment