2024.9.26
今日总结:
完成了作业中的三道题
1:GCD vs XOR
点击查看代码
#include<bits/stdc++.h>
using namespace std;
const int M = 1e6 + 10;
typedef long long LL;
int cnt[M];
vector<int> vec[M];
void init()
{
int cnt = 0;
for(int i = 1;i < M;i ++)
{
for(int j = 1;j * j <= i;j ++)
{
if(i % j) continue;
cnt ++;
int a = i;
int d = j;
int b = a ^ d;
if(b > 0 && a > b)
{
int g = __gcd(a,b);
if(g == d) vec[i].push_back(b);
}
if(j * j != i)
{
d = i / j;
b = a ^ d;
if(b > 0 && a > b)
{
int g = __gcd(a,b);
if(g == d) vec[i].push_back(b);
}
}
}
}
}
int main(){
init();
int T;
scanf("%d",&T);
while(T --)
{
int n;
scanf("%d",&n);
int tot = 0;
for(int i = 1;i <= n;i ++)
{
int x;
scanf("%d",&x);
cnt[x] ++;
tot = max(tot,x);
}
LL ans = 0;
for(int x = tot;x >= 1;x --)
{
for(auto y : vec[x])
ans += 1ll * cnt[x] * cnt[y];
cnt[x] = 0;
}
printf("%lld\n",ans);
}
return 0;
}
2:Emojis
点击查看代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const int INF = 10000000000;
int T,n,cnt,ans;
int a[N],dp[N],pos[N];
int main()
{
scanf("%d",&T);
while(T --)
{
cnt = 0;
ans = INF;
scanf("%d",&n);
fill(dp,dp + n + 1,INF);
for(int i = 1;i <= n;i ++)
scanf("%d",&a[i]);
for(int i = 1;i <= n;i ++)
{
int tmp = upper_bound(dp + 1, dp + n + 1, a[i]) - dp;
dp[tmp] = a[i];
pos[i] = tmp;
}
cnt = lower_bound(dp + 1,dp + n + 1,INF) - dp - 1;
for(int i = n;i;i --)
{
int j,k;
if(pos[i] == cnt)
{
j = i - 1,k = cnt - 1;
while(k)
{
if(pos[j] == k) k --;
j --;
}
ans = min(ans,i - j - cnt);
}
}
printf("%d %d\n",cnt,ans);
}
return 0;
}
3:Fenwick Tree
点击查看代码
#include <bits/stdc++.h>
using namespace std;
#define int long long
int lowbit(int x)
{
return x & (-x);
}
signed main()
{
int T;
scanf("%lld",&T);
while(T --)
{
int n;
string s;
scanf("%lld",&n);
cin >> s;
vector<int> cnt(n + 1);
for(int i = 1;i <= n;i ++)
if(s[i - 1] == '1')
if(i + lowbit(i) <= n) cnt[i + lowbit(i)] ++;
int sum = 0;
for(int i = 1;i <= n;i ++)
{
if(s[i - 1] == '0' && cnt[i] == 1) sum ++;
else if(s[i - 1] == '1' && cnt[i] == 0) sum ++;
}
printf("%lld\n",sum);
}
return 0;
}