2019 Hang electric multi-school third

2019 Multi-University Training Contest 3

Replenishment link: 2019 Multi-3 Contest The University Training

1002 Blow up the city (HDU-6604)

The meaning of problems

Given \ (n-\) points and \ (m \) have edges directed acyclic graph is given \ (Q \) times inquiry, each interrogation given \ (A \) and \ (B \) , find the number of points, the points meet after deleting \ (a \) and \ (B \) in at least one point does not reach a degree of \ (0 \) point.

answer

Dominator tree / tree topology sort of extinction recent common ancestor

1006 Fansblog (HDU-6608)

The meaning of problems

Given prime \ (\ P (1E ^. 9 \ Leq P \ Leq 1E ^ {14}) \) , try to find less than \ (P \) maximum number of prime \ (\ Q \) , obtains \ (\ Q ! \ MOD \ P \) .

answer

Wilson's Theorem density distribution of prime numbers inverse Miller-Rabin test primes

Wilson's Theorem: if and only if \ (P \) when primes: \ (! (P -. 1) \ equiv -1 \ MOD \ P \)

That \ (! (P-. 1) \ equiv (P-. 1) \ MOD \ P \) , since \ ((P -!! 1 ) = (Q) * (Q + 1) * (Q + 2) * * ... (P -. 1) \) , available \ (Q \ mod \ P = \ frac {(P - 1!)} {(Q + 1) * (Q + 2) * ... * ( P - 1)} \ mod \ P = \ frac {1} {(Q + 1) * (Q + 2) * ... * (P - 2)} \ mod \ P \)

May be used \ (Miller-Rabin \) primes test determined prime number, may be used as trial division.
The spacing between the prime number not exceeding \ (600 \) ( large interval between prime (GAPS BETWEEN Large primes) ), and therefore directly available from \ (P - 1 \) Find \ (Q \) .
Note that the number of large, need to quickly multiply. (WA several hair)

Code:

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;

ll mulmod(ll a, ll b, ll m) {
    ll ans = 0;
    while (b) {
        if (b & 1) ans = (ans + a) % m;
        a = (a << 1) % m;
        b >>= 1;
    }
    return ans % m;
}

ll qmod(ll a, ll b, ll m) {
    if(!b) return 1 % m;
    ll ans = 1;
    while (b) {
        if (b & 1) ans = mulmod(ans, a, m);
        a = mulmod(a, a, m);
        b >>= 1;
    }
    return ans % m;
}

bool Miller_Rabbin(ll n, ll a) {
    ll d = n - 1, s = 0, i;
    while (!(d & 1)) {
        d >>= 1;
        s++;
    }
    ll t = qmod(a, d, n);
    if (t == 1 || t == -1)
        return 1;
    for (i = 0; i < s; i++) {
        if (t == n - 1)
            return 1;
        t = mulmod(t, t, n);
    }
    return 0;
}

bool is_prime(ll n) {
    ll i, tab[4] = {3, 4, 7, 11};
    for (i = 0; i < 4; i++) {
        if (n == tab[i])
            return 1;
        if (!n % tab[i])
            return 0;
        if (n > tab[i] && !Miller_Rabbin(n, tab[i]))
            return 0;
    }
    return 1;
}

int main() {
    int T;
    scanf("%d", &T);
    while (T--) {
        ll n;
        scanf("%lld", &n);
        for (ll i = n - 1;; --i) {
            if (is_prime(i)) {
                ll ans = 1;
                for (ll j = i + 1; j <= n - 2; ++j) {
                    ans = mulmod(ans, qmod(j, n - 2, n), n);
                }
                ans = (ans + n) % n;
                printf("%lld\n", ans);
                break;
            }
        }
    }
    return 0;
}

1007 Find the answer (HDU-6609)

The meaning of problems

Given \ (n-\) integers \ (W_i (1 \ leq i \ leq n) \) and an integer \ (m \) , for each \ (\ i (1 \ leq i \ leq n) \) , find the minimum number required to delete \ (W_k (. 1 \ Leq K <I) \) , so \ (\ sum_. 1} = {J ^ iW_j \ Leq m \) . Wherein \ (1 \ leq W_i \ leq m (1 \ leq i \ leq n) \)

answer

multiset STL

Fairy practice

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;

multiset<int> s;
ll a[maxn];

int main() {
    int T;
    scanf("%d", &T);
    while(T--) {
        int n; ll m;
        s.clear();
        scanf("%d %lld", &n, &m);
        for(int i = 1; i <= n; ++i) {
            scanf("%lld", &a[i]);
        }
        ll sum = 0;
        int res = 0;
        for(int i = 1; i <= n; ++i) {
            int cnt = 0;
            ll tmp = sum;
            if(tmp + a[i] > m) {
                auto it = s.end();
                while(tmp + a[i] > m) {
                   --it;
                   tmp -= *it;
                   ++cnt;
                }
            }
            printf("%d ",cnt + res);
            s.insert(a[i]);
            sum += a[i];
            auto it = s.end();
            while(sum > m) {
                --it;
                sum -= *it;
                s.erase(s.find(*it));
                ++res;
            }
        }
        printf("\n");
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/wulitaotao/p/11428294.html