HDU4768:Flyer(二分)
来源:互联网 发布:木枪托在淘宝怎么搜索 编辑:程序博客网 时间:2024/06/03 21:40
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
21 10 12 10 145 20 76 14 35 9 17 21 12
Sample Output
1 18 1
题意:n个社团派发传单,有a,b,c三个参数,派发的规则是,派发给序号为a,a+c....a+k*c,序号要求是小于等于b
这其中,有一个学生只收到了奇数传单,要求找出这个学生的编号与得到的传单数目
思路:如果使用异或运算,也还是比较简单的,但是这样的话所花费的时间就比较长,正确的做法是使用二分
使用二分来划分区间,由于是每个社团得到的序列都是等差数列,所以我们很容易能得到区间派发的传单数
如果是奇数,那么所求的人肯定在左区间,否则在右区间,这样二分下去找到答案
二分代码:
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;#define ll __int64#define L 20005ll a[L],b[L],c[L],l,r,n;ll solve(ll mid){ ll k,sum = 0; int i; for(i = 0; i<n; i++) { k = min(mid,b[i]); if(k>=a[i]) sum+=(k-a[i])/c[i]+1; } return sum;}int main(){ int i,j; while(~scanf("%d",&n)) { for(i = 0; i<n; i++) scanf("%I64d%I64d%I64d",&a[i],&b[i],&c[i]); l = 0,r = 1LL<<31; ll mid; while(l<r) { mid = (l+r)/2; if(solve(mid)%2) r = mid; else l = mid+1; } if(l == 1LL<<31) printf("DC Qiang is unhappy.\n"); else { while(solve(l)%2==0)l--; printf("%I64d %I64d\n",l,solve(l)-solve(l-1)); } } return 0;}
异或运算
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;#define ll __int64#define L 20005ll a[L],b[L],c[L];int main(){ int i,j,n; while(~scanf("%d",&n)) { for(i = 0; i<n; i++) scanf("%I64d%I64d%I64d",&a[i],&b[i],&c[i]); ll ans = 0,cnt = 0; for(i = 0; i<n; i++) for(j = a[i]; j<=b[i]; j+=c[i]) ans^=j; if(ans) { for(i = 0; i<n; i++) for(j = a[i]; j<=b[i]; j+=c[i]) if(ans == j) cnt++; printf("%I64d %I64d\n",ans,cnt); } else printf("DC Qiang is unhappy.\n"); } return 0;}
0 0
- HDU4768:Flyer(二分)
- Flyer(二分 HDU4768)
- HDU4768 Flyer 二分
- HDU4768:Flyer(二分)
- HDU4768:Flyer(二分)
- HDU4768 Flyer
- HDU4768 Flyer 二分|异或的性质|暴力
- 2013 ACM/ICPC Asia Regional Changchun Online Problem J & hdu4768 Flyer(二分)
- HDU4768(二分)
- hdu4768 二分
- hdu4768(二分)
- hdu4768(二分)
- hdu4768 非常规的二分
- hdu 4768 - Flyer(二分)
- HDU 4768 Flyer 二分
- hdu 4768 Flyer (二分)
- 二分-hdu-4768-Flyer
- HDU 4768 Flyer(二分)
- 做RFID卡开门器的一些总结(未完)
- 各种进制的字符串与数值之间的互相转换
- IntServ和DiffServ的优缺点
- 一个程序员如何快速挣到一百万
- Integer to Roman
- HDU4768:Flyer(二分)
- 第三层交换机和路由器的区别
- MongoDB的模糊查询
- CArchive 飞鸽传书 如何实现文件的序列号存取
- c++ 多线程编程 网络编程 fork
- PAT 1001. 害死人不偿命的(3n+1)猜想
- 函数返回引用类型
- 获取itunes app 信息
- 34.笔记