前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDUOJ--4565 So Easy!

HDUOJ--4565 So Easy!

作者头像
Gxjun
发布2018-03-22 11:31:10
5020
发布2018-03-22 11:31:10
举报
文章被收录于专栏:mlml

So Easy!

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

Problem Description

  A sequence Sn is defined as:

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!

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

2013 ACM-ICPC长沙赛区全国邀请赛——题目重现

此题的思路在于这儿....看图...

代码语言:javascript
复制
 1 /*快速矩@ coder Gxjun*/
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 __int64 ma[2][2];   //matrix
 6 __int64 ans[2][2]; 
 7 void init(__int64 a[][2])
 8 {
 9     int i;
10    for(i=0;i<2;i++)
11           a[i][i]=1;   //E 设置为1
12           a[1][0]=0;
13           a[0][1]=0;
14 }
15 int main()
16 {
17     int    a,b,m,n,i,j,k;
18     __int64 temp[2] ;
19     __int64 hop[2][2];
20     while(scanf("%d%d%d%d",&a,&b,&n,&m)!=EOF)
21     {
22         if(n==1)
23         {
24             printf("%I64d\n",(2*a)%m);
25             continue;
26         }
27         else if(n==2)
28         {
29             printf("%I64d\n",(2*(a*a+b))%m);
30             continue;
31         }
32         n-=2;
33         init(ans);
34         /*init(ma);*/
35          ma[0][0]=(2*a)%m;
36          ma[0][1]=(b-a*a)%m;
37          ma[1][0]=1;
38          ma[1][1]=0;
39         while(n>0)
40         {
41             if(n&1)
42             {
43                 for(k=0;k<2;k++)
44                 {
45                  memset(temp,0,sizeof(temp));
46                  for(i=0;i<2;i++)
47                  {
48                     for(j=0;j<2;j++)
49                     {
50                       temp[i]+=ans[k][j]*ma[j][i];
51                       temp[i]%=m;
52                     }
53                  }
54                  ans[k][0]=temp[0];
55                  ans[k][1]=temp[1];
56                 }
57                  n--;
58                 continue;
59             }
60            memset(hop,0,sizeof(hop));
61             for(k=0;k<2;k++)
62             {
63               for(i=0;i<2;i++)
64               {
65                 for(j=0;j<2;j++)
66                 { 
67                   hop[k][i]+=ma[k][j]*ma[j][i];
68                   hop[k][i]%=m;    
69                 }
70              }
71             }
72             for(i=0;i<2;i++)
73             {
74                 for(j=0;j<2;j++)
75                 {
76                   ma[i][j]=hop[i][j];
77                 }
78             }
79             n>>=1;
80         }
81         __int64 gong=(2*((ans[0][1]*a)%m+(ans[0][0]*(a*a+b)%m)%m)%m)%m;
82         if(gong<0) gong=m+gong;
83         printf("%I64d\n",gong);
84     }
85     return 0;
86 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-03-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • So Easy!
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档