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强制类型转换

    object对象转换为String的一些总结 ----------------------------------------------------------------------------- ...

  2. 入门训练 A+B问题

    http://lx.lanqiao.org/problemset.page?code=BEGIN-&userid=34549   入门训练 A+B问题   时间限制:1.0s   内存限制:2 ...

  3. Hibernate的关系配置

    一对一: <hibernate-mapping> <class name="cn.jnit.bean.User" table="T_user" ...

  4. 深度解析 Java 内存原型

    一.Java 虚拟机内存原型 寄存器:我们在程序中无法控制. 栈:存放基本类型的数据和对象的引用,但对象本身不存放在栈中,而是存放在 堆中. 堆:存放用 new 产生的数据. 静态域:存放在对象中用  ...

  5. C#访问PostGreSQL数据库的方法 http://www.jb51.net/article/35643.htm

    这次的项目中的一个环节要求我把PostGreSQL数据取出来,然后放到SqlServer里,再去处理分析. http://www.jb51.net/article/35643.htm - 我对Post ...

  6. 大数据量的csv文件如何导入到 sql 数据库

    BULK INSERT dbo.T_test001 FROM 'E:\bus_20160316\bus全量评级及借款编号_20160316.csv' WITH ( FIELDTERMINATOR =' ...

  7. RMAN连接数据库

    连接本地数据库: 方法1: C:\Documents and Settings\Administrator>set oracle_sid=jssweb C:\Documents and Sett ...

  8. ADB server didn't ACK的解决方法

    异常信息如下: C:\Users\Administrator>adb devices* daemon not running. starting it now on port 5037 *ADB ...

  9. Linux kernel的 Makefile和Kconfig以及Make menuconfig的关系【转】

    本文转载自:http://blog.sina.com.cn/s/blog_4ba5b45e0102e6vp.html 熟悉内核的Makefile对开发设备驱动.理解内核代码结构都是非常重要的linux ...

  10. 【secureCRT】如何在secureCRT上设置常用的快捷输出按钮栏

    转自:http://jingyan.baidu.com/article/5d6edee2f32de199eadeec25.html 方法/步骤   打开secureCRT软件,单击菜单栏上面的[查看] ...