3636. 数组延伸
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 100010;
int n, x;
int w[N];
int main() {
int T;
scanf("%d", &T);
while(T--){
scanf("%d%d", &n, &x);
for (int i = 0; i < n;++i)
scanf("%d", &w[i]);
ll sum = 0, psum = 0;
int cnt = N;
for (int i = 0; i < n;++i){
sum += w[i];
int c = 0;
for (int j = w[i]; j % x == 0;j/=x)
c++;
if(c<cnt){
cnt = c;
psum = sum - w[i];
}
}
printf("%lld\n", sum * (cnt + 1) + psum);
}
return 0;
}
题解:找规律,一个数字n被x整除,得到商n/x,而且有x个n/x,所以后面添加的x个数的总和也还是n,如此一来只需要算出一个数n能被x整除k次便能求出后面数组添加的数字总和为k*n,再加上原有数组的n,就是(k+1) * n 。