Represent 1 to n ^ 2 as the orthogonal and

Source: Internet
Author: User
Tags gmp

We can find two series with the length of N, so that the number of N ^ 2 obtained from each of the two series is exactly all the integers between 1 and N ^ 2.

For a positive series, if we add a constant to all the numbers in one of the series, and all the numbers in the other series subtract a constant, we will surely get a qualified series, therefore, we can think that the sequence pair obtained in this way is equivalent to the original sequence pair, which is out of our consideration.

The question is, how many unequal number of series pairs?

This problem comes from a discussion in csdn:

Http://community.csdn.net/Expert/TopicView.asp? Id = 4501259

The final computing program will use the GMP library because of the large integer.

The final result is interesting, and it is related to the factorization result of N. For example

N = p1 ^ A1 * P2 ^ A2 * .. * PT ^

The result is only related to A1, A2,... and at, but not P1, P2,... and PT.


For integer N, for a sequence
N0 = N, N1, N2,..., NK
Where n (I + 1) | n (I) and N (I + 1)> N (I), nk = 1
We call this sequence n a K-length factor sequence.
For example, there is only one factor sequence with N length of 1 as N, 1.
The number of factor sequences whose n length is S is L (N, S)
Then l (n, 0) = 0, L (n, 1) = 1
The result of the above counting problem is
F (n) = sum {L (n, k) * [L (n, k) + L (n, k-1)], for k =, 3 ,...}
For example, for n = 6 = 2*3
1 {6, 1} factor sequence with a length of 1}
2 2 {6, 2, 1}, {6, 3, 1}
The factor sequence with a length greater than 3 does not exist.
So F (6) = 1*(1 + 0) + 2*(2 + 1) = 7
For a factor sequence with N = P ^ K and T Length, it is equivalent to starting from P, P ^ 2 ,..., number of K-1 selected in P ^ (t-1), a total of C (K-1, t-1)
So l (P ^ K, t) = C (K-1, t-1)
F (P ^ K) = sum {C (K-1, t-1) * [C (K-1, t-1) + C (K-1, T-2)], t = ,..., k}
= Sum {C (K-1, t-1) * C (K, t-1), t =,..., k}
= Sum {C (K-1, s) * C (K, S), S =,..., k-1}
= Sum {C (K-1, s) * C (K, K-S), S =,..., k-1}
= C (2k-1, k) (from 2k-1 samples to take K of the solution is just from the K-1 to take s, after K to take K-s, sums all possible S)
= (2k-1 )! /(K! * (K-1 )!), That is, the conclusion of gxqcn.

The program I am writing now is to find all L (N, S) for the given n, and then calculate F (n ).
No better method has been found.

In fact, this method is not only available for the square matrix of N * n, for the Count scheme of filling 0, 1,..., M * N-1 into a matrix of M * n is also available.


Here, l (N, S) can be imagined as the following model.
Hypothesis
N = p1 ^ R1 * P2 ^ R2 *... * PT ^ RT
We imagine there was a bag containing R1 + r2 + in total... + RT balls, which have t colors in total, R1 is the first color, R2 is the second color ,..., RT is the T color.
How many solutions are there to take the ball from the bag in seconds?
The number of this solution is L (N, S ).

Find an algorithm with the time complexity of O (L ^ 2) multiplication.
Where l = R1 + r2 +... + RT

Define c (m, n) = M! /(N! * (M-N )!) Is the number of combinations.

I) calculate the number of combinations C (m, n), where M = 1, 2 ,..., L + max (R)-1. 0 <= n <= m (max (R) = max (R1, R2 ,..., RT ))
Calculation Method
C () = 1
For (m = 2; m <L + max (r); m ++ ){
C (M, 0) = 1;
For (n = 1; n <= m; n ++) C (m, n) = C (S-1, n-1) + C (S-1, N );
}
Total o (L ^ 2) Addition.

Then we define S (n, 1) = 1, S (0, K) = 1 S (n, k) = S (n, k-1) + S (N-1, k ),
Where S (n, k) is the number of Expression Methods for k non-negative integers and N (K integers are ordered, that is, 0 + 1 and 1 + 0 are different)
Ii) Calculate S (n, m), where n = 0, 1, 2,..., L; 1 <= m <= L-n + 1
With the above recursive calculation, the time complexity is also the addition of O (L ^ 2 ).

The last step is to directly calculate L (n, k)
Where l (n, 1) = 1
Iii) L (n, k) = C (R1 + k-1, k-1) * C (r2 + k-1, k-1) *... * C (RT + k-1, k-1)
-Sum {S (J + 1, K-j) * L (n, k-j-1), for J = 0 to k-1}
Recursive calculation from L (n, 1) to L (n, l), where each step of max (K, T) <= L multiplication, a total of L steps, therefore, the time complexity is O (L ^ 2) multiplication.

With L (n, k), we can use the O (l) multiplication to calculate F (n.

It is found that S (n, m) is actually C (n + S-1 m-1), so it is enough to calculate the number of combinations in advance.

 

# Include <stdio. h>
# Include <stdlib. h>
# Include <memory. h>
# Include <GMP. h>

Int * factor_list;
Int factor_count;
Int L, lar;
Mpz_t * triangle;
Mpz_t * F;
Mpz_t N;

# Define triangle (x, y) Triangle [(x) * lar + (y)]

Void set_triangle ()
{
Int I, J;
Triangle = (mpz_t *) malloc (sizeof (mpz_t) * lar * LAR );
For (I = 0; I <lar; I ++) for (j = 0; j <= I; j ++) mpz_init (Triangle (I, j ));
Mpz_set_ui (Triangle (0, 0), 1 );
Mpz_set_ui (Triangle (1, 0), 1 );
Mpz_set_ui (Triangle (1, 1), 1 );
For (I = 2; I <lar; I ++ ){
Mpz_set_ui (Triangle (I, 0), 1 );
Mpz_set_ui (Triangle (I, I), 1 );
For (j = 1; j <I; j ++ ){
Mpz_add (Triangle (I, j), triangle (I-1, J-1), triangle (I-1, j ));
}
}
}


Void calc_f (){
Int I, K;
Mpz_t M, R;
Mpz_init (m );
Mpz_init (R );
F = (mpz_t *) malloc (sizeof (mpz_t) * (L + 1 ));
For (I = 0; I <= L; I ++) mpz_init (F [I]);
Mpz_set_ui (F [1], 1 );
Printf ("L [1] = 1/N ");
For (k = 2; k <= L; k ++ ){
Mpz_set_ui (M, 1 );
For (I = 0; I <factor_count; I ++ ){
Mpz_mul (M, M, triangle (factor_list [I] + k-1, k-1 ));
}
For (I = 0; I <K-1; I ++ ){
Mpz_set (R, triangle (K, I + 1 ));
Mpz_mul (R, r, f [k-i-1]);
Mpz_sub (M, M, R );
}
Mpz_set (F [K], M );
Printf ("L [% d] =", k );
Mpz_out_str (stdout, 10, F [k]);
Printf ("/N ");
}
Mpz_clear (R );
Mpz_clear (m );
}

Void output_result ()
{
Mpz_t M, R;
Int I;
Mpz_init (m );
Mpz_init (R );
Mpz_set_ui (M, 1 );
For (I = 2; I <= L; I ++ ){
Mpz_set (r, f [I]);
Mpz_add (R, r, f [I-1]);
Mpz_mul (R, r, f [I]);
Mpz_add (M, M, R );
}
Printf ("the result is:/N ");
Mpz_out_str (stdout, 10, M );
Printf ("/N ");
Mpz_clear (R );
Mpz_clear (m );
}

# Define prime_limit 1048576
# Define sqr_limit 1024
Int prime_flag [prime_limit];
Int * prime_list;
Int prime_count;
Void init_prime (){
Int I, count;
Memset (prime_flag,-1, sizeof (prime_flag ));
Prime_flag [0] = prime_flag [1] = 0;
For (I = 2; I <= sqr_limit; I ++ ){
If (prime_flag [I]) {
Int J;
For (j = I * I; j <prime_limit; j + = I)
Prime_flag [J] = 0;
}
}
For (I = 2, Count = 0; I <prime_limit; I ++ ){
If (prime_flag [I]) Count ++;
}
Prime_count = count;
Prime_list = (int *) malloc (sizeof (INT) * count );
For (I = 2, Count = 0; I <prime_limit; I ++ ){
If (prime_flag [I]) {
Prime_list [count ++] = I;
}
}
}

Void defactor ()
{
Int I, PC, maxr;
Mpz_t R, M;
Mpz_init (R );
Mpz_init (m );
Mpz_set (m, n );
For (I = 0, Pc = 0; I <prime_count; I ++ ){
If (! Mpz_mod_ui (R, M, prime_list [I]) {
PC ++;
Do {
Mpz_fdiv_q_ui (M, M, prime_list [I]);
} While (! Mpz_mod_ui (R, M, prime_list [I]);
If (! Mpz_cmp_si (M, 1) break;
}
}
If (mpz_cmp_si (M, 1 )){
Int R;
If (r = mpz_probab_prime_p (M, 5) {// using prime test to verify the left is a prime
PC ++;
If (r = 1 ){
Fprintf (stderr, "Warning: The Left Factor cocould not be verified to be prime but looks like to a prime. It is treated as a prime/N ");
}
} Else {
Fprintf (stderr, "cannot find all prime factors of input/N ");
Exit (-1 );
}
}
Factor_list = (int *) malloc (sizeof (INT) * PC );
Factor_count = pc; L = 0, maxr = 0;
Mpz_set (m, n );
For (I = 0, Pc = 0; I <prime_count; I ++ ){
If (! Mpz_mod_ui (R, M, prime_list [I]) {
Factor_list [PC] = 0;
Do {
Mpz_fdiv_q_ui (M, M, prime_list [I]);
Factor_list [PC] ++;
} While (! Mpz_mod_ui (R, M, prime_list [I]);
L + = factor_list [PC];
If (maxr <factor_list [PC]) maxr = factor_list [PC];
PC ++;
If (! Mpz_cmp_si (M, 1) break;
}
}
If (mpz_cmp_si (M, 1 )){
Factor_list [PC ++] = 1;
L ++;
If (maxr <1) maxr = 1;
}
Lar = L + maxr + 1;
Mpz_clear (R );
Mpz_clear (m );
}


Int
Main (void)
{
Mpz_init (N );
Printf ("Please input the large number:/N ");
Mpz_inp_str (n, stdin, 10 );
Init_prime ();
Defactor ();
Set_triangle ();
Calc_f ();
Output_result ();
Mpz_clear (N );
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.