这里补BCD题,这次还是太菜了,只a了2题,D还理解错了,c是一点不会
B题
- B题wa了三发好惨,这个时间比较宽裕,所以有很多办法,最终基本都是DP
- 原本想的是把每一个数分解,求出他的因子,然后对因子进行操作
- 或者直接DP,直接对当前数值的倍数进行操作
- 这里比较坑的是让dist[i] = 1,原本只对1进行了赋初值,然后想到一个样子
- a[1] = 10, a[2] = 1, a[3] =2, a[4] =3,这里是输出 2,因为2,4符合,但是如果不都赋初值1,那么输出的只是1,所以这个地方挺坑的,需要注意一下
代码:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 100010;
int a[N], dist[N];
int main(){
int t;
scanf("%d",&t);
while(t--){
memset(dist,0,sizeof dist);
int n;
scanf("%d",&n);
for (int i = 1; i <= n; i++){
scanf("%d",&a[i]);
dist[i] = 1;
}
for (int i = 1; i <= n; i++){ // 直接倍数
int x = i;
for (int j = 1; x * j <= n; j ++){
if (a[j * i] > a[i]) dist[i*j] = max(dist[i*j] ,dist[i] + 1);
}
}
int res = 0;
for (int i = 1; i <= n; i++){
res = max(res,dist[i]);
}
printf("%d\n",res);
}
}
C题解题思路:
- C题有点复杂,自己想了好久才明白了一个复杂的做法,那种lcm又gcd的看不懂,还没理解好
- 先讲下质因子分解的思路
- 首先我们得出的数将他质因子分解 res = p1 ^ k1 * p2 ^ k2 …
- 然后我们对p1^k1举例,为了方便下面简化成 p ^ k
- 对于p ^ k, 由于p ^ k 是由于他们gcd得出的,所以是他们共有的
- 因此我们对他们的每个数都质因子分解,然后存储在vector里
- 然后我们从1 ~ 200000,这个范围(是因为输入的数据范围是1 ~ 2000000),我们依次判断他是否存在值 >= n - 1
- 为什么是n - 1,因为只有存在大于等于n - 1个数的质因子都存在,那么他们的lcm最后的gcd才能存在当前的值
- 如果数目是n,那么我选选取第二小的位置,就相当 2, 4,8他们的lcm的gcd肯定是4,因为他们受最少的这两个指数的约束,这个自己理解一下
- 然后如果数目是n - 1,那么就取最小值就可以,因为道理和上面的基本相同,因为缺少一位所以值受最小值的影响
- 然后他们相乘即可
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 200010;
vector<long long> ans[N];
ll mod_mult(ll a,ll b){ //计算 a*b
ll res = 0;
ll exp = a;
while(b){
if (b&1){
res += exp;
}
exp<<=1;
b>>=1;
}
return res;
}
ll mod_exp(ll a,ll b){
ll res = 1;
ll exp = a ;
while(b){
if (b & 1) res = mod_mult(res,exp);
exp = mod_mult(exp,exp);
b>>=1;
}
return res;
}
ll pow_(ll a, ll b){
for (int i = 1; i<= b; i++){
a = a * a;
}
return a;
}
int main(){
int n;
scanf("%d",&n);
for (int i = 1; i <= n; i++){
int x;
scanf("%d",&x);
for (int j = 2; j * j <= x; j ++){
if (x % j == 0){
int cnt = 0;
while(x % j == 0){
cnt++;
x /= j;
}
ans[j].push_back(cnt);
}
}
if (x != 1) ans[x].push_back(1);
}
ll res = 1;
for (long long i = 1; i <= 200000; i++){
if (ans[i].size() >= n - 1){
sort(ans[i].begin(),ans[i].end());
if (ans[i].size() == n) res *= mod_exp(i,ans[i][1]);
else res *= mod_exp(i,ans[i][0]);
}
}
printf("%lld\n",res);
return 0;
}
D题解题思路:
- D题相对于C题来说比较好理解,因为情况很少,不过一开始理解错题意了
- 这里寻找l - r 的中位数,那么这个中位数是值的中位数而不是下标的(理解错题意了)
- 所以首先判断这里面是否存在k,如果存在就成功了一半,如果不存在,当然是"no"
- 然后我们判断2个条件,是否存在2个连续的>= k 的值存在,因为如果存在,那么他可以将所有的值都变为>= k,然后如果原本存在k,那么就可以都变为k
- 还有一个是是否存在3个连续的值当中,里面存在2个值>= k,其中寻找的就是3个值中是否至少有2个值>=k 即可(这个就是一开始中位数理解错的地方),然后这两个条件一合并如果符合就"yes",否则就“no”。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int N = 100010;
int a[N];
int main(){
int t;
scanf("%d",&t);
while(t--){
int n, k;
scanf("%d%d",&n,&k);
int f = 0;
for (int i = 1; i <= n; i++){
scanf("%d",&a[i]);
if (a[i] == k) f = 1;
}
if (n == 1){
if (a[1] == k) puts("yes");
else puts("no");
continue;
}
int ff = 0;
for (int i = 1; i <= n; i++){
if (a[i] >= k){
if (i != n){
if (a[i + 1] >= k) ff = 1;
}
if (i < n - 1){
if (a[i + 2] >= k) ff = 1;
}
}
}
if (f && ff){
puts("yes");
}
else{
puts("no");
}
}
return 0;
}