题解:P13830 【MX-X18-T2】「FAOI-R6」二进制与一 III(bit)

· · 题解

Solution 【MX-X18-T2】

这题有很多档分,我们来分析一下。

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxn=1e6+5;
int maxi=0;
int n;
int popcount(int x){
    int cnt=0;
    while(x){
        cnt+=(x%2==1);
        x/=2;
    }
    return cnt;
} 
void work(){
    cin>>n;
    if(n>7){
        int tt=popcount(7)*(n/7);
        int tt2=popcount(3)*(n/3);
        if(n%7>=2) tt+=popcount(n%7);
        if(n%3>=2) tt2+=popcount(n%3);
        cout<<max(tt,tt2)<<"\n";
        return ;
    }
    else{
        int tt2=popcount(3)*(n/3);
        if(n%3>=2) tt2+=popcount(n%3);
        cout<<tt2<<"\n";
    }
    return ;
}
signed main(){
    int t;
    cin>>t;
    while(t--){
        work();
    }
    return 0;
}