#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[30005];
ll b[100005];
int main(){
ll t;
cin>>t;
while(t--){
ll n;
cin>>n;
memset(b,0,sizeof(b));
for(ll i=1;i<=n;i++){
cin>>a[i];
if(a[i]%3==0) b[0]++;
else if(a[i]%3==1) b[1]++;
else b[2]++;
}
int num=0;
for(int i=0;i+1<=100;i++){
if(b[i]>b[i+1]){
int pre=b[i];
int x=(b[i]+b[i+1]);
if(x%2==0) b[i+1]=x/2;
else b[i+1]=x/2+1;
b[i]=x-b[i+1];
num+=abs(pre-b[i]);
}
b[i+3]=b[i];
}
cout<<num<<endl;
}
return 0;
}
C. Sum of Cubes
用map从1到1e4打表,然后for循环判断即可
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll,ll> mp;
int main(){
//把1-10^12里面的立方求出来
for(ll i=1;i<=10000;i++) mp[i*i*i]=1;
ll t;
cin>>t;
while(t--){
ll x;
cin>>x;
bool flag=false;
for(ll i=1;;i++){
if(x-i*i*i<=0) break;
if(mp[x-i*i*i]==1){
flag=true;
cout<<"YES"<<endl;
break;
}
}
if(!flag) cout<<"NO"<<endl;
}
return 0;
}