Bell (hdu4767 + matrix + Chinese Remainder Theorem + bell number + Stirling Number + Euclidean), hdu4767stirling

Source: Internet
Author: User

Bell (hdu4767 + matrix + Chinese Remainder Theorem + bell number + Stirling Number + Euclidean), hdu4767stirling
BellTime Limit:3000 MSMemory Limit:32768KB64bit IO Format:% I64d & % I64uSubmit Status Practice HDU 4767 DescriptionWhat? MMM is learning Combinatorics !?
Looks like she is playing with the bell sequence now:
Bell [n] = number of ways to partition of the set {1, 2, 3,..., n}

E.g. n = 3:
{1, 2, 3}
{1} {2 3}
{1, 2} {3}
{1, 3} {2}
{1} {2} {3}
So, bell [3] = 5

MMM wonders how to compute the bell sequence efficiently. InputT -- number of cases
For each case:
N (1 <= n <2 ^ 31) Outputfor each case:
Bell [n] % 95041567 Sample Input 6123456 Sample Output 1251552203 first, I'm glad to finally solve this problem !!! I have had a few difficulties that I have never figured out. I thought about it when I was bored today. Then the thought goes along inexplicably, and everything is taken for granted... Then, I took A hard time and thought about it!

This is the number of division methods for a non-empty set of n numbers;

For example, n = 3

{1, 2, 3}

{1} {2, 3}

{1, 2} {3}

{1, 3} {2}

{1} {2} {3}

So Bell (3) = 5

Give you an n, and calculate the value of Bell (n) % 95041567

 

This question involves a lot of knowledge. I think it has a certain difficulty coefficient and I don't understand it. I suggest you start with the entry question first. thought: first, you must understand the concept of bell number (from Wikipedia) & Stirling Number (links that do not understand). Second, if you do not understand the Stirling Number, click it on your own, in fact, this is still simple, hang DIAN has a simple question, can practice: http://acm.hdu.edu.cn/showproblem.php? Pid = 2512 I believe many people have done it, but I didn't know it was the Stirling number. Here is the second type of Stirling number, and then the bell number is the sum of the second type of Stirling number; through the above formula, we can use the Matrix to calculate the number of Bell numbers greater than n. The method is to use the Chinese Remainder Theorem to combine Euclidean and homophone equations. First we input n (Suppose, n> p, p is a quality factor of 95041567. For example: n = 50), preprocessing is performed on the number of Bell numbers of n <50 to construct a matrix of p * p;

Using the formula, their remainder is saved to the array at [I]; prime factor m [5] = {31,37, 41,43, 47}; using the Chinese Remainder theorem, the remainder is obtained!

 

Reprinted please indicate the source: Find and star child questions link: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 4767

 

1 # include <stdio. h> 2 # include <string. h> 3 # define LL long 4 # define mod 95041567 5 # define MM 50 6 7 int m [5] = {, 47 }; 8 int Sti [MM] [MM], bell [MM] [MM]; 9 int at [5]; // the remainder of each item 10 11 void stirling2 () 12 {13 memset (Sti, 0, sizeof (Sti); 14 Sti [0] [0] = 1; 15 for (int I = 1; I <= MM; I ++) 16 {17 for (int j = 1; j <= I; j ++) 18 {19 Sti [I] [j] = (int) (Sti [I-1] [J-1] + (LL) j * (LL) Sti [I-1] [j]) % mod; 20} 21} 22} 23 void init () 24 {25 stirling2 (); 26 for (int I = 0; I <5; I ++) 27 {28 bell [I] [0] = 1; 29 for (int j = 1; j <m [I]; j ++) 30 {31 bell [I] [j] = 0; 32 for (int k = 1; k <= j; k ++) 33 {34 bell [I] [j] = (bell [I] [j] + Sti [j] [k]) % m [I]; 35} 36 // printf ("% d \ t % d \ n", j, bell [I] [j]); 37} 38} 39} 40 struct Matrix 41 {42 int mat [MM] [MM]; 43}; 44 45 Matrix multiply (Matrix a, Matrix B, int M) 46 {47 Matrix c; 48 memset (c. mat, 0, sizeo F (c. mat); 49 for (int I = 0; I <M; I ++) 50 {51 for (int j = 0; j <M; j ++) 52 {53 if (. mat [I] [j] = 0) continue; 54 for (int k = 0; k <M; k ++) 55 {56 if (B. mat [j] [k] = 0) continue; 57 c. mat [I] [k] = (c. mat [I] [k] +. mat [I] [j] * B. mat [j] [k]) % M; 58} 59} 60} 61 return c; 62} 63 Matrix quickmod (Matrix a, int n, int M) 64 {65 Matrix res; 66 67 for (int I = 0; I <M; I ++) 68 {69 for (int j = 0; j <M; j ++) 70 res. mat [I] [j] = (I = j); 71} 72 7 3 while (n) 74 {75 if (n & 1) res = multiply (res, a, M); 76 n> = 1; 77 a = multiply (, a, M); 78} 79 return res; 80} 81 82 int work (int n, int M, int k) 83 {84 Matrix per; // base Matrix; 85 memset (per. mat, 0, sizeof (per. mat); 86 per. mat [0] [M-1] = 1; 87 88 for (int I = 1; I <M; I ++) 89 {90 per. mat [I] [I] = per. mat [I] [I-1] = 1; 91} 92 93 Matrix tmp = quickmod (per, n/(M-1), M); 94 95 int ans [MM]; 96 for (int I = 0; I <M; I ++) 97 {98 ans [I] = 0; 99 for (int j = 0; j <M; j ++) 100 {101 ans [I] = (ans [I] + bell [k] [j] * tmp. mat [I] [j]) % M; 102} 103} 104 return ans [n % (M-1)]; 105} 106 void exgcd (int a, int B, int & d, int & x, int & y) 107 {108 if (! B) {d = a; x = 1; y = 0;} 109 else110 {111 exgcd (B, a % B, d, y, x ); 112 y-= x * (a/B); 113} 114} 115 int China (int r) 116 {117 int Mc = 1; 118 int Mi, d, x, y, as = 0; 119 for (int I = 0; I <r; I ++) 120 {121 Mc * = m [I]; 122} 123 for (int I = 0; I <r; I ++) 124 {125 Mi = Mc/m [I]; 126 exgcd (Mi, m [I], d, x, y); 127 as = (as + Mi * x * at [I]) % Mc; 128} 129 if (as <0) as + = Mc; 130 return as; 131} 132 int main () 133 {134 int T, n; 135 scanf ("% d", & T); 136 137 init (); 138 while (T --) 139 {140 scanf ("% d", & n); 141 for (int I = 0; I <5; I ++) 142 {143 at [I] = work (n, m [I], I); 144} 145 int sol = China (5 ); 146 printf ("% d \ n", sol); 147} 148 149 return 0; 150}

 

 

The time passes so fast that we have to take the final exam. There are not many days in school. aking2015...... for our common dream! Fighting!

 

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.