A. Era
維護一個新數組的末尾位置變量pos,遍歷的時候不斷更新即可。
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#define mod 1000000007
//#define mod 998244353
#define ll long long
#define pb push_back
using namespace std;
ll fpow(ll a, ll b) {
ll ans = 1;
for(; b; b >>= 1) {
if(b & 1) ans = ans * a % mod;
a = a * a % mod;
}
return ans;
}
ll gcd(ll a, ll b) {
return b ? gcd(b, a % b) : a;
}
int a[105], n;
int main() {
int T = 1;
cin >> T;
while(T--) {
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
ll ans = 0;
a[0] = 0;
int pos = 1;
for(int i = 1; i <= n; i++) {
if(a[i] > pos) {
ans += a[i] - pos;
pos += a[i] - pos;
}
pos++;
}
cout << ans << endl;
}
return 0;
}
B. XOR Specia-LIS-t
注意到長度為1的序列也滿足單調遞增的條件(雖然比較特殊),所以如果n為偶數一定可以(把序列分為n個子數組,每個子數組的最長單調遞增子列長度為1,且一共有偶數個);如果n為奇數的話,只要整個序列不是單調遞增的話一定可以(把a[i] >= a[i + 1]的這兩個數拿出來,其最長遞增子序列長度為1,剩下的n - 2個數自己構成一個子數組,這樣保證xor和為0),否則不可(因為每個子數組的LCS就是本身,整個序列一定要被划分為若干對,n為奇數顯然不可能划分為若干“對”)
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#define mod 1000000007
//#define mod 998244353
#define ll long long
#define pb push_back
using namespace std;
ll fpow(ll a, ll b) {
ll ans = 1;
for(; b; b >>= 1) {
if(b & 1) ans = ans * a % mod;
a = a * a % mod;
}
return ans;
}
ll gcd(ll a, ll b) {
return b ? gcd(b, a % b) : a;
}
ll n, a[100005];
int main() {
int T = 1;
cin >> T;
while(T--) {
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
if(n % 2 == 0) puts("YES");
else {
bool ok = 0;
for(int i = 2; i <= n; i++) {
if(a[i] <= a[i - 1]) {
ok = 1;
break;
}
}
if(ok) puts("YES");
else puts("NO");
}
}
return 0;
}
C. Di-visible Confusion
這個題其實可以暴力,對於每個數的位置pos直接判斷2到pos+1有沒有滿足條件的位置(因為隨着數列的變化,一個數的位置一定是單調不增的),如果沒有則輸出NO。所有數都可以則輸出YES。
那么為什么能暴力呢?因為如果2到pos+1所有位置都不滿足條件(即a[i]都是它們的倍數),如果考慮2到pos+1所有的素數,a[i]一定是這些素數若干次冪的乘積(唯一分解定理),而1到100所有的素數的乘積就已經爆int了,所以內循環根本跑不到100,故暴力可行。
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#define mod 1000000007
//#define mod 998244353
#define ll long long
#define pb push_back
using namespace std;
ll fpow(ll a, ll b) {
ll ans = 1;
for(; b; b >>= 1) {
if(b & 1) ans = ans * a % mod;
a = a * a % mod;
}
return ans;
}
ll gcd(ll a, ll b) {
return b ? gcd(b, a % b) : a;
}
ll n, a[100005];
int main() {
int T = 1;
cin >> T;
while(T--) {
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
//最后一定是一個奇數
bool ok = 1;
for(int i = 1; i <= n; i++) {
bool flag = 0;
for(int j = 2; j < i + 2; j++) {
if(a[i] % j != 0) {
flag = 1;
break;
}
}
if(!flag) {
cout << "NO" << endl;
ok = 0;
break;
}
}
if(ok) cout << "YES" << endl;
}
return 0;
}
D. Moderate Modular Mode
實際上是分類討論的構造題,具體怎么構造見代碼。對於最后一種情況畫一下數軸就比較好理解了。
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <map>
#include <set>
#define mod 1000000007
//#define mod 998244353
#define ll long long
#define pb push_back
using namespace std;
ll fpow(ll a, ll b) {
ll ans = 1;
for(; b; b >>= 1) {
if(b & 1) ans = ans * a % mod;
a = a * a % mod;
}
return ans;
}
ll gcd(ll a, ll b) {
return b ? gcd(b, a % b) : a;
}
int a[105], n;
int main() {
int T = 1;
cin >> T;
while(T--) {
ll x, y;
cin >> x >> y;
if(x == y) {
cout << x << endl;
continue;
}
if(x > y) {
cout << x + y << endl;
} else {
if(y % x == 0) {
cout << x << endl;
} else {
ll n = (x + y) / 2;
if(n % x == y % n) {
cout << n << endl;
} else {
n = y - (y % x) / 2;
cout << n << endl;
}
}
}
}
return 0;
}