程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU4768:Flyer(二分)

HDU4768:Flyer(二分)

編輯:C++入門知識

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
題意:n個社團派發傳單,有a,b,c三個參數,派發的規則是,派發給序號為a,a+c....a+k*c,序號要求是小於等於b 這其中,有一個學生只收到了奇數傳單,要求找出這個學生的編號與得到的傳單數目
思路:如果使用異或運算,也還是比較簡單的,但是這樣的話所花費的時間就比較長,正確的做法是使用二分 使用二分來劃分區間,由於是每個社團得到的序列都是等差數列,所以我們很容易能得到區間派發的傳單數 如果是奇數,那麼所求的人肯定在左區間,否則在右區間,這樣二分下去找到答案
二分代碼:
#include 
#include 
#include 
using namespace std;
#define ll __int64
#define L 20005

ll a[L],b[L],c[L],l,r,n;

ll solve(ll mid)
{
    ll k,sum = 0;
    int i;
    for(i = 0; i=a[i])
            sum+=(k-a[i])/c[i]+1;
    }
    return sum;
}

int main()
{
    int i,j;
    while(~scanf("%d",&n))
    {
        for(i = 0; i
異或運算
#include 
#include 
#include 
using namespace std;
#define ll __int64
#define L 20005

ll a[L],b[L],c[L];

int main()
{
    int i,j,n;
    while(~scanf("%d",&n))
    {
        for(i = 0; i

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved