[题解]P9714 「QFOI R1」摸摸

思路

首先发现对于操作 1 显然进行一次就可以将 \(t\) 变为一个回文序列。

那么,不难发现如果进行两次操作 1,在进行一次操作 2,是与进行一次操作 1,在进行两次操作 2 等效的。

所以考虑处理出 \(tt\) 表示 \(t\) 进行操作 1 后的序列。

不妨枚举一个 \(x,y\) 分别表示使用 \(t\)\(tt\) 的使用数量。

然后可以 \(\Theta(n)\) 求出 \(a\),判断是否与 \(b\) 相等即可。

注意:我们可以在枚举 \(x,y\) 时,加上 \(x \times t_1 \leq b_1\)\(x \times t_1 + y \times t_1 \leq b_1\) 的条件,如果它们大于 \(b_1\) 一定凑不出来,同时能保障时间复杂度是 \(\Theta(nw)\) 的,其中 \(w\) 表示值域。

Code

#include <bits/stdc++.h>  
#define re register  
  
using namespace std;  
  
const int N = 2010;  
int T,n;  
int b[N],t[N],tt[N];  
  
inline int read(){  
    int r = 0,w = 1;  
    char c = getchar();  
    while (c < '0' || c > '9'){  
        if (c == '-') w = -1;  
        c = getchar();  
    }  
    while (c >= '0' && c <= '9'){  
        r = (r << 3) + (r << 1) + (c ^ 48);  
        c = getchar();  
    }  
    return r * w;  
}  
  
int main(){  
    T = read();  
    while (T--){  
        bool falg = false;  
        n = read();  
        for (re int i = 1;i <= n;i++) b[i] = t[i] = tt[i] = 0;  
        for (re int i = 1;i <= n;i++) t[i] = read();  
        for (re int i = 1;i <= n;i++) b[i] = read();  
        for (re int i = 1;i <= n;i++) tt[i] = t[i] + t[n - i + 1];  
        for (re int x = 0;x * t[1] <= b[1];x++){  
            for (re int y = 0;x * t[1] + y * tt[1] <= b[1];y++){  
                bool ok = true;  
                for (re int i = 1;i <= n;i++){  
                    if (b[i] != x * t[i] + y * tt[i]){  
                        ok = false;  
                        break;  
                    }  
                }  
                if (ok){  
                    falg = true;  
                    break;  
                }  
            }  
        }  
        if (falg) puts("Yes");  
        else puts("No");  
    }  
    return 0;  
}  
posted @ 2024-06-26 12:37  WBIKPS  阅读(20)  评论(0)    收藏  举报