首页 > ACM题库 > HDU-杭电 > hdu 3983 Harry Potter and the Binary Kingdom待解决[解题报告]C++
2015
04-14

hdu 3983 Harry Potter and the Binary Kingdom待解决[解题报告]C++

Harry Potter and the Binary Kingdom

问题描述 :

After defeating the Voldemort, Harry is enjoying his senior year in Hogwarts with his friends. They decide to make a graduation trip, using their newest magical broom. In the journey, Harry is trapped in a Kingdom named Binary Kingdom, maybe because the smart but isolated people still have no idea of the great reputation of this young wizard. Loving numbers and binary very much, the subjects of Binary Kingdom claim only Harry solve the problem can he leave there.
Harry Potter and J.K.Rowling

The problem begins with an empty number multi-set, with six kinds of operation:

1. INS M: insert M into the set
2. DEL M: delete all numbers equal to M if any one exists; otherwise report “Del error”
3. ADD M: increase the value of every number by M
4. SUB M: decrease the value of every number by M if after decreasing no number will be less than zero; otherwise report “Sub error”
5. QNUM M: query how many numbers equal to M in the set
6. QBIT M: query how many numbers whose M-th bit is 1 in its binary, M is counting from lower to higher, beginning with 0

Though our brave Harry Potter can proficient in many fields, he is not good at math, now, it’s your time.

输入:

The first line contains a single integer T, indicating the number of test cases.
Each test case begins with an integer M, following M operations.

Technical Specification

1. 1 <= T <= 12
2. 1 <= M <= 100 000
3. The QBIT operations’ M will in the range [0, 17], the other five operations’ M in the range [0, 1 000 000 000].

输出:

The first line contains a single integer T, indicating the number of test cases.
Each test case begins with an integer M, following M operations.

Technical Specification

1. 1 <= T <= 12
2. 1 <= M <= 100 000
3. The QBIT operations’ M will in the range [0, 17], the other five operations’ M in the range [0, 1 000 000 000].

样例输入:

1
10
INS 1
INS 2
DEL 1
INS 3
DEL 1
ADD 1
SUB 4
QNUM 2
QBIT 1
QBIT 2

样例输出:

Case 1:
Del error
Sub error
0
1
1


  1. 漂亮。佩服。
    P.S. unsigned 应该去掉。换行符是n 不是/n
    还可以稍微优化一下,
    int main() {
    int m,n,ai,aj,bi,bj,ak,bk;
    while (scanf("%d%d",&m,&n)!=EOF) {
    ai = sqrt(m-1);
    bi = sqrt(n-1);
    aj = (m-ai*ai-1)>>1;
    bj = (n-bi*bi-1)>>1;
    ak = ((ai+1)*(ai+1)-m)>>1;
    bk = ((bi+1)*(bi+1)-n)>>1;
    printf("%dn",abs(ai-bi)+abs(aj-bj)+abs(ak-bk));
    }
    }