(HDU 1757)A Simple Math Problem

A Simple Math Problem
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

Lele now is thinking about a simple function f(x). 

If x < 10 f(x) = x. 
If x >= 10 f(x) = a0 * f(x-1) + a1 * f(x-2) + a2 * f(x-3) + …… + a9 * f(x-10); 
And ai(0<=i<=9) can only be 0 or 1 . 

Now, I will give a0 ~ a9 and two positive integers k and m ,and could you help Lele to caculate f(k)%m. 
 

Input

The problem contains mutiple test cases.Please process to the end of file. 
In each case, there will be two lines. 
In the first line , there are two positive integers k and m. ( k<2*10^9 , m < 10^5 ) 
In the second line , there are ten integers represent a0 ~ a9. 
 

Output

For each case, output f(k) % m in one line.
 

Sample Input

10 9999 1 1 1 1 1 1 1 1 1 1 20 500 1 0 1 0 1 0 1 0 1 0
 

Sample Output

45 104
 

Please process to the end of file

  沒注意這句話,WA了好幾次。。



#include <iostream>  
#include <cstdio>  
#include <cstring>     
int n , m;  
int A[12];
struct Node  
{  
    int m[12][12];  
    int x;  
    int y;  
}pt , st , dt;  
Node Pow(Node a, Node b)  
{  
      memset(dt.m , 0 , sizeof(dt.m));  
         dt.x = a.x;  
          dt.y = b.y;  
    for(int i = 1 ; i <= a.x; i++)  
       {  
         for(int k = 1; k <= a.y; k++)  
          {  
             if(a.m[i][k] == 0)  
                 continue;  
                 {  
                   for(int j = 1; j <= b.y; j++)  
                    {  
                        dt.m[i][j] = ((dt.m[i][j] + a.m[i][k] * b.m[k][j]) % m) % m;  
  
                    }  
                }      
        }  
      }  
      return dt;  
}  
void  quickpow(int n)  
{     
      while(n)  
    {  
       if(n % 2 == 1)     
         pt = Pow(st , pt);  
        n = n / 2;  
        st = Pow(st , st);  
    }  
 }   
int main()  
{   
     while(~scanf("%d %d",&n , &m))  
   {      
        memset(pt.m , 0 ,sizeof(pt.m));   
        memset(st.m , 0 , sizeof(st.m));
          pt.x = pt.y = 10;  
         st.x = st.y = 10; //矩陣的行列長度   
         for(int i = 0; i <= 9; i++)
		   scanf("%d",&st.m[i+1][1]);   
		for(int i = 1; i <= 10; i++)
		   pt.m[i][i]  =  1;
			 for(int i = 2; i <= 10; i++)
			 {
			 	  st.m[i-1][i] = 1;//寫成了st.m[i][i-1]  檢查了n多次 才找到
			 }
			 if(n < 9)
			 printf("%d\n",n%m);
			 else
	   {
			 quickpow(n-9);
			 int sum;
			  sum = 9*pt.m[1][1]+8*pt.m[2][1]+7*pt.m[3][1]+6*pt.m[4][1]+5*pt.m[5][1]+4*pt.m[6][1]+3*pt.m[7][1]+2*pt.m[8][1]+1*pt.m[9][1];
          printf("%d\n",sum%m);
       }
    }
     
    return 0;  
}  














發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章