HDU 5015 233 matrix (the fastest way)

Source: Internet
Author: User

233 Matrix Time Limit: 10000/5000 MS (Java/others) memory limit: 65536/65536 K (Java/Others)
Total submission (s): 191 accepted submission (s): 125


Problem descriptionin our daily life we often use 233 to express our feelings. actually, we may say 2333,233 33, or 233333... in the same meaning. and here is the question: Suppose we have a matrix called 233 matrix. in the first line, it wocould be 233,233 3, 23333... (It means A0, 1 = 233, A0, 2 = 2333, A0, 3 = 23333 ...) besides, in 233 matrix, we got AI, j = ai-1, J + AI, J-1 (I, j = 0 ). now you have known A1, 0, A2, 0,..., An, 0, cocould you tell me an, m in the 233 matrix?
Inputthere are multiple test cases. Please process till EOF.

For each case, the first line contains two postive integers n, m (n ≤ 10, M ≤109 ). the second line contains N integers, A1, 0, A2, 0 ,..., an, 0 (0 ≤ AI, 0 <231 ).
Outputfor each case, output an, M mod 10000007.
Sample Input
1 112 20 03 723 47 16
 
Sample output
234279972937Hint
  
 
Source2014 ACM/ICPC Asia Regional Xi 'an online
Recommendhujie | we have carefully selected several similar problems for you: 5017 5016 5014 5013

In the Code:


# Include <cstdio> # define mod 10000007 using namespace STD; typedef long ll; # define rep (I, a, B) for (INT I = A; I <B; I ++) ll inv [11]; // reverse ll inv_fac [11]; // factorial inverse ll n, m, a [11], TMP; /** after simulation, it is found that ANS = C (M, 0) * A [n-1] + C (m +) * A [N-2] +... C (m + n, n) * A [0] + C (n, 0) * (23333 ...) + C (n + 1, 1) * (2333 ...) + C (n + m-1) * 233; deformation is as follows: ANS = C (M, 0) * A [n-1] + C (m +) * A [N-2] +... C (m + n, n) * A [0] + C (n, n) * (23333 ...) + C (n + 1, n) * (2333 ...) + C (n + m-2, n) * 233; ans = C (M, 0) * A [n-1] + C (m +) * A [N-2] +... C (m + n, n) * A [0] + {C (n, n) + C (n + 1, n) + C (n + m-2, n )} * 233 + 2100*{10 ^ (m-2) * C (n, n) + 10 ^ (m-3) * C (n + 1, n) + 10 ^ 0 * C (n + M-4, n)}; ans = C (M, 0) * A [n-1] + C (m +) * A [N-2] +... C (m + n, n) * A [0] + C (n + s-1, n) * 233 + 2100*{10 ^ (m-2) * C (n, n) + 10 ^ (m-3) * C (n + 1, n) + 10 ^ 0 * C (n + M-4, n)}; set F (N) = {10 ^ (m-2) * C (n, n) + 10 ^ (m-3) * C (n + 1, n) + 10 ^ 0 * C (n + M-4, n)}, then f (n) = (10 * F (n-1)-C (n + m-2, n)/9) % MOD; that is, F (N) = (10 * F (n-1)-C (n + m-2, n) * inv [9] % MOD; **/Vo Id Init () // initialization Array {int I; inv [0] = inv [1] = 1; inv_fac [0] = inv_fac [1] = 1; for (I = 2; I <15; I ++) {inv [I] = (INV [mod % I] * (mod-mod/I) % MOD; /// Recursive Inverse element inv_fac [I] = (inv_fac [I-1] * inv [I]) % MOD; /// returns the inverse element of the factorial} ll C (ll n, ll K) // calculates the number of combinations {If (k = 0) return 1; ll I, ans = inv_fac [k]; for (I = 0; I <K; I ++) ans = ans * (N-I) % MOD; return ans ;} ll quick_mod (ll x, ll N) // fast power modulo {ll temp = x % mod, result = 1; while (n) {If (N & 1) result = Result * temp % MOD; temp = Temp * temp % MOD; n >>= 1;} return result;} ll f (ll n) {If (n = 0) Return (tmp-1 + mod) * inv [9] % MOD; // sum of the used proportional Series sum [N] = A1 (1-Q ^ N)/(1-Q); Return (10 * F (N-1) -C (n + m-2, n) * inv [9] % MOD;} void solve () {If (n = 0 & M = 0) {puts ("0"); return;} TMP = quick_mod (10 m-1); LL ans = (C (n + m-1, n) * 233 + f (N) * 2100) % MOD; // The sum of the first line and the number of multiplication groups int I; for (I = 1; I <= N; I ++) ans = (ANS + C (n + m-1-i, n-I) * A [I-1]) % MOD; /// add the first column and the number of multiplication groups printf ("% i64d \ n", ANS);} int Mai N () {Init (); While (~ Scanf ("% i64d % i64d", & N, & M) {rep (I, 0, n) {scanf ("% i64d", & A [I]); A [I] % = MOD;} solve ();} return 0 ;}

HDU 5015 233 matrix (the fastest way)

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.