HDU4768(二分)

G++对太大的数报编译错误。 。

1<<31就已经等于21亿了,表示不开。

long long的话得 1LL<<32这样表示。

 

自己也至少算是写过二分的人。

while(l<r)

{

     int mid = (l+r)/2;

     if()      l = mid +1;

     else   r = mid;

}

这样写才不会漏掉某些边界数。

再一个就是自己判断x,y与a[i],b[i]写的太挫。    别人的程序用一个判断就行。    分析不够好。

还有就是当时没想到可以不用开越大数组存数据。    毕竟你每次二分32次算一下就OK了。  

先是自己的挫代码

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;

typedef __int64 LL;

const int maxn = 21000;

LL a[maxn], b[maxn], c[maxn];
int T, l, r;
LL calc(LL x, LL y)  //所有这些都可以通过判断x,y,a[i],b[i]的太小。。。
{
    LL sum = 0, temp;
    int i;
    for(i = 1;i <= T;i++)
    {
        if(c[i]==0) continue;
        if(x == y)
        {
            if(((x-a[i])%c[i]==0&&x>=a[i]&&y<=b[i]) || x==a[i] ||y==a[i])  //负数取余的情况自己得考虑好。 囧
                sum++;
            continue;
        }
        if(y<a[i]) continue;
        else if(x>b[i]) continue;
        else if(x<=a[i]&&y>=b[i])
        {
            temp = (b[i]-a[i])/c[i] + 1;//起始数得计算在内
            sum += temp;
        }
        else if(x<=a[i]&&y<=b[i])
        {
            temp = (y-a[i])/c[i] + 1;
            sum += temp;
        }
        else if(x>=a[i]&&y>=b[i])
        {
            temp = (b[i]-a[i])/c[i] - (x-a[i])/c[i];
            if((x-a[i])%c[i]==0) sum++;
            sum += temp;
        }
        else if(x>=a[i]&&y<=b[i])
        {
            temp = (y-a[i])/c[i] - (x-a[i])/c[i];
            if((x-a[i])%c[i]==0) sum++;
            sum += temp;
        }
        else if(x==b[i])
        {
            if(b[i]!=a[i] && (b[i]-a[i])%c[i]==0)
                sum++;
        }
    }
    return sum;
}

int main()
{
    while(~scanf("%d",&T))
    {

       for(int i = 1;i <= T;i++)
       {
           scanf("%I64d%I64d%I64d", &a[i], &b[i], &c[i]);
       }

       LL l = 1, r = 1LL<<32;


       int flag = 0;
       while(l<r)
       {
           LL temp = (l+r)/2;
           if(calc(l, temp)%2==1)
           {
               flag = 1;
               r = temp;
           }
           else if(calc(temp+1, r)%2==1)
           {
               flag = 2;
               l = temp + 1;  //这个二分写的还比较OK 要考虑到一些边界问题恰好
           }
           else break;
       }

       if(flag)  printf("%I64d %I64d\n", l, calc(l, l));
       else printf("DC Qiang is unhappy.\n");

    }
    return 0;
}


简单判断r和b[i]的大小即可。

 

还有人用异或做的,没想到速度可以这么快?  难道2^32异或一遍耗时很少?

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值