zoukankan      html  css  js  c++  java
  • 2019 ICPC Asia Xuzhou Regional A. Cat(异或和性质)

    Yen-Jen loves cat very much.

    Now, there are 10^{18}1018 cats standing in a line, the i^{th}ith cat's cost value c_ic**i is equal to ii, the i^{th}ith cat's index is also equal to ii.

    Now, Yen-Jen wants to buy some cats with continuous index, but he only has SS dollars. He wants to buy some cats with continuous indices. In order to buy cat with index x, x + 1, cdots, y - 1, yx,x+1,⋯,y−1,y, he needs to spend x oplus (x + 1) oplus cdots oplus (y - 1) oplus yx⊕(x+1)⊕⋯⊕(y−1)⊕y dollars. oplus⊕ is the bitwise exclusive-or operator.

    Now, he wants to ask you TT questions. In each question, he has SS dollars, and he wants the indices of cats in range [L, R][L,R]. What's the maximum number of cat that Yen-Jen can buy? If he can't buy any cat, please report -1 instead.

    Input

    The first line of the input file contains one integer TT denotes the number of questions that Yen-Jen wants to challenge you.

    Then, in the next TT lines, each line contains one question. In each line, there are three integers L, R, SL,R,S, which means that Yen-Jen has SS dollars, and he wants to buy cats whose indices are in the range [L, R][L,R].

    • 1 le T le 5 imes 10^51≤T≤5×105
    • 1 le L le R le 10^{18}1≤LR≤1018
    • 0 le S le 2 imes 10^{18}0≤S≤2×1018

    Output

    In each question, output the maximum number of cats that Yen-Jen can buy. If Yen-Jen can't buy any cats, output -1 instead.

    样例输入复制

    2
    1 1 0
    2 2 2
    

    样例输出复制

    -1
    1
    

    由于连续四个偶数奇数异或起来为0,因此只需要暴力([l, l+3])以及([r-3, r])这两部分然后对答案求最大值即可。以及l到r的异或和其实可以(O(1))计算。

    #include <bits/stdc++.h>
    #define ll long long
    using namespace std;
    long long getXor(long long start, long long x) {//获得从start开始到x的异或和
    	if(!(start & 1)) {
    		if(x & 1) {
    			if(((x - 1) / 2) & 1) return 0;
    			else return 1;
    		} else {
    			if(!((x / 2) & 1)) return x + 1;
    			else return x;
    		}
    	} else {//start为偶数有规律 为奇数的话相当于为偶数的情况再异或以下start
    		if(x & 1) {
    			if(((x - 1) / 2) & 1) return 0 ^ start;
    			else return 1 ^ start;
    		} else {
    			if((x / 2) & 1) return (x + 1) ^ start;
    			else return x ^ start;
    		}
    	}
    }
    int solve(int l,int r)
    {
        int res=0;
        if(r-l+1<=10){
            for(int i=l;i<=r;i++) res^=i;
            return res;
        }
        else {
            while(l%4!=0){
                res^=l;
                l++;
            }
            while(r%4!=3){
                res^=r;
                r--;
            }
            return res;
        }
    }
    int main() {
    
    	int t;
    	cin >> t;
    	while(t--) {
    		ll l, r, s;
    		scanf("%lld%lld%lld", &l, &r, &s);
    		ll ans = 0;
    		ans = -1;
    		for(ll nx = l; nx <= l + 3; nx++) {
    			for(ll ny = r - 3; ny <= r; ny++) {
    				if(nx > ny) continue;
    				ll tmp = (getXor(1, ny) ^ getXor(1, nx - 1));
    				if(s >= tmp) ans = max(ans, ny - nx + 1);
    
    			}
    		}
    		if(ans != 0) printf("%lld
    ", ans);
    		else printf("%-1
    ", ans);
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    vs错误集合及解决方案
    使用内存映射文件进行EXE、DLL通信(非MFC)
    visual studio使用小技巧(以vs2012为例)
    GetModuleHandle(NULL)获取当前DLL模块基址?
    格式化输出中的%s和%S的陷阱
    关于字符编码
    远程附加调试服务的方法
    结构体内包含位段,其数据内存分布
    微信个人公众号开发-java
    Docker-常用基建的安装与部署
  • 原文地址:https://www.cnblogs.com/lipoicyclic/p/14757066.html
Copyright © 2011-2022 走看看