HDU4768——Flyer

Problem

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+2C_i,…A_i+kC_i (A_i+kC_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


思路一

做这道题时候的想法,因为乍一看最多一个奇数,直接暴力异或就好。

代码一

#include <iostream>
#include <cstring>
using namespace std;
const int N=2e4+5;
long long a[N],b[N],c[N];
int main(){
  ios::sync_with_stdio(false);
  cin.tie(0);
  long long n,res,sum;
  while(cin>>n){
    res=sum=0;
    memset(a,0,sizeof(a));
    memset(b,0,sizeof(b));
    memset(c,0,sizeof(c));
    for(long long i=1;i<=n;i++)cin>>a[i]>>b[i]>>c[i];
    for(long long i=1;i<=n;i++){
      for(long long j=a[i];j<=b[i];j+=c[i])res^=j;
    }
    if(res){
      for(long long i=1;i<=n;i++){
        if(res>=a[i]&&res<=b[i]&&(res-a[i])%c[i]==0)++sum;
      }
      cout<<res<<" "<<sum<<endl;
    }
    else cout<<"DC Qiang is unhappy."<<endl;
  }
  return 0;
}

思路二

代码一通过没问题,不过后来经过学长提醒,这道题正确的思路是二分——所有组中最小编号为下界,最大B为上界,二分条件是所有组从A到mid(二分中点) 得到玩具的个数和为奇数时,则奇数小朋友在low--mid里,否则在mid--high里(如果有的话)。
因为如果mid之前小朋友有一个得到奇数的话,之前所有小朋友得到玩具数一定为奇,否则为偶 (偶+偶得偶,偶+奇得奇数) 。这是因为题目说了有的话只有一个奇数,如果有多个的话奇+奇得奇,就不能这么去做了。

代码二

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 2e4+5;
int n;
long long l[N],r[N],k[N];
long long deal(long long x,long long y,long long z){
    return 1 + (y - x)/z;
}
int got(long long x){
    int cnt = 0;
    for(int i = 1;i <= n;i++)
     if(l[i] <= x && r[i] >= x)
      if(x % k[i] == l[i] % k[i]) cnt++;
    return cnt;
}
long long check(long long mid){
    long long cnt = 0;
    for(int i = 1;i <= n;i++)
     if(l[i] <= mid) cnt += deal(l[i],min(r[i],mid),k[i]);
    return cnt;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    while(cin>>n){
        long long x = 1,y = 1;
        memset(l,0,sizeof(l));
        memset(r,0,sizeof(r));
        memset(k,0,sizeof(k));
        for(int i = 1;i <= n;i++){
            cin>>l[i]>>r[i]>>k[i];
            y = max(y, r[i]);
        }
        while(x != y){
            long long mid = (x+y)>>1;
            if(check(mid) & 1) y = mid;
            else x = mid+1;
        }
        if(check(x) & 1) cout<<x<<" "<<got(x)<<endl;
        else cout<<"DC Qiang is unhappy."<<endl;
    }
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,939评论 0 23
  • 前不久,很长时间不联系的一个兄弟突然发消息给我,告诉我他要结婚了,当时,很惊讶,他怎么会想起结婚? 也许你会惊讶,...
    杨先生i阅读 262评论 0 2
  • 《Redis实战》--读书笔记 时间:2017年10月02日01:13:48 第1章 共有五种结构 ①String...
    MR_ChanHwang阅读 543评论 1 1
  • 今天来跟大家讨论一下何去何从这个主题,从哪里来到从哪里去,一直走一直走发现并不是你想走的那条路,想重新走心里有点胆...
    A羽翼阅读 284评论 0 1
  • 回汉后,一直还没有开始事业,对于我这种双鱼座的人来说,绝对有选择综合症,能做好一件事,真的是奇葩. 今天当了回刺客...
    大熊坤阅读 166评论 1 0