HDU5895 Mathematician QSC(经典数论集合题)(一点点更新)

来源:互联网 发布:怎样购买空间和域名 编辑:程序博客网 时间:2024/06/07 18:15

Mathematician QSC
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 142    Accepted Submission(s): 66


Problem Description

QSC dream of becoming a mathematician, he believes that everything in this world has a mathematical law.

 Through unremitting efforts, one day he finally found the QSC sequence, it is a very magical sequence, can be calculated by a series of calculations to predict the results of a course of a semester of a student.

 This sequence is such like that, first of all,f(0)=0,f(1)=1,f(n)=f(n−2)+2∗f(n−1)(n≥2)Then the definition of the QSC sequence is g(n)=∑ni=0f(i)2. If we know the birthday of the student is n, the year at the beginning of the semester is y, the course number x and the course total score s, then the forecast mark is xg(n∗y)%(s+1).
 QSC sequence published caused a sensation, after a number of students to find out the results of the prediction is very accurate, the shortcoming is the complex calculation. As clever as you are, can you write a program to predict the mark?


 


Input

First line is an integer T(1≤T≤1000).

The next T lines were given n, y, x, s, respectively.

n、x is 8 bits decimal integer, for example, 00001234.

y is 4 bits decimal integer, for example, 1234.
n、x、y are not negetive.

 1≤s≤100000000

 


Output

For each test case the output is only one integer number ans in a line.

 


Sample Input

2
20160830 2016 12345678 666
20101010 2014 03030303 333


 


Sample Output

1
317


 


Source

 2016 ACM/ICPC Asia Regional Shenyang Online

题意: 给你 n,y,x,s  问 (x^g(n∗y))%(s+1),gn=.∑(0--n)fi^2 ,fn:f(0)=0,f(1)=1,f(n)=f(n−2)+2∗f(n−1)(n≥2)

题解:题中给出fn的公式,构建两个矩阵 |  2,1| 和   | fn,        0    |

                                                               | 1,0 |        |  f(n-1),0   |

矩阵快速幂求出fn,打表gn,发现gn=fn*f(n+1)/2, 这样我们完成了第一步。剩下的学了再补。。。。

#include<stdio.h>#include<string.h>typedef struct haha {     int mar[3][3]; }marty; marty a,c; int n,m; marty multi(struct haha a1,struct haha a2) {     marty temp;     int i,j,k;     for(i=0;i<3;i++)         for(j=0;j<3;j++)         {             temp.mar[i][j]=0;             for(k=0;k<3;k++)             {                 temp.mar[i][j]+=(a1.mar[i][k]*a2.mar[k][j]);             }         }         return temp;}marty  matrix_binary(int k){     marty b;     memset(b.mar,0,sizeof(b.mar));     b.mar[0][0]=2;     b.mar[0][1]=0;     b.mar[1][0]=1;     b.mar[1][1]=0;     while(k)     {         if(k & 1)             b = multi(a,b);         a = multi(a,a);         k = k >> 1;     }     return b; } int main() {     a.mar[0][0]=2;     a.mar[0][1]=1;     a.mar[1][0]=1;     a.mar[1][1]=0;     while(scanf("%d",&n))     {     c=matrix_binary(n-2); } }


0 0
原创粉丝点击