Flyer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2159 Accepted Submission(s): 792
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
Recommend
二分法,如果[le,ri]区间的总次数为奇数,那么目标在左区间,否则在右区间。
当le==ri时,若存在答案,则答案为le或ri,若不存在则有(cal(le)-cal(le-1) )%2==0 ;
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<cctype>
#include<utility>
#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n) for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n) for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n) for(int i=(n) ;i>=0 ;i--)
#define lson num<<1,le,mid
#define rson num<<1|1,mid+1,ri
#define MID int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)
#define mk make_pair
#define _f first
#define _s second
using namespace std;
//const int INF= ;
typedef long long ll;
//const ll inf =1000000000000000;//1e15;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int INF =0x3f3f3f3f;
const int maxn= 20000+20 ;
//const int maxm= ;
int n;
ll a[maxn],b[maxn],c[maxn];
ll mid;
ll cal(ll x)
{
ll sum=0;
for(int i=1;i<=n;i++)
{
ll p=min(b[i],x);
if(p>=a[i])
sum+=(p-a[i])/c[i]+1;
}
return sum;
}
bool Work()
{
ll le=1;
ll ri=(ll) 1<<31;
while(le<ri)
{
mid=(le+ri)>>1;
if(cal(mid)%2 ==0) le=mid+1;
else ri=mid;
}
mid=(le+ri)>>1;
if(cal(mid)%2==0) return false;
// if(cal(le)%2==0) return false;
// if(cal(ri)%2==0) return false;
return true;
}
int main()
{
while(~scanf("%d",&n) )
{
for(int i=1;i<=n;i++)
{
scanf("%lld%lld%lld",&a[i],&b[i],&c[i]);
}
if(!Work() ) puts("DC Qiang is unhappy.");
else printf("%lld %lld\n",mid,cal(mid)-cal(mid-1));
}
return 0;
}