/*
HDU4565 So Easy!
http://acm.hdu.edu.cn/showproblem.php?pid=4565
数论 快速幂 矩阵快速幂
题意:求[(a+sqrt(b))^n ]%m [ ]代表向上取证
联想到斐波那契数列,所以第一感觉是矩阵快速幂
后来发现根本不用这么麻烦,我们认为a+b*sqrt(c)中的a为实部
b为虚部,就能直接用二元乘法模拟矩阵快速幂了,因此这两种方法
是等价的。于是乎,我们就解决了精度问题。
然而即使我们得出了结果中的a+b*sqrt(c)也不能这么算
因为[b*sqrt(c)]%m不和[b%m*sqrt(c)]%m同余,因此只能另辟蹊径。
注意到题目中的(a-1)^2< b < a^2 <=> 0<a-sqrt(b)<1
所以 0<(a+sqrt(b))^n<1 而 (a+sqrt(b))^n与(a-sqrt(b))^n是公轭的
所以其和只剩下了实部的二倍。因此只需求出实部,将其乘以2就是答案。
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <iostream>
#include <map>
#include <set>
#define test
using namespace std;
const int Nmax=;
//const long long mod=2147483647LL;
//double eps=1e-8;
long long a,b,n,m;
struct Num
{
long long a;
long long b;
long long c;
Num(long long _a,long long _b,long long _c)
{
a=_a;
b=_b;
c=_c;
}
friend Num operator * (Num x,Num y)
{
long long a1=x.a%m,b1=x.b%m,a2=y.a%m,b2=y.b%m,c=x.c;
long long a=(a1*a2%m+((c*b1*b2)%m))%m;
long long b=((a1*b2)%m+((b1*a2)%m))%m;
return Num(a,b,c);
}
friend Num qpow(Num base,long long n)
{
Num ans(1LL,0LL,base.c);
//ans.print();
while(n>0LL)
{
if(n&1LL)
ans=ans*base;
base=base*base;
n>>=;
//ans.print();
}
//printf("%lld %lld %lld\n",ans.a,ans.b,ans.c);
return ans;
}
void print()
{
printf("a:%lld b:%lld c:%lld\n",a,b,c);
}
}; void work()
{
Num base(a,1LL,b);
Num ans=qpow(base,n);
//ans.print();
long long a=ans.a,b=ans.b,c=ans.c;
//long long res=(long long)ceil(a+b*sqrt(c));//不能这么写,因为整数和小数直接不能模
//while(a<=0)
//a+=m;
//while(b<=0)
//b+=m;
//ans.print();
//long long res=(long long)ceil(a+b*sqrt(c));
//printf("res:%lld\n",res);
//res=res%m;
//printf("%lld %lld %ll\n",a,b);
//ans.print();
//if(2LL*a!=ceil(a+b*sqrt(c)))
//printf("NO\n");
//res=(2LL*a)%m;
//printf("ans:%lld myans:%lld\n",(2LL*a)%m,(long long)ceil(a+b*sqrt(c))%m);
long long res=2LL*a%m;
printf("%lld\n",res); }
int main()
{
#ifdef test
//freopen("hdu4565.in","r",stdin);
#endif
while(scanf("%lld%lld%lld%lld",&a,&b,&n,&m)==)
work();
return ;
}

HDU4565 So Easy!的更多相关文章

  1. HDU4565 So Easy! —— 共轭构造、二阶递推数列、矩阵快速幂

    题目链接:https://vjudge.net/problem/HDU-4565 So Easy! Time Limit: 2000/1000 MS (Java/Others)    Memory L ...

  2. hdu4565 So Easy! 矩阵快速幂

    A sequence Sn is defined as: Where a, b, n, m are positive integers.┌x┐is the ceil of x. For example ...

  3. HDU4565 So Easy! 矩阵高速幂外加数学

    easy 个屁啊,一点都不easy,题目就是要求公式的值,但是要求公式在最后的取模前的值向上取整.再取模,无脑的先试了高速幂 double  fmod来做,结果发现是有问题的.这题要做肯定得凑整数,凑 ...

  4. hdu4565 So Easy!(矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4565 题解:(a+√b)^n=xn+yn*√b,(a-√b)^n=xn-yn*√b, (a+√b)^n ...

  5. 【HDU4565】So Easy!

    [HDU4565]So Easy! 题面 要你求 \[ \lceil (a+\sqrt b)^n \rceil \% m \] 其中\(0<a,m<2^{15},(a-1)^2<b& ...

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

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

  7. 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 ...

  8. hdu4565---So Easy!(矩阵)

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

  9. 【转】Windows下使用libsvm中的grid.py和easy.py进行参数调优

    libsvm中有进行参数调优的工具grid.py和easy.py可以使用,这些工具可以帮助我们选择更好的参数,减少自己参数选优带来的烦扰. 所需工具:libsvm.gnuplot 本机环境:Windo ...

随机推荐

  1. Android和H5交互-基础篇

    hybrid App开发也不是什么新鲜事了,其中native和h5之间的交互则是必不可少的.Android中是如何和H5交互的? 1.webView加载页面 我们都知道在Android中是通过webV ...

  2. elasticsearch date_histogram

    (5)Date Histogram Aggregation 时间直方图聚合,专门对时间类型的字段做直方图聚合.这种需求是比较常用见得的,我们在统计时,通常就会按照固定的时间断(1个月或1年等)来做统计 ...

  3. spring boot测试

    今天在springside里试了spring boot,果然很方便,内置容器,不需要配置web.xml,简单几个文件就可以实现增删改查操作,一些配置如tomcat端口之类的直接写在applicatio ...

  4. kafka参数在线修改

    当kafka集群单个节点出现磁盘满了,需要清理历史topic数据:方法如下 1): 停掉kafka进程,将kafka的server.properties中的log.retention.hours=1/ ...

  5. BZOJ 1037 生日聚会 DP

    [ZJOI2008]生日聚会Party Time Limit: 10 Sec Memory Limit: 162 MB Description 今天是hidadz小朋友的生日,她邀请了许多朋友来参加她 ...

  6. 微信图片不可显示java解决方法

    先看知乎:https://www.zhihu.com/question/35044484 场景: 微信上传了图片素材,返回了图片url,然后不能在img标签中正常显示. 原因是微信做了图片防盗连接. ...

  7. pgsql 远程机器无法连接数据库报错处理方法

    因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本地localhost访问正常,在相同网段的远程机器访问报如下错误 “server closed the connection unexpe ...

  8. 【SQL】BETWEEN操作符

    BETWEEN 操作符在 WHERE 子句中使用,作用是选取介于两个值之间的数据范围. 操作符 BETWEEN ... AND 会选取介于两个值之间的数据范围.这些值可以是数值.文本或者日期. 注意: ...

  9. C# 不卡屏延时方法,延迟系统时间,但系统又能同时能执行其它任务

    //延迟系统时间,但系统又能同时能执行其它任务,不卡屏延时方法 public static void Delay(int milliSecond) { int start = Environment. ...

  10. 去除安卓apk中的广告

    一般来说,安卓应用很多免费的apk都是有广告的.尽管我们要坚持尊重开发者,帮帮他们点击广告赚钱来可持续发展,但是有的时候,很多游戏中游戏实在是太影响感觉了,当找不到汉化破解版本的时候,也许需要亲自把它 ...