zoukankan      html  css  js  c++  java
  • 数学--数论--HDU 5223

    Describtion
    In mathematics, the greatest common divisor (gcd) of two or more integers, when at least one of them is not zero, is the largest positive integer that divides the numbers without a remainder. For example, the GCD of 8 and 12 is 4.—Wikipedia

    BrotherK and Ery like playing mathematic games. Today, they are playing a game with GCD.

    BrotherK has an array A with N elements: A1 ~ AN, each element is a integer in [1, 10^9]. Ery has Q questions, the i-th question is to calculate
    GCD(ALi, ALi+1, ALi+2, …, ARi), and BrotherK will tell her the answer.

    BrotherK feels tired after he has answered Q questions, so Ery can only play with herself, but she don’t know any elements in array A. Fortunately, Ery remembered all her questions and BrotherK’s answer, now she wants to recovery the array A.
    Input
    The first line contains a single integer T, indicating the number of test cases.

    Each test case begins with two integers N, Q, indicating the number of array A, and the number of Ery’s questions. Following Q lines, each line contains three integers Li, Ri and Ansi, describing the question and BrotherK’s answer.

    T is about 10

    2 ≤ N Q ≤ 1000

    1 ≤ Li < Ri ≤ N

    1 ≤ Ansi ≤ 109
    Output
    For each test, print one line.

    If Ery can’t find any array satisfy all her question and BrotherK’s answer, print “Stupid BrotherK!” (without quotation marks). Otherwise, print N integer, i-th integer is Ai.

    If there are many solutions, you should print the one with minimal sum of elements. If there are still many solutions, print any of them.
    Sample Input
    2
    2 2
    1 2 1
    1 2 2
    2 1
    1 2 2
    Sample Output
    Stupid BrotherK!
    2 2

    由于区间长度只有1000,所以暴力枚举,完事了,最后在检查一编完事。

    #include <bits/stdc++.h>
    using namespace std;
    const int N = 1010;
    long long  n, q;
    
    long long  num[N], l[N], r[N], s[N];
    
    long long  gcd(long long  a, long long  b)
    {
        if (b == 0)
        {
            return a;
        }
        else
        {
            return gcd(b, a % b);
        }
    }
    
    int main()
    {
        int t;
        scanf("%d", &t);
        while (t--)
        {
            cin >> n >> q;
            for (int i = 0; i < N; ++i)
            {
                num[i] = 1;
            }
            for (int i = 0; i < q; ++i)
            {
                cin >> l[i] >> r[i] >> s[i];
                for (int j = l[i]; j <= r[i]; ++j)
                {
                    num[j] = (num[j] * s[i]) / gcd(num[j], s[i]);
                }
            }
            bool flag = true;
            for (int i = 0; i < q; i++)
            {
                long long  ans = num[l[i]];
                for (int j = l[i] + 1; j <= r[i]; j++)
                {
                    ans = gcd(ans, num[j]);
                }
                if (ans != s[i])
                {
                    flag = false;
                    break;
                }
            }
            if (flag)
            {
                for (int i = 1; i <n; i++)
                {
                    cout << num[i]<<" ";
                }
                cout<<num[n]<<endl;
            }
            else
            {
                printf("Stupid BrotherK!
    ");
            }
        }
        return 0;
    }
    
  • 相关阅读:
    HDU 5828 Rikka with Sequence (线段树+剪枝优化)
    Educational Codeforces Round 5 E. Sum of Remainders (思维题)
    HDU 2256 Problem of Precision (矩阵快速幂)
    Codeforces 597C. Subsequences (树状数组+dp)
    Codeforces Round #292 (Div. 1) B. Drazil and Tiles (类似拓扑)
    HDU 5794 A Simple Chess (Lucas + dp)
    Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)
    Codeforces Round #313 (Div. 2) E. Gerald and Giant Chess (Lucas + dp)
    进程内存空间的分布
    快排,堆排与归并排序
  • 原文地址:https://www.cnblogs.com/lunatic-talent/p/12798484.html
Copyright © 2011-2022 走看看