Codeforces 1471 B. Strange List

Codeforces 1471 B. Strange List
在这里插入图片描述
思路分析:
最开始用的队列,结果 test5 报 T 了,所以只能找找规律。。。

16可以整除4次x,2只能整除x1次,4能够整除x2次。

16 2 4 8 8
16 2 4 8 8 1 1
16 2 4 8 8 1 1 2 2
16 2 4 8 8 1 1 2 2 4 4
16 2 4 8 8 1 1 2 2 4 4 4 4
然后我们就发现:16只能用2次,4只能用1次,而2是阻断操作的原因。

关键就来了,我们找到序列中能够对x整除次数cnt最少的那个位置,那么在它之前的数最多也只能操作cnt+1次,在它之后(包含本身)的数最多只能操作cnt次。

AC代码:

#include <iostream>

#define ll long long
#define inf 0x3f3f3f    // 表示无限大
using namespace std;

const int N = 1e5 + 10;

int a[N];

int main() {
    
    
    int t;
    cin >> t;
    while(t--) {
    
    
        int n,x;
        cin >> n >> x;
        ll pos = 0,cnt = inf;
        ll sum = 0;
        for(int i = 0; i < n; i++) {
    
    
            cin >> a[i];
            ll s = 0;
            for(int j = x;;j *= x) {
    
    
                if(a[i] % j == 0)
                    s++;
                else
                    break;
            }
            if(s < cnt) {
    
    
                pos = i;
                cnt = s;
            }
        }
        for(int i = 0;i<pos;i++)
            sum += (cnt + 2) * a[i];
        for(int i = pos;i<n;i++)
            sum += (cnt + 1) * a[i];
        cout << sum << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_45654671/article/details/112727544