Blue Bridge Cup Training Plan (Day 1-Bit Operation)

Catalog

Preface:

Problem A: a^b

  Questions:

  Problem B: Wake-up Difficulty Syndrome

  Questions:

Preface:

         Senior three, played for two years, even the second year of the Blue Bridge didn't touch it, feeling painful, and decided to learn the algorithm well before learning. In this flag, next year, Blue Bridge will rush into the second year of the country (Guoyi can't imagine, Gugong.jpg) to sort out the knowledge points that I learned. The main topic is mainly written, there will be no explanation of knowledge, at most there is a way of thinking, each chapter will randomly pick two or three topics (Lazy Cancer.jpg). Update Frequency..... Recently CET, stable update after No. 18.

        Title-brushing Platform: Cowdriver Contest - Advanced Algorithmic Guide ( Portal)

Problem A: a^b

Title Description:

Finds the value of the B power of a modular to p, where 0 < a, b, p < 10 ^9

Enter a description:    

Three integers a,b, and p separated by spaces.

  Output description:

An integer representing the value of a^b mod p.

Example 1:

Input:

2 3 9

Output:

8

  Questions:

/*
b=2^k+2^(k-1)+2^(k-2)...+2^0;(Only if the k-bit of b under binary is 1) 

b&1 Take out the lowest bit 
  1.For 1ans=ans*a%p a=a*a%p>=1 
  2.If 0 a=a*a%p>=1

To ensure that the exponent of a is always 2^i so a = a * a

tips:
a*b%p=(a%p*b%p)%p
*/
#include <iostream>
#define ll long long
 
using namespace std;
 
int main() {
    ll a, b, p, ans;
     
    cin >> a >> b >> p;
    ans = 1%p;
    while(b){
        if(b&1) ans = ans * a % p;   //When the last bit of b is 1, multiply a
        b>>=1;                       //Each time b moves one bit to the right, it means dividing by two
        a=a*a%p;                     //Double a every time
    }
    cout << ans;
    return 0;
}

 Problem B:Wake-up Difficulty Syndrome

  Title Description:


         In the 21st century, many people suffered from a strange disease: the difficult to get up syndrome, whose clinical manifestations are: difficult to get up and poor spirits after getting up. As a young sunny teenager, atm has been fighting the difficult to get up syndrome. By studying the relevant literature, he found the cause of the disease: in the deep Pacific Ocean, a dragon named DRD appeared. It holds the essence of sleep and is able to prolong everyone's sleep at will. Thanks to the activity of the drd, the wake-up difficulties syndrome is getting worse and spreading worldwide at an alarming rate. In order to eradicate the disease completely, atm decided to go to the seafloor and eradicate the dragon.
After a lot of hard work, atm finally arrived where DRD was and was ready to fight it hard and hard. DRD has a very special skill, and his defensive front can use certain operations to change the damage he suffers. Specifically, the drd's defense front consists of a bayonet-fan defense door. Each defense gate consists of an op operation and a parameter, where the operation must be one of OR,XOR,AND, and the parameter must be a non-negative integer. If the attack force is jitter before passing through the defense door, the attack force behind the defense door will become jitter opjit. The final damage to the DRD is the attack that the other party's initial attack power changes from behind all the defense doors in turn.
Due to the limited atm level, his initial attack power can only be an integer between 0 and anthracene (that is, his initial attack power can only be 0, 1,..., anthracene is optional, but it is not limited by anthracene after passing through the defense gate). To save energy, he wants to make the drd suffer the most damage by choosing the right initial attack power. Please help him figure out how much damage his drd can get from one attack.

  Enter a description:

          The first row contains two integers, Th and Th, indicating that the drd has a Th-Fan defense gate and that the initial attack power of the atm is an integer between 0 and Th.
Next, the line represents each defense gate in turn. Each line consists of a string OP and a non-negative integer, separated by a space, with OP in the front and op in the back. OP represents the operation of the defense gate, and op represents the corresponding parameters.

Output description:

Output a row of integers indicating how much damage the drd will receive from an atm attack.

Example 1:  

Input:

3 10
AND 5
OR 6
XOR 7

Output:

1

 tips:

  Questions:

/*
The corresponding location results are only related to the initial values of the corresponding locations, so we make a table where each location is the result of a 0 or 1 operation.
Greed is used after the table is typed, from high position to low position.
If the result of 0 on this bit is 1, then put 0 on this bit, and if only 1 result is 1, judge if the sum exceeds M and add up the answers.
*/
#include<iostream>

#define MAXN 100005
using namespace std;

int N,M,A=0,B=(1<<31)-1,tmp,ans,sum;
char s[5];

int main()
{
    cin >> N >> M;
    for(int i=1;i<=N;i++){
        cin >> s >> tmp;
        if(s[0]=='A')A&=tmp,B&=tmp;
        if(s[0]=='O')A|=tmp,B|=tmp;
        if(s[0]=='X')A^=tmp,B^=tmp;
    }
    for(int i=31;i>=0;i--){
        bool f1=A&(1<<i),f2=B&(1<<i);
        if(!(f1||f2))
            continue;
        if(f1){
            ans+=(1<<i);
            continue;
        }
        if(sum+(1<<i)<=M)
            ans+=(1<<i),sum+=(1<<i); //Only 1 result is 1, let's first judge if the sum is less than M
    }
    cout << ans << endl;
    return 0;
}

Another day of paddling, hhh, good night.  ˘ω˘ ) _

Tags: C++

Posted on Fri, 26 Nov 2021 20:02:30 -0500 by chaking