HDU4768 Flyer 二分

                    Flyer

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2777    Accepted Submission(s): 1038


Problem Description
The new semester begins! Different kinds of student societies are all trying to advertise themselves, by giving flyers to the students for introducing the society. However, due to the fund shortage, the flyers of a society can only be distributed to a part of the students. There are too many, too many students in our university, labeled from 1 to 2^32. And there are totally N student societies, where the i-th society will deliver flyers to the students with label A_i, A_i+C_i,A_i+2*C_i,…A_i+k*C_i (A_i+k*C_i<=B_i, A_i+(k+1)*C_i>B_i). We call a student "unlucky" if he/she gets odd pieces of flyers. Unfortunately, not everyone is lucky. Yet, no worries; there is at most one student who is unlucky. Could you help us find out who the unfortunate dude (if any) is? So that we can comfort him by treating him to a big meal!
 

Input
There are multiple test cases. For each test case, the first line contains a number N (0 < N <= 20000) indicating the number of societies. Then for each of the following N lines, there are three non-negative integers A_i, B_i, C_i (smaller than 2^31, A_i <= B_i) as stated above. Your program should proceed to the end of the file.
 

Output
For each test case, if there is no unlucky student, print "DC Qiang is unhappy." (excluding the quotation mark), in a single line. Otherwise print two integers, i.e., the label of the unlucky student and the number of flyers he/she gets, in a single line.
 

Sample Input
  
  
2 1 10 1 2 10 1 4 5 20 7 6 14 3 5 9 1 7 21 12
 

Sample Output
  
  
1 1 8 1
 

Source



//题意 
n个社团派发传单,有a,b,c三个参数,派发的规则是,派发给序号为a,a+c....a+k*c,序号要求是小于等于b
这其中,有一个学生只收到了奇数传单,要求找出这个学生的编号与得到的传单数目
// 二分原理也许不难  难的是怎么去二分
// 2^31 __int64 不用说
// 突破点就是  只有一个人拿到的是奇数的 二分 成左右区间 mid 算出左边区间 各个社团发的次数总和如果是奇数则肯定在 否者在右边

// 还有一个方法是异或法 如果 0^a^b^a^b^c=c;
// 如果只有一个数异或了一次最后得数就是这个人了 如果最后为0 则没有

#include<stdio.h>
#include<algorithm>
using namespace std;
#define L 20005
#define ll __int64
ll n;
ll a[L],b[L],c[L],low,high;
ll binaryg(ll mid)           //函数返回值 保持一致
{
    ll sum=0,k;
    for(int i=0; i<n; i++)
    {
        k = min(mid,b[i]);
        if(k>=a[i])
            sum+=(k-a[i])/c[i]+1;
    }
    return sum;          //sum是ll型
}

int main()
{
    int i;
    while(~scanf("%d",&n))
    {
        for(i = 0; i<n; i++)
            scanf("%I64d%I64d%I64d",&a[i],&b[i],&c[i]);
        low=0,high = 1LL<<32; //注意要把1变成ll型 不然直接炸了 系统默认int
        ll mid;
        while(low<high)
        {
            mid = (low+high)>>1;
            if(binaryg(mid)&1) high= mid;
            else low= mid+1;
        }
        if(low == 1LL<<32)
            printf("DC Qiang is unhappy.\n");
        else
        {
            while(binaryg(low)%2==0) low--;//注意细节 最后只剩两个数肯定是low+1结束,所以那个数可能就是low
            // 或者比low小1;
            printf("%I64d %I64d\n",low,binaryg(low)-binaryg(low-1));
        }
    }

    return 0;
}

如果一个程序员不细心严谨,就是对自己残忍.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值