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. java中创建线程的几种方法及区别

    1,实现Runnable接口创建线程 特点: A:将代码和数据分开,形成清晰的模型 B:线程体run()方法所在的类可以从其它类中继承一些有用的属性和方法 C:有利于保持程序风格的一致性 2,继承Th ...

  2. Mysql索引总结(一)

    数据库开发中索引的使用占了很重要的位置,好的索引会使数据库的读写效率加倍,烂的索引则会拖累整个系统甚至引发灾难. 索引分三类: index ----普通的索引,数据可以重复 unique ----唯一 ...

  3. mydetails-yii1

    1.yii验证码多余的get a new code ,即使在main.php中配置了中文也是出现获取新图片,影响效果 需要把 <?php $this->widget('CCaptcha') ...

  4. Limit the query running time with Resource limit facility (RLF)

    If you need to limit the query(package,plan) running time, but the JCL/JOB TIME parameters doesn't w ...

  5. 薛非《品悟C-抛弃C程序设计中的谬误与恶习》读后感part1【转】

    薛非<品悟C-抛弃C程序设计中的谬误与恶习>读后感part1 作者:宝贝孙秀楠﹣大连程序员 发表于2012年10月5日由admin 出处:http://sunxiunan.com/?p=2 ...

  6. MFC 进度条控件

    1.进度条 主要用来进行数据读写.文件拷贝和磁盘格式等操作时的工作进度提示情况,如安装程序等,伴随工作进度的进展,进度条的矩形区域从左到右利用当前活动窗口标题条的颜色来不断填充. 2.进度条控制在MF ...

  7. hibernate笔记03

  8. Ceph的客户端丢失文件夹的解决办法

    原来的解决办法 更新linux内核,使用linux内核级的mount方式,一段时间后将会在客户端看不到部分长期不使用的文件夹 更正后的解决办法 参考Ceph的客户端安装设置ceph-fuse方式挂载c ...

  9. Robotium Recorder的初试

    一.安装 资料来自官方 Prerequisites: Install the Java JDK. Install the Android SDK. The ADT bundle with Eclips ...

  10. Asp.net的post提交方式

    //建立WebRequest对象,url目标地址HttpWebRequest req =(HttpWebRequest)WebRequest.Create(url); //将LoginInfo转换为b ...