Title link
A-Great Lucky
Practice: You can perform bit-wise operations, count the number of each bit as 1, the number is odd, the bit contributes the answer, and then move forward.
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=(b);++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define pi pair<int, int>
#define mk make_pair
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
const int N=2e5+10;
ll a[N],n;
int main()
{
cin>>n;
rep(i,1,n) scanf("%lld",&a[i]);
ll pre=0,num=0;
ll ans=0;
for(ll j=0;j<=60;++j){
num=0;
rep(i,1,n)
{
if((a[i]>>j)&1ll)num++;
}
ll s=num*num;
s+=pre;
if(s%2) ans+=(1ll<<j);
pre=s/2;
}
cout<<ans;
}
B-triangle circumference
Method: Enumerate two points, find the Manhattan distance of other points to these two points, x and y are sorted separately, and O (1) can be obtained by using a prefix sum.
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=(b);++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define pi pair<int, int>
#define mk make_pair
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
const int N=1e3+10;
const ll mod=998244353;
ll x[N],y[N],n,pre[N],ans;
void cal(ll x[])
{
// rep(i,1,n)
// {
// printf("i:%d x:%lld\n",i,x[i]);
// }
//sort(x+1,x+1+n);
rep(i,1,n) pre[i]=(pre[i-1]+x[i]);
rep(i,1,n)
for(int j=i+1;j<=n-1;++j){
ans+=(pre[n]-pre[j]-(n-j)*x[i])%mod;
ans+=mod;
ans+=(pre[n]-pre[j]-(n-j)*x[j])%mod;
ans%=mod;
ans+=(n-j)*(x[j]-x[i]);
ans%=mod;
}
}
int main()
{
cin>>n;
rep(i,1,n)
{
scanf("%lld%lld",&x[i],&y[i]);
x[i]+=1e9+10,y[i]+=1e9+10;
}
sort(x+1,x+1+n);
sort(y+1,y+1+n);
cal(x);
cal(y);
cout<<ans<<endl;
}
C-operation highlights
Find subsequences that are essentially different
Idea: Let the number of subsequences of the first i digits be f (i).
1. If the i-th digit has not appeared in the first i-digit, then the original sub-sequence in the i-1 digit is also the sub-sequence of the first i-digit, totaling f (i-1), and in The original sub-sequences of i-1 digits are followed by a [i] is also a new sub-sequence, a total of f (i-1), and then the last digit alone can also form a new sub-sequence, is One, so f (i) = f (i) + f (i) +1 at this time.
2. If the i-th song number has appeared in the previous i number, then f (i) = f (i) + f (i) -f (a [i] last position-1). Because at this time, the case of a single a [i] has been calculated, so there is no +1, and the case of adding a [i] to the last position of a [i] -1 has also been calculated. , To be subtracted once.
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=(b);++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define pi pair<int, int>
#define mk make_pair
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
const int N=1e3+10;
const ll mod=1e9+7;
char s[N];
int n,m;
ll dp[N][N],vis[30],last[N];
void add(ll &x,ll y)
{
x=(x+y)%mod;
}
int get(char c)
{
return c-'a';
}
int main()
{
cin>>n>>m>>s+1;
if(m==0){
printf("1");
return 0;
}
//rep(i,1,n) dp[1][s[i]-'a'
for(int i=1;i<=n;++i){
int id=get(s[i]);
for(int j=1;j<=m&&j<=n;++j){
add(dp[i][j],dp[i-1][j]);
}
for(int j=1;j<=m&&j<=n;++j){
add(dp[i][j+1],dp[i-1][j]);
}
if(last[id]==0) add(dp[i][1],1);
else{
for(int j=2;j<=m&&j<=n;++j){
dp[i][j]=(dp[i][j]-dp[last[id]-1][j-1]+mod)%mod;
}
}
last[id]=i;
}
//ll ans=0;
//rep(i,1,n) printf("i:%d %lld\n",i,dp[i][2]);
cout<<dp[n][m]<<endl;
}
/*
4 2
abab
5 2
abcc
5 3
abccc
*/
D-Slasher Master
Practice: Reference from: Portal
exgcd don't understand?
Learn exgcd reference blog: Portal
easy to understand
#include<bits/stdc++.h> using namespace std; #define ll long long const int mod=998244353; ll exgcd(ll a,ll b,ll &x,ll &y){ if(b==0){x=1;y=0; return a; } ll r=exgcd(b,a%b,x,y); ll temp=x; x=y; y=temp-(a/b)*y; return r; } ll gcd(ll a,ll b){ if(a%b==0)return b; return gcd(b,a%b); } int main(){ ll t,i,j,n,k,m=0,a,b,c; cin>>a>>b>>c>>k; ll g=gcd(b,c); for(i=0;i<k/a;i++){ if((k-a*i)%g!=0)continue; ll x,y; ll cc=exgcd(b,c,x,y); t=(k-a*i)/cc;//乘上t才是目前 b,c的其中一个解 ll x1=x*t,y1=y*t,c1=c/g; //printf("x1:%lld y1:%lld\n",x1,y1); x1=(x1%c1+c1)%c1;//最小正整数解,类似于取模 模数为c1 y1=(k-a*i-b*x1)/c; if(x1<0||y1<0)continue; cout<<i<<" "<<x1<<" "<<y1; break; } }
E-competitive opponent
Method: Combine heuristics, mp [u] [x] is the sum of the node weights of the node depth of x, and num [u] [x] is the number of nodes of the node depth of x.
Here I am missing steps, that is to judge the size of u and v, and then swap, it is the data water,
After the game, I added an additional array dep [i] to represent the offset, record the offset when the i node goes up, and realize the real heuristic merge: the second code
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=(b);++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define pi pair<int, int>
#define mk make_pair
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
const int N=2e5+10;
int n,k,a[N];
vector<int>G[N];
map<int,ll>mp[N],num[N];
ll ans[N];
void dfs(int u,int f)
{
for(int v:G[u])
{
if(v==f) continue;
dfs(v,u);
ll val=0;
for(auto x:mp[v]){
int d=k-x.first-1;
if(d<=0) continue;
if(mp[u][d]==0) continue;
val+=num[u][d]*mp[v][x.first];
val+=num[v][x.first]*mp[u][d];
}
for(auto x:mp[v]){
mp[u][x.first+1]+=x.second;
num[u][x.first+1]+=num[v][x.first];
}
ans[u]+=val;
}
num[u][0]++;
mp[u][0]+=a[u];
}
int main()
{
cin>>n>>k;
rep(i,1,n) scanf("%d",&a[i]);
rep(i,2,n)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1,1);
rep(i,1,n)
printf("%lld ",ans[i]);
}
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=(b);++i)
#define mem(a,x) memset(a,x,sizeof(a))
#define pb push_back
#define pi pair<int, int>
#define mk make_pair
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
const int N=2e5+10;
int n,k,a[N];
vector<int>G[N];
map<int,ll>mp[N],num[N];
ll ans[N],dep[N];
void dfs(int u,int f)
{
for(int v:G[u])
{
if(v==f) continue;
dfs(v,u);
ll val=0;
dep[v]++;
if(mp[u].size()<mp[v].size())
{
swap(mp[u],mp[v]);
swap(num[u],num[v]);
swap(dep[u],dep[v]);
}
for(auto x:mp[v]){
int d=k-(x.first+dep[v])-dep[u];
if(mp[u][d]==0) continue;
val+=num[u][d]*mp[v][x.first];
val+=num[v][x.first]*mp[u][d];
}
for(auto x:mp[v]){
mp[u][x.first+dep[v]-dep[u]]+=x.second;
num[u][x.first+dep[v]-dep[u]]+=num[v][x.first];
}
ans[u]+=val;
}
num[u][-dep[u]]++;
mp[u][-dep[u]]+=a[u];
}
int main()
{
cin>>n>>k;
rep(i,1,n) scanf("%d",&a[i]);
rep(i,2,n)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(1,1);
rep(i,1,n)
printf("%lld ",ans[i]);
}