Lightoj 1132-summing up Powers matrix fast Power + permutation combination

Source: Internet
Author: User

Links: http://lightoj.com/volume_showproblem.php?problem=1132


1132-summing up Powers
PDF (中文版) Statistics Forum
Time Limit: 2 second (s) Memory Limit: MB

Given N and K, you had to find

(1K + 2K + 3K + ... + NK)% 232

Input

Input starts with an integer T (≤200), denoting the number of test cases.

Each case contains the integers N (1≤n≤1015) and K (0≤k≤50) in a.

Output

For each case, print the case number and the result.

Sample Input Output for Sample Input

3

3 1

4 2

3 3

Case 1:6

Case 2:30

Case 3:36


Test instructions: Calculates the value of the string of equations for N and K.


Practice:

Find out how 1^k pushed to 2^k and then push to N^k method, then open one-dimensional record total value, OK.

Initial matrix

1^ 0 1^1 1^2 1^3 ..... 1^k Total

Construct The matrix:

C (0,0) C (0,1) C (0,2) C (0,3) ... C (0,k-1) C (0,k) 0

0 C (+ C) C (1,3) ... C (1,k-1) C (1,k) 0

......

0 0 0 0 C (k-1,k-1) C (k-1,k) 0

0 0 0 0 0 C (k,k) 1

0 0 0 0 0 0 1


C (n,m) =c (n-1,m) +c (n-1,m-1);


#include <stdio.h> #include <string.h> #define MATR 55//matrix size, note can be small on the small matrix starting from 1 so Matr to +1 Max can 100#define ll uns igned int#define LL long longstruct mat//matrix struct, a means content, size matrix starting from 1 but size does not add a {LL A[matr][matr];mat ()//constructor {memset (a,0, sizeof (a));}; int Size; Mat multi (Mat m1,mat m2)//Two multiplication of equal matrices, for sparse matrices, there are 0 optimizations without operations {Mat ans=mat (); for (int. i=1;i<=size;i++) for (int j=1;j<= size;j++) if (M1.a[i][j])//Sparse matrix optimization for (int k=1;k<=size;k++) ans.a[i][k]= (Ans.a[i][k]+m1.a[i][j]*m2.a[j][k]); I row k column J item return ans; Mat Quickmulti (Mat m,ll N)//fast power of two minutes {mat ans=mat (); int i;for (i=1;i<=size;i++) ans.a[i][i]=1;while (n) {if (n&1) ans= multi (M,ans);//odd multiply even sub-multiplication is very good to remember. M=multi (m,m); n>>=1;} return ans;}  void print (Mat m)//output matrix information, debug with {int i,j;  printf ("%d\n", Size);  for (i=1;i<=size;i++) {for (j=1;j<=size;j++) printf ("%u", m.a[i][j]);  printf ("\ n"); }} int main () {LL n;int t;int cas=1;int k;scanf ("%d", &t), while (t--) {scanf ("%lld%d", &n,&k); Mat Gouzao=mat () , Chu=mat ();p rintf ("Case%d:", cas++); size=k+2;for (int i=1;i<=k+1;i++) {chu.a[1][i]=1;} for (int j=1;j<=k+1;j++) {for (int i=1;i<=j;i++) {if (i==1| | I==J) {gouzao.a[i][j]=1;continue;} ELSE{GOUZAO.A[I][J]=GOUZAO.A[I][J-1]+GOUZAO.A[I-1][J-1];}}} gouzao.a[k+1][k+2]=1;gouzao.a[k+2][k+2]=1;/*printf ("chu\n");p rint (CHU);p rintf ("gouzao\n");p rint (GOUZAO); */ printf ("%u\n", multi (Chu,quickmulti (Gouzao,n)). A[1][k+2]);}  return 0;}










Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Lightoj 1132-summing up Powers matrix fast Power + permutation combination

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.