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. 关于SpringCloud微服务架构概念的一点理解

    目前微服务是非常火的架构或者说概念,也是在构建大型互联网项目时采用的架构方式. 1.单体架构单体架构,是指将开发好的项目打成war包,然后发布到tomcat等容器中的应用. 假设你正准备开发一款与Ub ...

  2. JQuery文件上传及以Base64字符串形式呈现图片

    一:上传之 首先,你必然得有一个 file input,如下: <td>     <img id="imgGif" style="display: no ...

  3. Jquery Mobile实例--利用优酷JSON接口读取视频数据

    本文将介绍,如何利用JqueryMobile调用优酷API JSON接口显示视频数据. (1)注册用户接口. 首页,到 http://open.youku.com 注册一个账户,并通过验证.然后找到A ...

  4. [每日一题] OCP1z0-047 :2013-08-04 INSERT --- WITH CHECK OPTION

    这题是考:insertWITH CHECK OPTION的用法 insert into (<select clause> WITH CHECKOPTION) values (...) 这样 ...

  5. Binary Tree Inorder Traversal leetcode java

    题目: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binar ...

  6. permission 权限 列表 6.0 运行时【案例】

    demo地址:https://github.com/baiqiantao/PermissionTest.git <uses-permission android:name="andro ...

  7. java 判断字符串是否相等 (转)

    http://blog.csdn.net/chtnj/article/details/7909720 判断字符串相等我们经常习惯性的写上if(str1==str2),这种写法在java中可能会带来问题 ...

  8. linux loop device介绍

    在Linux中,有一种特殊的块设备叫loop device,这种loop device设备是通过影射操作系统上的正常的文件而形成的虚拟块设备.因为这种设备的存在,就为我们提供了一种创建一个存在于其他文 ...

  9. https://leetcode-cn.com/

    https://leetcode-cn.com/ 码,马不停蹄,码不停题 英文版:https://leetcode.com/

  10. URL参数转换对象

    var parseQueryString = function (url) { var reg_url = /^[^\?]+\?([\w\W]+)$/, reg_para = /([^&=]+ ...