HDU 4059 The Boss on Mars-matrix + rejection

Source: Internet
Author: User

The error is 29 times, and the final result is .....

According to the question, it is easy to think of rejection.

Then the question is how to find

Sum (n) = 1 ^ 4 + 2 ^ 4 + 3 ^ 4 +... + n ^ 4;

There are three types of roads:

Apparently: 1 ^ 4 + 2 ^ 4 + 3 ^ 4 + .... + n ^ 4 = (n ^ 5)/5 + (n ^ 4)/2 + (n ^ 3)/EDTA/30;

Then 1, use the java large number to knock on this code.

2. Use c ++, but use the score modulo to calculate the reverse element.

3. Use c ++, but do not use this formula. Use a matrix to construct sum (n ).

I use the third method. However, the third defect is that the time complexity is a little high.

The next question is how to optimize the time complexity.

Preprocessing is supported ~ Sum of W, and then the rest are constructed using a matrix.

See the code for the specific construction method ..


# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
Using namespace std; # define LL _ int64 # define MOD 1000000007 # define maxn 2000007LL yu [maxn]; struct matrix {LL mat [7] [7]; matrix () {memset (mat, 0, sizeof (mat);} friend matrix operator * (matrix A, matrix B) {int I, j, k; matrix C; for (I = 1; I <= 6; I ++) {for (j = 1; j <= 6; j ++) {for (k = 1; k <= 6; k ++) {C. mat [I] [j] = (C. mat [I] [j] + (. mat [I] [k] * B. mat [k] [j]) % MOD;} C. mat [I] [j] = C. mat [I] [j] % MOD ;}} return C ;}one, AA [30]; matrix powmul (matrix A, LL k) {matrix B; for (int I = 1; I <= 6; I ++) B. mat [I] [I] = 1; int l = 1; while (k) {if (k & 1) B = B * AA [l]; l ++; k> = 1;} return B;} vector
      
        Vec; void init () {int I, j; // constructs a matrix ONE. mat [1] [1] = 1; ONE. mat [1] [2] = 1; ONE. mat [1] [3] = 4; ONE. mat [1] [4] = 6; ONE. mat [1] [5] = 4; ONE. mat [1] [6] = 1; ONE. mat [2] [1] = 0; ONE. mat [2] [2] = 1; ONE. mat [2] [3] = 4; ONE. mat [2] [4] = 6; ONE. mat [2] [5] = 4; ONE. mat [2] [6] = 1; ONE. mat [3] [1] = 0; ONE. mat [3] [2] = 0; ONE. mat [3] [3] = 1; ONE. mat [3] [4] = 3; ONE. mat [3] [5] = 3; ONE. mat [3] [6] = 1; ONE. mat [4] [4] = 1; ONE. mat [4] [5] = 2; ONE. mat [4] [6] = 1; ONE. mat [5] [4] = 0; ONE. mat [5] [5] = 1; ONE. mat [5] [6] = 1; ONE. mat [6] [6] = 1; yu [0] = 0; yu [1] = 1; LL x; for (I = 2; I
       
         = N) return 0; LL p = 1; LL ns = 4; while (ns --) {p = p * x; p = p % MOD;} ns = (n-1) /x; p = p * kan (ns); p = p % MOD; return p;} void dos (LL n) {LL p = 1; LL ans = 0; int I, j, leap; LL m = n; vec. clear (); for (I = 2; I * I <= n; I ++) // The process of Factor Evaluation {if (n % I = 0) {vec. push_back (I);} while (n % I = 0) n = n/I;} if (n! = 1) vec. push_back (n); n = m; int t = 1 <(vec. size (); for (I = 1; I
        
         

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.