Flyer
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3094 Accepted Submission(s): 1150
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个三元组(ai,bi,ci),那么对于y = ci*x
+ ai(ai <= y <= bi,x >= 0),求所有三元组产生的y中,出现次数为奇数的数以及出现次数。数据保证最多存在一个数出现奇数次。如果没有数出现奇数次,输出一句话。
思路:显然该方程是一个等差数列,又保证最多存在一个奇数,所以该奇数左边的传单总数一定是偶数,因此可以二分y值,找到该奇数所在的位置,再计算他收到几张传单即可。
# include <stdio.h> # include <math.h> # include <algorithm> using namespace std; typedef unsigned long long LL; const int MAXN = 20000; LL a[MAXN+1], b[MAXN+1], c[MAXN+1]; int n; bool fun(LL num) { LL sum = 0, imin; for(int i=1; i<=n; ++i) { imin = min(b[i], num); if(imin >= a[i]) sum += (imin-a[i])/c[i] + 1; } return sum&1; } LL fun2(LL num) { LL ans = 0; for(int i=1; i<=n; ++i) if(num >= a[i] && num<=b[i] && (num-a[i])%c[i]==0) ++ans; return ans; } int main() { while(~scanf("%d",&n)) { for(int i=1; i<=n; ++i) scanf("%llu%llu%llu",&a[i],&b[i],&c[i]); LL l=1, r=(LL)1<<31, mid; while(l<r) { mid = (l+r)>>1; if(!fun(mid)) l = mid+1; else r = mid; } if(l == (LL)1<<31) puts("DC Qiang is unhappy."); else printf("%llu %llu\n",r, fun2(r)); } return 0; }