K-QAQ (hit list)

Topic links: http://acm.csust.edu.cn/problem/3033

Description

 

Defined , j to 1, i.e. i th exclusive OR and

Seeking value

 

Input

 

Row of three integers K , X , Y

1k1e5

1xy1e9

 

Output

 

An integer that represents the answer

 

Sample Input 1 

1 2 7

Sample Output 1

6

This problem looks complicated, but to make a table you will find is a problem ... because the formula (i% 2) exists, so we only need to calculate i is odd enough. So make the odd table you will find:

Then the problem is over.

Certify as follows:

Note f (x, y) of the exclusive-OR value of all integers from x to y. For f (2 ^ k, 2 ^ (k + 1) -1) (note article ^ represents the "power", xor means "exclusive or", or represents "or"): 2 ^ k 2 ^ (k + 1) -1 this number k ^ 2, the most significant bit (bit + k) is 1, the number 2 ^ k, if k> = 1, then 2 ^ k is an even number, the number of these 2 ^ k the most significant bit (+ k bits) is removed, the exclusive oR value remains unchanged.

Thus f (2 ^ k, 2 ^ (k + 1) -1) = f (2 ^ k - 2 ^ k, 2 ^ (k + 1) -1 -2 ^ k) = f (0, 2 ^ k -1)

Thus f (0, 2 ^ (k + 1) -1) = f (0, 2 ^ k -1) xor f (2 ^ k, 2 ^ (k + 1) -1) = 0 (k> = 1 ) i.e., f (0, 2 ^ k - 1) = 0 (k> = 2)

For f (0, n) (n> = 4) Let n be the highest bit 1 is + k bits (k> = 2), f (0, n) = f (0, 2 ^ k - 1) xor f (2 ^ k, n) = f (2 ^ k, n) of 2 ^ k n that the number n + 1-2 ^ k, the highest level (+ k bits) total m = n + 1-2 ^ k a 1, removing the most significant bit is an odd number when n is 1, m is an even number,

Thus f (0, n) = f (2 ^ k, n) = f (0, n - 2 ^ k) Since the n - 2 ^ k n with the same parity, the above recurrence formula can be obtained:

f(0, n) = f(0, n % 4)

When n% 4 == 1 when, f (0, n) = f (0, 1) = 1

When n% 4 == 3, f (0, n) = f (0, 3) = 0

The following is a list of programs to fight and play table results:

#include <bits/stdc++.h>
using namespace std;

int work(int i,int j)
{
    int ans=0;
    for (int k=1; k<=pow(i,j); k++)
        ans^=k;
    return ans;
}

void en(int i,int j)
{
    printf ("f(%d,%d)=%d\n",i,j,work(i,j));
    
}

int main()
{
    int n;
    n=11;
    int ans=0;
    for (int i=1; i<=n; i+=2){
        for (int j=1; j<=5; j++){
            en(i,j);
        }
        printf ("\n");
    }
    return 0;
}

/*
f(1,1)=1
f(1,2)=1
f(1,3)=1
f(1,4)=1
f(1,5)=1

f(3,1)=0
f(3,2)=1
f(3,3)=0
f(3,4)=1
f(3,5)=0

f(5,1)=1
f(5,2)=1
f(5,3)=1
f(5,4)=1
f(5,5)=1

f(7,1)=0
f(7,2)=1
f(7,3)=0
f(7,4)=1
f(7,5)=0

f(9,1)=1
f(9,2)=1
f(9,3)=1
f(9,4)=1
f(9,5)=1

f(11,1)=0
f(11,2)=1
f(11,3)=0
f(11,4)=1
f(11,5)=0
*/
View Code

The following is the AC codes:

#include <bits/stdc++.h>
using namespace std;

const int mod=1e9+7;

int main()
{
    int n,x,y;
    scanf ("%d%d%d",&n,&x,&y);
    int ans=0;
    int mark=0;
    for (int i=1; i<=n; i+=2){
        if (!mark) ans=(ans+y-x+1)%mod;
        else ans=(ans+y/2-(x-1)/2)%mod;
        mark^=1;
    }
    printf ("%d\n",ans);
    
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/lonely-wind-/p/12003956.html
Recommended