Luo Gu AT249 black tea (Tea) - simulation, two points

Luo Gu AT249 black tea (Tea)

Topic links: Luo Gu AT249 black tea (Tea)

Algorithms 模拟Tags: ,二分

topic

Title Description

One day, \ (kagamiz \) while drinking tea, while trying to answer the following questions:

When the group consisting of a positive integer composed of two positive integers are arranged in such a manner, \ ((m, n-) \) is the number of columns in the first few sets?

\((1,1),(2,1),(1,2),(3,1),(2,2),(1,3),(4,1),(3,2),(2,3),(1,4),(5,1),…\)

This question is too simple for him, so he is more in-depth consideration to the following question:

When said number of columns in the \ (I \) th group \ ((a_i, B_i) \) , the first \ (J \) th group \ ((a_j, b_j) \ ) when, \ ((a_i + a_j, b_i + b_j) \) is the number of columns in the first few sets?

Your task is to help him answer this question.

Input Format

Input line only, two positive integers \ (I, J \) .

Output Format

The output is a positive integer, indicates when the first above-described series of \ (I \) th group \ ((a_i, B_i) \) , the first \ (J \) th group \ ((a_j, b_j) \ ) when , \ ((a_i + a_j, b_i + b_j) \) is the number of columns in the first few sets.

Sample input and output

Input # 1

1 1

Output # 1

5

Input # 2

3 2

Output # 2

13

Input # 3

114 514

Output # 3

1155

Description / Tips

data range:

  • \ (i≥1,1≤j≤10 ^ 8 \) , and \ (i, j \) are positive integers.
  • \ (i, j \) may be equal.

answer:

First we look at this question on the number given by:

\((1,1),(2,1),(1,2),(3,1),(2,2),(1,3),(4,1),(3,2),(2,3),(1,4),(5,1),…\)

We can find, and as \ (n \) of the total number of \ (n - 1 \) right, so the statistics can be interrogated for the first several groups according to the law.

After we continue to observe, in each combined into a \ (n-\) is the number of groups, the first one from \ (n - 1 \) to \ (1 \) decreasing, while the second is just the opposite, from \ (1 \) to \ (n - 1 \) increments, this can be counted as the first few lines (the same number for all and seen as one line) of the first few.

Also due to the scope of the data theme \ (1 ≤ J ≤ 10 ^ 8 \) , obviously we query violence is very limited and the sum of (not measured before), so we chose to use arithmetic sequence summation and half answers the way to solve the specific code to achieve the following:

long long getsum(ll q) // 等差数列求和
{
    return 1ll * ((1 + q) * q) / 2;
}
void find(long long q) // 二分查找答案
{
    ll l = 1, r = N + 1, mid;
    while(l < r)
    {
        mid = (l + r) >> 1;
        if (getsum(mid) >= q)
            r = mid;
        else 
            l = mid + 1;
    }
    yy = q - getsum(l - 1);
    xx = l - yy + 1;
}

AC Code

#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;
const int N = 1e8 + 10;
typedef long long ll;
int xx, yy;
long long getsum(ll q)
{
    return 1ll * ((1 + q) * q) / 2;
}
void find(long long q)
{
    ll l = 1, r = N + 1, mid;
    while(l < r)
    {
        mid = (l + r) >> 1;
        if (getsum(mid) >= q)
            r = mid;
        else 
            l = mid + 1;
    }
    yy = q - getsum(l - 1);
    xx = l - yy + 1;
}
int main()
{
    ll x, y;
    scanf("%lld%lld", &x, &y);
    find(x);
    ll ax = xx, ay = yy;
    find(y);
    ll bx = xx, by = yy;
    ll qx = ax + bx, qy = ay + by;
    ll pos = qx + qy - 1;
    pos = getsum(pos - 1) + qy;
    printf("%lld\n", pos);
    return 0;
}

Guess you like

Origin www.cnblogs.com/littleseven777/p/11845655.html