码迷,mamicode.com
首页 > 其他好文 > 详细

hdu So Easy!

时间:2014-05-30 01:47:19      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:des   c   style   class   blog   code   

So Easy!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2026    Accepted Submission(s): 624


Problem Description
  A sequence Sn is defined as:
bubuko.com,布布扣

Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example, ┌3.14┐=4. You are to calculate Sn.
  You, a top coder, say: So easy! 
bubuko.com,布布扣
 

 

Input
  There are several test cases, each test case in one line contains four positive integers: a, b, n, m. Where 0< a, m < 215, (a-1)2< b < a2, 0 < b, n < 231.The input will finish with the end of file.
 

 

Output
  For each the case, output an integer Sn.
 

 

Sample Input
2 3 1 2013
2 3 2 2013
2 2 1 2013
 

 

Sample Output
4
14
4
 

 

Source

 

bubuko.com,布布扣
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<cstring>
 4 #include<cstdlib>
 5 using namespace std;
 6 typedef __int64 LL;
 7 
 8 LL  p ;
 9 struct Matrix
10 {
11     LL mat[3][3];
12     void init()
13     {
14         mat[1][1]=1;mat[1][2]=0;
15         mat[2][1]=0;mat[2][2]=1;
16     }
17     void mem(LL a,LL b)
18     {
19         mat[1][1]=(2*a)%p; mat[1][2]=(b-a*a)%p;
20         mat[2][1]=1;     mat[2][2]=0;
21     }
22 };
23 Matrix multiply(Matrix cur,Matrix ans)
24 {
25     Matrix now;
26     memset(now.mat,0,sizeof(now.mat));
27     int i,j,k;
28     for(i=1;i<=2;i++)
29     {
30         for(k=1;k<=2;k++)
31         {
32             for(j=1;j<=2;j++)
33             {
34                 now.mat[i][j]+=cur.mat[i][k]*ans.mat[k][j];
35                 now.mat[i][j]%=p;
36                 while(now.mat[i][j]<0) now.mat[i][j]+=p;
37             }
38         }
39     }
40     return now;
41 }
42 void pow_mod(Matrix cur,LL n,LL a,LL b)
43 {
44     Matrix ans;
45     ans.init();
46     while(n)
47     {
48         if(n&1) ans=multiply(ans,cur);
49         n=n>>1;
50         cur=multiply(cur,cur);
51     }
52     LL sum=(ans.mat[1][1]*2*a+ans.mat[1][2]*2)%p;
53     printf("%I64d\n",sum);
54 }
55 int main()
56 {
57     LL a,b,n;
58     while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&n,&p)>0)
59     {
60         Matrix hxl;
61         hxl.mem(a,b);
62         if(n>1)
63         pow_mod(hxl,n-1,a,b);
64         else printf("%I64d\n",(2*a)%p);
65     }
66     return 0;
67 }
bubuko.com,布布扣

 

hdu So Easy!,布布扣,bubuko.com

hdu So Easy!

标签:des   c   style   class   blog   code   

原文地址:http://www.cnblogs.com/tom987690183/p/3757901.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!