文章目录
前言
出于某些原因,模拟了一下2023年上海大学ACM校赛练练手,随便写了几个题,整理一下自己的题解。
题目地址
A.Antiamuny wants to learn binary search
傻呗签到题,略。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int f(int l,int r,int x) {
// l <= x <= r
int cnt = 0;
while(l <= r) {
cnt++;
int mid = (l + r) / 2;
if (mid == x)
break;
if (mid < x)
l = mid + 1;
else
r = mid - 1;
}
return cnt;
}
int main(){
int t;
cin >> t;
while(t--){
int l,r,x;
cin >> l >> r >> x;
cout << f(l,r,x) << endl;
}
}
B.Bespread with chequers
稍加观察可以得到,对于 2 × n 2×n 2×n的情况下,可以由 2 × ( n − 1 ) 2×(n-1) 2×(n−1)的基础上拼一个 2 × 1 2×1 2×1的块,或者在 2 × ( n − 2 ) 2×(n-2) 2×(n−2)的基础上拼一个 2 × 2 2×2 2×2或两个 2 × 1 2×1 2×1的块,即 a n = a n − 1 + 2 a n − 2 a_{n}=a_{n-1}+2a_{n-2} an=an−1+2an−2
由于题目数据范围在 1 e 6 1e6 1e6内,但考虑到有 1 e 3 1e3 1e3组数据,考虑打表,时间复杂度 O ( n ) O(n) O(n)。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll maxn=1e6;
const ll mod=1e9+7;
int a[maxn+10];
int main(){
int t;
cin >> t;
a[1]=1,a[2]=3;
for(int i=3;i<=maxn;i++){
a[i]=(2*a[i-2]%mod+a[i-1])%mod;
}
while(t--){
int n;
cin >> n;
cout << a[n] << endl;
}
}
G.Golden jade matrix checker
按照定义就是,给定一个 n × m n \times m n×