交大OJ 2113题


Time Limit:2800MS  Memory Limit:3000K


哎,这题太神了,各种卡内存和时间。。。

数据太多,输入要用 getchar()

//#include <iostream>
#include<cstdio>
#include<cstring>
#define M 2000000
//using namespace std;
bool f[M];   // 所能开的最大数组

int main()
{
    int n;
    unsigned long long x;
    char ch;
    while(scanf("%d",&n)==1)
    {
        memset(f,false,sizeof(f));  // 如果手动归 0 ,会超时!

        for(int i=0;i<n;i++)
        {
            x=0;
            while((ch=getchar())==' ' || ch=='\n');
            x=ch-'0';

            while( (ch=getchar())>='0' && ch<='9')
                x=x*10+ch-'0';

             if(f[x%M]){
                printf("%llu\n",x);
             }else f[x%M]=true;
        }

      //  puts("cao");
    }

    return 0;
}<strong>
</strong>

方法二:

我们可以在输入的过程中得到3个数,即min(最小的数),max(最大的数),XOR(所有数的异或值),显然答案等于 XOR ^ [min ^ (min + 1) ^ ... ^ max],这里 ^ 表示异或运算。此时问题变成了需要求区间[min, max]所有的数的异或值,这是OJ上的一道陈题(链接),这里简单说下一种求解区间[a, b]所有的数的异或值的解法:

假设x是偶数,那么x ^ (x+1) 就等于1,也即x和(x+1)只有最低位不同。好了,我们会发现区间[a, b]是由大量的这样的x和x+1构成(除了边界情况)。

所以我们只需要从a和b的奇偶性入手即可,这里只考虑其中一种情况,即a是奇数,b是偶数,那么我们总可以把区间[a, b]划分为:

a, (a+1, a+2), (a+3, a+4), ..., (b-2, b - 1), b

显然中间有括号的两个数的异或值为1,假设这样的配对的数有y个,那么答案就是a ^ (y & 1) ^ b,不是吗?(其他情况可以参考着来。)


代码如下:

//#define SPJ
#ifdef SPJ
#include <bits/stdc++.h>
#else
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#endif
#define dbg(x) cout << #x << " = " << x << endl
#define dbg2(x,y) cout << #x << " = " << x << ", " << #y << " = " << y << endl
#define dbg3(x,y,z) cout << #x << " = " << x << ", " << #y << " = " << y << ", " << #z << " = " << z << endl
#define out(x) cout << (x) << endl
#define out2(x,y) cout << (x) << " " << (y) << endl

unsigned long long gao(unsigned long long l, unsigned long long r)
{
    if( l == r )
        return l;
    unsigned long long res = 0;
    if( l & 1LL ) {
        res ^= l;
        l ++;
    }
    if( !(r & 1LL) ) {
        res ^= r;
        r --;
    }
    if( l > r )
        return res;
    unsigned long long last = ((r - l + 1LL) >> 1) & 1LL;
    return (res ^ last);
}

int main()
{
    //freopen("data.in", "r", stdin);
    //freopen("data.out", "w", stdout);
    int n, i;
    unsigned long long mi, ma, res, ans;
    unsigned long long val;
    char ch;
    while( scanf("%d", &n) == 1 ) {
        //scanf("%llu", &val);
        
        while( (ch = getchar()) == ' ' || ch == '\n' ) ;
        val = ch - '0';
        while( (ch = getchar()) >= '0' && ch <= '9' )
            val = val * 10 + (ch - '0');
        ans = mi = ma = val;
        for(i = 1; i < n; i ++) {
            //scanf("%llu", &val);
            
            while( (ch = getchar()) == ' ' || ch == '\n' ) ;
            val = ch - '0';
            while( (ch = getchar()) >= '0' && ch <= '9' )
                val = val * 10 + (ch - '0');
            if( mi > val )
                mi = val;
            if( ma < val )
                ma = val;
            ans ^= val;
        }
        res = gao(mi, ma);
        //dbg3(mi, ma, res);
        printf("%llu\n", (res ^ ans));
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值