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:

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
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef __int64 LL; LL p ;
struct Matrix
{
LL mat[][];
void init()
{
mat[][]=;mat[][]=;
mat[][]=;mat[][]=;
}
void mem(LL a,LL b)
{
mat[][]=(*a)%p; mat[][]=(b-a*a)%p;
mat[][]=; mat[][]=;
}
};
Matrix multiply(Matrix cur,Matrix ans)
{
Matrix now;
memset(now.mat,,sizeof(now.mat));
int i,j,k;
for(i=;i<=;i++)
{
for(k=;k<=;k++)
{
for(j=;j<=;j++)
{
now.mat[i][j]+=cur.mat[i][k]*ans.mat[k][j];
now.mat[i][j]%=p;
while(now.mat[i][j]<) now.mat[i][j]+=p;
}
}
}
return now;
}
void pow_mod(Matrix cur,LL n,LL a,LL b)
{
Matrix ans;
ans.init();
while(n)
{
if(n&) ans=multiply(ans,cur);
n=n>>;
cur=multiply(cur,cur);
}
LL sum=(ans.mat[][]**a+ans.mat[][]*)%p;
printf("%I64d\n",sum);
}
int main()
{
LL a,b,n;
while(scanf("%I64d%I64d%I64d%I64d",&a,&b,&n,&p)>)
{
Matrix hxl;
hxl.mem(a,b);
if(n>)
pow_mod(hxl,n-,a,b);
else printf("%I64d\n",(*a)%p);
}
return ;
}

hdu So Easy!的更多相关文章

  1. HDU 别easy在一系列的

    别easy在一系列的 Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Su ...

  2. HDU 5572--An Easy Physics Problem(射线和圆的交点)

    An Easy Physics Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  3. HDU 4359——Easy Tree DP?——————【dp+组合计数】

    Easy Tree DP? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  4. HDU——4565So Easy!(矩阵快速幂)

    So Easy! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  5. HDU 2601An easy problem-素数的运用,暴力求解

    id=17433" target="_blank" style="color:blue; text-decoration:none">An ea ...

  6. HDU 5475An easy problem 离线set/线段树

    An easy problem Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  7. HDU 4359 Easy Tree DP?

    Easy Tree DP? Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  8. 2015 Multi-University Training Contest 6 hdu 5357 Easy Sequence

    Easy Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  9. ACM 2015年上海区域赛A题 HDU 5572An Easy Physics Problem

    题意: 光滑平面,一个刚性小球,一个固定的刚性圆柱体 ,给定圆柱体圆心坐标,半径 ,小球起点坐标,起始运动方向(向量) ,终点坐标 ,问能否到达终点,小球运动中如果碰到圆柱体会反射. 学到了向量模板, ...

随机推荐

  1. fzu 2111 Min Number

      http://acm.fzu.edu.cn/problem.php?pid=2111  Problem 2111 Min Number Accept: 572    Submit: 1106Tim ...

  2. redis数据库使用测试

    注:java spring data redis内置了几种redis序列化机制.JdkSerializationRedisSerializer最高效.有关序列化更详细的介绍-http://www.my ...

  3. angularjs---$http.post发的数据,后台取不到

    我用$http.post(url, data). 后台用play框架,不知道为什么总是取不到data数据.如果直接用$.post(url, data); 就可以! 后台Play的一个action: 打 ...

  4. android怎么换背景图片

    我不晓得一般是怎么做的,但是至少可以用两种方法,一种是用一个全屏的ImageView来当作背景,通过修改imageview来修改背景图片,一种是将你xml中最外层的那个布局,LinerLayout之类 ...

  5. libSVM的数据格式

    首先介绍一下 libSVM的数据格式 Label 1:value 2:value -. Label:是类别的标识,比如上节train.model中提到的1 -1,你可以自己随意定,比如-10,0,15 ...

  6. microsoft .netframework Available Source Code Components

    http://referencesource.microsoft.com/netframework.aspx http://blogs.msdn.com/b/dotnet/archive/2012/0 ...

  7. OpenStack 镜像密码修改办法

    Contents [hide] 1 场景 2 方案一 3 方案二 4 方案三 5 目前采用方案三 场景 用户将实例里的root密码修改了,/root/.ssh/的公钥文件删除了,然后把密码忘记了,需要 ...

  8. linux终端快捷键

    tab 补全 ctrl+k 删除此处到末尾所有内容 ctrl+u 删除此处到开始所有内容 ctrl+a 将光标移至开始处 cttl+e 将光标移至结尾 ctrl+l 清屏 ctrl+c 删除整行或停止 ...

  9. 鸟哥的linux私房菜之档案与文件系统的压缩与打包

    00000001 节约空间 其实简单的说压缩就是把没有用到的0给去掉,解压的时候在加上 在linux中,压缩文件档案的扩展名大多是.tar,.tar.gz,tgz,gz,.Z,.bz2 compres ...

  10. python生成数据库中所有表的DESC描述

    在数据库设计完成之后, 常常需要在 wiki 或其他文档中保存一份数据库中所有表的 desc 描述, 尤其是每个字段的含义和用途. 手动去生成自然是不可取的. 因此, 我编写了一个简单的 python ...