2014 Xian live game F Q Uvala 7040

Source: Internet
Author: User

Address

Test instructions: To choose K colors in M colors, there are several ways to paint n flowers.

Analysis: Drawing can be found that the basic formula is KX (K-1) ^ (n-1). However, this only guarantees that the adjacent color, the total number of colors not more than k, and there is no guarantee that there is a k color; Then there is a repulsion problem, the above calculation method contains only 2, 3 、...、 (k-1) Color of the case, need to be removed by the principle of repulsion. Assuming P (2 <= P <= k-1) color, choose p species from the k color to paint, the scheme number is C (k,p) xpx (p-1) ^ (n-1); The total number of scenarios is C (M,K) x (KX (K-1) ^ (n-1) +∑ (( -1) ^px C (k, p) xpx (p-1) ^ (n-1)).

Diagram Method:

There are 8 kinds of cases, but there are two types that are only two colors and need to be lost.

TLE Reason: 1) The inverse element does not hit the table; 2) The parameter is redundant, the mod is a global variable, but the template has this item, when the parameters are passed on, the result is tle.

This is our last year in Xi ' an live game when the topic, on the card in this topic, has been tle, has not been able to change, today the topic reproduced once, still tle. It is not a solid thing when learning new knowledge.

1#include <cstdio>2#include <iostream>3#include <sstream>4#include <cmath>5#include <cstring>6#include <cstdlib>7#include <string>8#include <vector>9#include <map>Ten#include <Set> One#include <queue> A#include <stack> -#include <algorithm> - using namespacestd; the #definell Long Long - #define_cle (M, a) memset (M, A, sizeof (m)) - #defineRepu (I, A, b) for (int i = A; I < b; i++) - #defineREPD (I, A, b) for (int i = b; i >= A; i--) + #defineSFI (n) scanf ("%d", &n) - #definePFI (n) printf ("%d\n", N) + #defineSfi2 (n, m) scanf ("%d%d", &n, &m) A #definePfi2 (n, m) printf ("%d%d\n", N, m) at #definePfi3 (A, B, c) printf ("%d%d%d\n", A, B, c) - #defineMAXN 1000005 - #defineMOD 1000000007 - Const intINF =0x3f3f3f3f; - ll INV[MAXN]; - ll Quickpow (ll M, ll N) in { -ll ans =1; to      while(n) +     { -         if(N &1)///if n is an odd number theAns = (ans * m)%MOD; *n = n >>1;///bitwise operation "Right shift 1 similar to 2" $m = (M * m)%MOD;Panax Notoginseng     } -     returnans; the } + ll C (ll N, ll M) A { the     if(M > N)return 0; +ll ans =1; -      for(inti =1; I <= m; i++) $     { $ll a = (n-m + i)%MOD; -ll B = i%MOD; -Ans = ans * (A * QUICKPOW (b, MOD-2)% MOD)%MOD; the     } -     returnans;Wuyi } the ll Lucas (ll N, ll M) - { Wu     if(M = =0)return 1; -     Else About         return(C (n mod, m% MoD) * Lucas (N/mod, m/mod))%MOD; $ } - void Get() - { -Repu (I,1, MAXN) AInv[i] = Quickpow (i, MOD-2); + } thell cc[1010100]; - intMain () $ { the     Get(); the     intT; the SFI (T); the     intKase =1; - ll N, M, K; in      while(t--) the     { thescanf"%lld%lld%lld", &n, &m, &k); About         if(k==1) the         { theprintf"Case #%d:", kase++); the             if(n==1) printf ("%d\n", m); +             Elseprintf"0\n"); -             Continue ; the         }Bayill CMK =Lucas (M, k); thell t = Quickpow (K-1N1); thet = (t * k)%MOD; -         intFlag =0; -ll cc =1; the          for(ll p = k-1; P >=2; p--) the         { theCC = (((CC * (P +1)) (% MOD) * Inv[k-p])%MOD; the             ///The hand residue added a mod also will be tle ....  -ll q = (((CC * p)% MOD) * QUICKPOW (P-1N1)) %MOD; the             if(flag) T = (t + q)%MOD; the             ElseT = (t-q + MOD)%MOD; theFlag =!Flag;94         } theprintf"Case #%d:%lld\n", kase++, (t * cmk)%MOD); the     } the     return 0;98}
View Code

2014 Xian live game F Q Uvala 7040

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.