рдЕрдВрдХ # 31: рдЖрдИрдЯреА рдкреНрд░рд╢рд┐рдХреНрд╖рдг - рд╡рд░реНрддрдорд╛рди рдореБрджреНрджреЛрдВ рдФрд░ рдкреНрд░рдореБрдЦ рдХрдВрдкрдирд┐рдпреЛрдВ рд╕реЗ рдЪреБрдиреМрддрд┐рдпрд╛рдВ

рд╣рд╛рдп рд╣рд╛рдп! рд╣рдордиреЗ рдлрд┐рд░ рд╕реЗ рдЖрдкрдХреЗ рд▓рд┐рдП рдкреНрд░рдореБрдЦ рдЖрдИрдЯреА рдХрдВрдкрдирд┐рдпреЛрдВ рдХреЗ рд╕рд╛рдХреНрд╖рд╛рддреНрдХрд╛рд░реЛрдВ рд╕реЗ рджрд┐рд▓рдЪрд╕реНрдк рд╕рд╡рд╛рд▓реЛрдВ рдФрд░ рдХрд╛рд░реНрдпреЛрдВ рдХреЗ рдЪрдпрди рдХреЗ рд▓рд┐рдП рддреИрдпрд╛рд░ рдХрд┐рдпрд╛! рд╡реИрд╕реЗ, рдкрд┐рдЫрд▓реЗ рдЕрдВрдХ рд╕реЗ рд╕рдорд╕реНрдпрд╛рдУрдВ рдХреЗ рдЙрддреНрддрд░ рдкрд╣рд▓реЗ рд╣реА рдкреНрд░рдХрд╛рд╢рд┐рдд рд╣реЛ рдЪреБрдХреЗ рд╣реИрдВ ред



рдореБрджреНрджреЗ рд╣рд░ рд╣рдлреНрддреЗ рджрд┐рдЦрд╛рдИ рджреЗрдВрдЧреЗ - рджреЗрдЦрддреЗ рд░рд╣реЗрдВ! рдХреЙрд▓рдо рднрд░реНрддреА рдПрдЬреЗрдВрд╕реА рд╕реНрдкрд╛рдЗрд╕ рдЖрдИрдЯреА рджреНрд╡рд╛рд░рд╛ рд╕рдорд░реНрдерд┐рдд рд╣реИ ред

рдЗрд╕ рд╕рдкреНрддрд╛рд╣ рд╣рдордиреЗ рдиреАрджрд░рд▓реИрдВрдб рдХреА рдХрдВрдкрдиреА рдлрд┐рд▓рд┐рдкреНрд╕ рдХреЗ рд╕рд╛рде рд╕рд╛рдХреНрд╖рд╛рддреНрдХрд╛рд░ рд╕реЗ рдХрд╛рд░реНрдп рдПрдХрддреНрд░ рдХрд┐рдПред

рдкреНрд░рд╢рди


1. рдЬрд╣рд░ рдФрд░ рдЪреВрд╣рд╛
1000 рд╢рд░рд╛рдм рдХреА рдмреЛрддрд▓реЗрдВ рд╣реИрдВред рдмреЛрддрд▓реЛрдВ рдореЗрдВ рд╕реЗ рдПрдХ рдореЗрдВ рдЬрд╣рд░реАрд▓реА рд╢рд░рд╛рдм рд╣реЛрддреА рд╣реИред рдЬрд╣рд░реАрд▓реА рд╢рд░рд╛рдм рдкреАрдиреЗ рдХреЗ рдПрдХ рдШрдВрдЯреЗ рдмрд╛рдж рдПрдХ рдЪреВрд╣рд╛ рдорд░ рдЬрд╛рддрд╛ рд╣реИред рдпрд╣ рдкрддрд╛ рд▓рдЧрд╛рдиреЗ рдХреЗ рд▓рд┐рдП рдХрд┐ рдХрд┐рддрдиреЗ рдмреЛрддрд▓ рдореЗрдВ рдХрд┐рддрдиреЗ рдиреНрдпреВрдирддрдо рдЪреВрд╣реЛрдВ рдХреА рдЬрд░реВрд░рдд рд╣реИред

рд╕реНрдерд╛рдирд╛рдВрддрд░рдг
1000 . . , . , , .

2. 5 рд╕рдореБрджреНрд░реА рдбрд╛рдХреВ рдФрд░ 100 рд╕реНрд╡рд░реНрдг рд╕рд┐рдХреНрдХреЗ
There are 5 pirates, they must decide how to distribute 100 gold coins among them. The pirates have seniority levels, the senior-most is A, then B, then C, then D, and finally the junior-most is E.

Rules of distribution are:
  • The most senior pirate proposes a distribution of coins.
  • All pirates vote on whether to accept the distribution.
  • If the distribution is accepted, the coins are disbursed and the game ends.
  • If not, the proposer is thrown and dies, and the next most senior pirate makes a new proposal to begin the system again.
  • In case of a tie vote the proposer can has the casting vote

Rules every pirates follows:
  • Every pirate wants to survive
  • Given survival, each pirate wants to maximize the number of gold coins he receives.

What is the maximum number of coins that pirate A might get?

5 , , 100 . , тАФ A, B, C, D, , , тАФ E.

:
  • . ( )
  • , .
  • , .
  • , , , .
  • .


, :
  • .
  • , , .

?


1. рдмрдбрд╝реА рд╕рдВрдЦреНрдпрд╛ рдХреЗ рддрдереНрдп
рдкреВрд░реНрдгрд╛рдВрдХ рдХреЛ рджреЗрдЦрддреЗ рд╣реБрдП, рдХрд╛рд░реНрдп рд╕рдВрдЦреНрдпрд╛ рдХреА рд╡рд╛рд╕реНрддрд╡рд┐рдХрддрд╛ рдХрд╛ рдкрддрд╛ рд▓рдЧрд╛рдирд╛ рд╣реИред

рдЗрдирдкреБрдЯ: рдЗрдирдкреБрдЯ
рдХреА рдкрд╣рд▓реА рдкрдВрдХреНрддрд┐ рдореЗрдВ рдПрдХ рдкреВрд░реНрдгрд╛рдВрдХ T рд╣реЛрддрд╛ рд╣реИ рдЬреЛ рдкрд░реАрдХреНрд╖рдг рдорд╛рдорд▓реЛрдВ рдХреА рд╕рдВрдЦреНрдпрд╛ рдХреЛ рджрд░реНрд╢рд╛рддрд╛ рд╣реИред
рдкреНрд░рддреНрдпреЗрдХ рдкрд░реАрдХреНрд╖рдг рдорд╛рдорд▓реЗ рдХреА рдкрд╣рд▓реА рдкрдВрдХреНрддрд┐ N рд╣реИ, рдЬрд┐рд╕ рд╕рдВрдЦреНрдпрд╛ рдХрд╛ рднрд╛рдЬреНрдп рдкрд╛рдпрд╛ рдЬрд╛рдирд╛ рд╣реИ

рдЖрдЙрдЯрдкреБрдЯ:
рд╕рдВрдЦреНрдпрд╛ рдХреЗ рднрд╛рдЬреНрдп рдХреЛ рдЕрд▓рдЧ рд▓рд╛рдЗрди рдореЗрдВ рдкреНрд░рд┐рдВрдЯ рдХрд░реЗрдВред

рдмрд╛рдзрд╛рдПрдВ: рдЙрджрд╛рд╣рд░рдг: рдЗрдирдкреБрдЯ рдЖрдЙрдЯрдкреБрдЯ
1 тЙд T тЙд 100
1 тЙд N тЙд 1000




3
5
10
2



120
3628800
2

рд╕реНрдерд╛рдирд╛рдВрддрд░рдг
, , .

:
T, .
тАФ N, ,

:
.

:
1 тЙд T тЙд 100
1 тЙд N тЙд 1000


:

3
5
10
2


:
120
3628800
2

2. рдмрд╛рдЗрдирд░реА рдЯреНрд░реА рдХрд╛ рд╡реНрдпрд╛рд╕
Given a Binary Tree, find diameter of it.
+The diameter of a tree is the number of nodes on the longest path between two leaves in the tree. The diagram below shows two trees each with diameter nine, the leaves that form the ends of a longest path are shaded (note that there is more than one path in each tree of length nine, but no path longer than nine nodes).



Input Format:
First line of input contains the number of test cases T. For each test case, there will be only a single line of input which is a string representing the tree as described below:

1. The values in the string are in the order of level order traversal of the tree where, numbers denotes node values, and a character тАЬNтАЭ denotes рдкреВрд░рд╛ рдмрдЪреНрдЪрд╛ред

2. рдЙрджрд╛рд╣рд░рдг рдХреЗ рд▓рд┐рдП:

рдЙрдкрд░реЛрдХреНрдд рдкреЗрдбрд╝ рдХреЗ рд▓рд┐рдП, рд╕реНрдЯреНрд░рд┐рдВрдЧ рд╣реЛрдЧреА: 1 2 3 рдПрдирдПрди 4 6 рдПрди 5 рдПрдирдПрди 7 рдПрди

рдЖрдЙрдЯрдкреБрдЯ рдкреНрд░рд╛рд░реВрдк:
рдкреНрд░рддреНрдпреЗрдХ рдЯреЗрд╕реНрдЯрдХреЗрд╕ рдХреЗ рд▓рд┐рдП, рдПрдХ рдирдИ рд▓рд╛рдЗрди рдореЗрдВ, рд╡реНрдпрд╛рд╕ рдкреНрд░рд┐рдВрдЯ рдХрд░реЗрдВред

рдЖрдкрдХрд╛ рдХрд╛рд░реНрдп:
рдЖрдкрдХреЛ рдлрд╝рдВрдХреНрд╢рди рд╡реНрдпрд╛рд╕ () рдХреЛ рдкреВрд░рд╛ рдХрд░рдирд╛ рд╣реЛрдЧрд╛ рдЬреЛ рдиреЛрдб рдХреЛ рдкреИрд░рд╛рдореАрдЯрд░ рдХреЗ рд░реВрдк рдореЗрдВ рд▓реЗрддрд╛ рд╣реИ рдФрд░ рд╡реНрдпрд╛рд╕ рдХреЛ рд╡рд╛рдкрд╕ рдХрд░рддрд╛ рд╣реИред

рдмрд╛рдзрд╛рдПрдВ: рдЙрджрд╛рд╣рд░рдг: рдЗрдирдкреБрдЯ: рдЖрдЙрдЯрдкреБрдЯ: рд╕реНрдкрд╖реНрдЯреАрдХрд░рдг: рдЯреЗрд╕реНрдЯрдХреЗрд╕ 1: рдкреЗрдбрд╝ рдХрд╛ рд╡реНрдпрд╛рд╕ 3 рд▓рдВрдмрд╛рдИ рдХрд╛ рд╣реИред рдЯреЗрд╕реНрдЯрдХреЗрд╕ 2: рдкреЗрдбрд╝ рдХрд╛ рд╡реНрдпрд╛рд╕ 4 рд▓рдВрдмрд╛рдИ рдХрд╛ рд╣реИред
1 <= T <= 100
1 <= Number of nodes <= 100
1 <= Data of a node <= 100




2
1 2 3
10 20 30 40 60



3
4









рд╕реНрдерд╛рдирд╛рдВрддрд░рдг
, .
+ тАФ . , , , , ( , , ).

:
T. , , , :

1. , , тАЬNтАЭ .

2. :

: 1 2 3 N N 4 6 N 5 N N 7 N

:
.

:
diameter(), node .

:
1 < = T <= 100
1 < = < = 100
1 < = <= 100


:
:

2
1 2 3
10 20 30 40 60


:
3
4


:
1: :

3.
2: :

4 .

3. рдмреНрд▓реИрдХ рдПрдВрдб рд╡реНрд╣рд╛рдЗрдЯ
How many ways are there to place a black and a white knight on an N * M chessboard such that they do not attack each other? The knights have to be placed on different squares. A knight can move two squares horizontally and one square vertically (L shaped), or two squares vertically and one square horizontally (L shaped). The knights attack each other if one can reach the other in one move.

Input:
The first line contains the number of test cases T. Each of the next T lines contains two integers N and M which is size of matrix.

Output:
For each testcase, print the required answer, i.e, number of possible ways to place knights.

Constraints:
1 <= T <= 100
1 <= N, M <= 105


Example:
Input:

3
2 2
2 3
4 5


Output:
12
26
312

Explanation:
Testcase 1:
We can place a black and a white knight in 12 possible ways such that none of them attracts each other.

N * M , ? . (L- ), (L- ). , .

:
T. T N M, .

:
, .

:
1 < = T <= 100
1 <= N, M < = 105


:
:

3
2 2
2 3
4 5


:
12
26
312


:
1:
12 , .



1
. 10 , . . 10, Log2(1000).

, 1 1000 . , . . 1 , 2 . 5, 7 9 , 42 ( 0000101010) .

2
тАФ 98.
1. , A, B C , D E. , (D (100, 0). , E - , 0.
2. , A B , C, D E . D , (C (99, 0, 1), E C).
3. , A . B, C, D E . , B 1 D. (99, 0, 1, 0)
4. A 3, 1 C 1 E, . - (98, 0, 1, 0, 1).
, B , (B , a ). A 2 , B , .

1
:
using namespace boost::multiprecision;
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        cpp_int n, res = 1;
        cin >> n;
        while (n != 1)
        {
            res = res * n;
            n = n - 1;
        }
        cout << res << "\n";
    }
    return 0;
}

2
int find(Node* root, int &h)
{
    if (root == NULL) return 0;
    int lh = find(root->left, h);
    int rh = find(root->right, h);
    h = max(h, lh + rh + 1);
    return max(lh, rh) + 1;
}
int diameter(Node* root)
{
    int h = -1;
    find(root, h);
    return h;
}

3
#include <iostream>
#include <cstdint>

typedef unsigned __int128 uint128_t;

/**
 * O(1) solution to https://practice.geeksforgeeks.org/problems/black-and-white/0
 */
int main()
{
    unsigned t;
    uint64_t n, m, upper, lower;
    uint128_t res;
    
    std::cin >> t;
    while (t--)
    {
        std::cin >> n >> m;
        res = n*m; // total ways to choose first knight
        res *= (n*m-1); // now total ways to choose both knights
        if (n>1 && m>1)
            res -= 4*(2*n*m-3*n-3*m+4); // remove any collisions
        
        // the rest is just for printing a uint128_t
        upper = (uint64_t)(res/1000000000000000000);
        lower = (uint64_t)(res%1000000000000000000);
        if (upper)
        {
            std::cout << upper;
            uint64_t digitChecker = 100000000000000000;
            while (lower/digitChecker == 0 && digitChecker)
            {
                std::cout << 0;
                digitChecker /= 10;
            }
        }
        std::cout << lower << std::endl;
    }
    
	return 0;
}

All Articles