Bestcoder Round #40 A B C

Source: Internet
Author: User

A, water problem, direct enumeration to SQRT

b, each time for each enumeration, if less than the current bit, then the answer can be calculated, add the answer is: Set 3 parts, the former exactly the same part A, the middle of a new selection of B, followed by the whole arrangement of C, so that each part and each two parts can be composed of the number of reverse order, because n only 100, Inside the enumeration is also no problem, mainly in the back of the full array of C reverse logarithm, this can be processed by DP, dp[i] = dp[i-1] * i + I! * SUM (i-1), sum (i) represents the sum of 1 to I.

C: Push formula, C (n, m) = C (n-1, m-1) + C (n-1, m) = C (n-1, m-1) + C (n-2, m-1) + C (n-2, m) .... So for C (i, K), a <= i <= B's sum, is equal to C (b + 1, K + 1)-C (A, K + 1), and then because the P is relatively small, to use Lucas to calculate the number of combinations can be

Code:

A:

#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;int T, n;int Main () {    scanf ("%d", &t);    while (t--) {        scanf ("%d", &n);        int ans = 2000000000LL;        for (int i = 1; I * i <= n; i++) {            if (n% i = = 0) {                ans = min (ans, (n/i + i) * 2);            }        }        printf ("%d\n", ans);    }    return 0;}

B:

#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;typedef long ll; const int N = 105;const int MOD = 1000000007;int N, a[n], vis[n], dp[n], fac[n], sum[n];int tot;int dfs (int u) {if (U    = = N) return 0;    int ans = 0;    int cnt = 0;        for (int i = 1; i < a[u]; i++) {if (vis[i]) continue;        Ans = (ans + (LL) tot * fac[n-u-1]% mod)% MoD;        if (n-u-1 > 0) ans = (ans + (LL) CNT * (n-u-1)% mod * fac[n-u-2]% mod)% MoD;        Ans = (ans + dp[n-u-1])% MOD;        int tmp = 0;        for (int j = u-1; J >= 0; j--) {if (A[j] > i) tmp++;        } int sb = 0;        int SBB = 0;            for (int j = 1; J <= N; j + +) {if (j = = i) continue;            if (Vis[j]) SBB + = SB;        else sb++;        } if (N-u-1 > 0) ans = (ans + (LL) SBB * (n-u-1)% mod * fac[n-u-2]% mod)% MoD; Ans = (ans + (LL) tmp * fac[n-u-1]% mod)% MoD;        cnt++;    } Vis[a[u]] = 1;    for (int i = a[u] + 1; I <= n; i++) if (vis[i]) tot++;    Ans = (ans + DFS (U + 1))% MOD; return ans;}    int main () {fac[0] = 1;        for (int i = 1; i < N; i++) {Fac[i] = (LL) fac[i-1] * I% MOD;    Sum[i] = (Sum[i-1] + i)% MOD;    } for (int i = 2; i < N; i++) Dp[i] = ((LL) dp[i-1] * I% mod + (LL) fac[i-1] * sum[i-1]% mod)% MoD;        while (~SCANF ("%d", &n)) {for (int i = 0; i < n; i++) scanf ("%d", &a[i]);        memset (Vis, 0, sizeof (VIS));        tot = 0;    printf ("%d\n", DFS (0)); } return 0;}


C:

#include <cstdio> #include <cstring> #include <algorithm>using namespace std;const int N = 100005;    typedef long Long Ll;int f[n], f2[n];int x1, x2, y1, y2, p;int pow_mod (int x, int k) {int ans = 1;        while (k) {if (k&1) ans = (ll) ans * x% P;        x = (ll) x * x% P;    K >>= 1; } return ans;    int C (int n, int m) {if (M > n | | n < 0 | | m < 0) return 0; Return (LL) f[n] * F2[m]% p * f2[n-m]% p;}    int Lucas (int n, int m) {if (M = = 0) return 1; Return (LL) C (n% p, m% p) * Lucas (n/p, m/p)% p;}        int main () {while (~scanf ("%d%d%d%d%d", &x1, &y1, &x2, &y2, &p)) {f[0] = 1;        for (int i = 1; i < min (N, p); i++) F[i] = (LL) f[i-1] * I% P;        F2[min (n, p)-1] = Pow_mod (F[min (n, p)-1], p-2);        for (int i = min (N, p)-2; I >= 0; i--) f2[i] = (LL) F2[i + 1] * (i + 1)% P;        int ans = 0; for (int i = y1; I <= y2; i++) ans = (ans + ((Lucas (x2 + 1, i + 1)-Lucas (x1, i + 1)% p + p)% p)% p;    printf ("%d\n", ans); } return 0;}


Bestcoder Round #40 A B C

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.