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
 
 
此题的思路在于这儿....看图...
 
 /*快速矩@ coder Gxjun*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
__int64 ma[][]; //matrix
__int64 ans[][];
void init(__int64 a[][])
{
int i;
for(i=;i<;i++)
a[i][i]=; //E 设置为1
a[][]=;
a[][]=;
}
int main()
{
int a,b,m,n,i,j,k;
__int64 temp[] ;
__int64 hop[][];
while(scanf("%d%d%d%d",&a,&b,&n,&m)!=EOF)
{
if(n==)
{
printf("%I64d\n",(*a)%m);
continue;
}
else if(n==)
{
printf("%I64d\n",(*(a*a+b))%m);
continue;
}
n-=;
init(ans);
/*init(ma);*/
ma[][]=(*a)%m;
ma[][]=(b-a*a)%m;
ma[][]=;
ma[][]=;
while(n>)
{
if(n&)
{
for(k=;k<;k++)
{
memset(temp,,sizeof(temp));
for(i=;i<;i++)
{
for(j=;j<;j++)
{
temp[i]+=ans[k][j]*ma[j][i];
temp[i]%=m;
}
}
ans[k][]=temp[];
ans[k][]=temp[];
}
n--;
continue;
}
memset(hop,,sizeof(hop));
for(k=;k<;k++)
{
for(i=;i<;i++)
{
for(j=;j<;j++)
{
hop[k][i]+=ma[k][j]*ma[j][i];
hop[k][i]%=m;
}
}
}
for(i=;i<;i++)
{
for(j=;j<;j++)
{
ma[i][j]=hop[i][j];
}
}
n>>=;
}
__int64 gong=(*((ans[][]*a)%m+(ans[][]*(a*a+b)%m)%m)%m)%m;
if(gong<) gong=m+gong;
printf("%I64d\n",gong);
}
return ;
}

HDUOJ--4565 So Easy!的更多相关文章

  1. HDU 4565 So Easy!(矩阵)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4565 题意: 题意: #include <iostream>#include <cs ...

  2. HDU 4565 So Easy!

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

  3. 数学(矩阵乘法):HDU 4565 So Easy!

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

  4. HDU 4565 So Easy(矩阵解公式)

    So Easy [题目链接]So Easy [题目类型]矩阵解公式 &题解: 感觉这种类型的题都是一个套路,这题和hdu 2256就几乎是一样的. 所以最后2Xn就是答案 [时间复杂度]\(O ...

  5. HDU 4565 So Easy!(公式化简+矩阵)

    转载:http://www.klogk.com/posts/hdu4565/ 这里写的非常好,看看就知道了啊. 题意很easy.a,b,n都是正整数.求 Sn=⌈(a+b√)n⌉%m,(a−1)2&l ...

  6. HDU 4565 So Easy!(数学+矩阵快速幂)(2013 ACM-ICPC长沙赛区全国邀请赛)

    Problem Description A sequence Sn is defined as:Where a, b, n, m are positive integers.┌x┐is the cei ...

  7. HDU 4565 So Easy! 数学 + 矩阵 + 整体思路化简

    http://acm.hdu.edu.cn/showproblem.php?pid=4565 首先知道里面那个东西,是肯定有小数的,就是说小数部分是约不走的,(因为b限定了不是一个完全平方数). 因为 ...

  8. hdu 4565 So Easy! (共轭构造+矩阵快速幂)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4565 题目大意: 给出a,b,n,m,求出的值, 解题思路: 因为题目中出现了开根号,和向上取整后求 ...

  9. HDU 4565 So Easy! 广义斐波拉数 数论 (a+sqrt(b))^n%mod 模板

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

  10. 【构造共轭函数+矩阵快速幂】HDU 4565 So Easy! (2013 长沙赛区邀请赛)

    [解题思路] 给一张神图,推理写的灰常明白了,关键是构造共轭函数,这一点实在是要有数学知识的理论基础,推出了递推式,接下来就是矩阵的快速幂了. 神图: 给个大神的链接:构造类斐波那契数列的矩阵快速幂 ...

随机推荐

  1. 《Windows核心编程》第3章——handle复制相关实验

    先写一个程序,用来查看进程的内核对象,这样我们就能比较子进程是否继承了父进程的某个句柄: #include <windows.h> #include <stdio.h> #de ...

  2. nginx配置location总结

    location匹配顺序 "="前缀指令匹配,如果匹配成功,则停止其他匹配 普通字符串指令匹配,顺序是从长到短,匹配成功的location如果使用^~,则停止其他匹配(正则匹配) ...

  3. 用java查询HBase中某表的一批数据

    java代码如下: package db.query; import java.io.IOException; import org.apache.hadoop.conf.Configuration; ...

  4. @JVM中的几种垃圾收集算法

    标记-清除(Mark-Sweep) 算法分为"标记"和"清除"两个阶段:首先标记出所有需要回收的对象,在标记完成后统一回收掉所有被标记的对象(没有与GC Roo ...

  5. [5] 柱台(Cylinder)图形的生成算法

    顶点数据的生成 bool YfBuildCylinderVertices ( Yreal topRadius, Yreal bottomRadius, Yreal height, Yuint slic ...

  6. Android之WifiManager

    移动设备离不开网络,android平台中在包android.net.wifi下提供了一些类专门用于管理设备的Wifi功能.该包下主要存在如下几个类: 1.  ScanResult:主要用来描述通过Wi ...

  7. Android之属性动画(一)

    一.概述 Android平台中常用的动画主要有两类,一类是View动画,一类是3.0后新增的属性动画.属性动画与View动画相比功能更加强大,主要体现在以下两个方面: 1.  属性动画不仅仅能应用到V ...

  8. sublime uable to read project

    在我用Sublime Text打开工程时出现这种情况: Unable to read project <path>/<project>.sublime-project这种错误对 ...

  9. [转] 公司局域网中代码访问 github.com

    一. 文档清晰 tencent同事.推荐 https://www.chenyudong.com/archives/use-git-or-github-in-company-local-net.html

  10. 4. Add override methods to class

    1. In the class, right click 2. "Scource" 3. "Override / Implement Menthods" 4. ...