Hdu 5015 233 Matrix (the fastest way), hdu5015

Source: Internet
Author: User

Hdu 5015 233 Matrix (the fastest way), hdu5015

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; **/v Oid 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 ma In () {Init (); while (~ Scanf ("% I64d % I64d", & n, & m) {rep (I, 0, n) {scanf ("% I64d", & a [I]); a [I] % = MOD;} solve ();} return 0 ;}




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.