2022 Nioke winter vacation algorithm basic training camp 2, Xiaosha's counting (greedy, bit operation)

portal

insert image description here
Ideas:

Since there is more than one one in the case of binary splitting and the last parity, then the contribution after XOR must be less than our cost, so we have to ensure that the number of each bit is either 0 or 1, so that we can guarantee a [ ∧ ] = a [ + ] a\left[\wedge\right] =a\left[ +\right]a[]=a[+](因为 a + b = 2 ∗ ( a & b ) + a ∧ b ≥ a ∧ b a+b=2\ast \left( a\& b\right) +a\wedge b\geq a\wedge b a+b=2(a&b)+abab,所以 a [ ∧ ] = m a\left[\wedge\right] =m a[]=m is the optimal case)

Then we found that for each of them, they do not interfere with each other, then the situation they may produce is nnn types, so we only need to ask for mmunder binaryHow many 1s does m have, then findnxn^xnx is enough , and the time complexity isO ( log ⁡ 2 m ) O\left( \log _{2}m\right)O(log2m)

Code:

#include<iostream>
using namespace std;
const int mod = 1e9+7;
int main(){
    
    
    long long n, m, ans = 1;
    cin >> n >> m;
    n %= mod;
    while(m){
    
    
        if(m&1)	ans = ans * n % mod;
        m >>= 1;
    }
    cout << ans << endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_45550375/article/details/122734664