快速幂模+佩尔方程

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
const int Mod=8191;
struct Matrax {
int m[3][3];
};
Matrax a,per; void slove(int d,int &x1,int &y1){
y1=1;
while(true){
x1=(int)sqrt(1.0*d*y1*y1+1);
if(x1*x1-d*y1*y1==1)
break;
y1++;
}
} Matrax multi(Matrax a,Matrax b){
Matrax c;
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
c.m[i][j]=0;
for(int k=0;k<2;k++)
c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j])%Mod;
}
}
return c;
} Matrax Power(int k){
Matrax ans=per,p=a;
while(k){
if(k&1){
ans=multi(ans,p);
}
k>>=1;
p=multi(p,p);
}
return ans;
} int main(){
int n,k,x1,y1,xt,yt,xn,yn;
while(scanf("%d%d",&n,&k)!=EOF){
int sq=(int)sqrt(n*1.0);
if(sq*sq==n){
printf("No answers can meet such conditions\n");
continue;
}
slove(n,x1,y1);
per.m[0][0]=per.m[1][1]=1;
per.m[0][1]=per.m[1][0]=0;
a.m[0][0]=x1; a.m[0][1]=n*y1;
a.m[1][0]=y1; a.m[1][1]=x1;
Matrax ans=Power(k-1);
int ansx=0;
ansx=(ans.m[0][0]*x1+ans.m[0][1]*y1)%Mod;
printf("%d\n",ansx);
}
return 0;
}

  

HDU 3292的更多相关文章

  1. HDU 3292 【佩尔方程求解 && 矩阵快速幂】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=3292 No more tricks, Mr Nanguo Time Limit: 3000/1000 M ...

  2. No more tricks, Mr Nanguo HDU - 3292(pell + 矩阵快速幂)

    No more tricks, Mr Nanguo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Jav ...

  3. hdu 3292 No more tricks, Mr Nanguo

    No more tricks, Mr Nanguo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Jav ...

  4. HDU 5643 King's Game 打表

    King's Game 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5643 Description In order to remember hi ...

  5. 转载:hdu 题目分类 (侵删)

    转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012. ...

  6. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  7. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  8. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  9. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

随机推荐

  1. ASIHTTPRequest框架的简单使用

    ASIHttpRequest是一款及其强劲的HTTP訪问开源项目.让简单的API完毕复杂的功能,如:异步请求,队列请求,GZIP压缩.缓存,断点续传,进度跟踪,上传文件.HTTP认证.在新版本号中,还 ...

  2. tflearn Training Step每次 We will run it for 10 epochs (the network will see all data 10 times) with a batch size of 16. n_epoch=10, batch_size=16

    Training TFLearn provides a model wrapper 'DNN' that can automatically performs a neural network cla ...

  3. Spring中JdbcTemplate中使用RowMapper

    转自:https://blog.csdn.net/u012661010/article/details/70049633 1 sping中的RowMapper可以将数据中的每一行数据封装成用户定义的类 ...

  4. 用dom4j解析xml文件并执行增删改查操作

    转自:https://www.aliyun.com/jiaocheng/1339446.html xml文件: <?xml version="1.0" encoding=&q ...

  5. Hdu-5992 2016ACM/ICPC亚洲区青岛站 K.Finding Hotels KDtree

    题面 题意:二维平面上有很多点,每个点有个权值,现在给你一个点(很多组),权值v,让你找到权值小于等于v的点中离这个点最近的,相同的输出id小的 题解:很裸的KDtree,但是查询的时候有2个小限制, ...

  6. 关于打包压缩几种格式(gzip,bzip2,xz)的试验对比

    要通过脚本进行备份,必然将会应用到压缩技术,这里简单针对几个常见的格式进行测验,从而得到一种合适的方式. 这里以一个应用目录做例子: [root@isj-test-5 mnt]$du -sh * 66 ...

  7. 一步步教会你微信小程序的登录鉴权

    前言 为了方便小程序应用使用微信登录态进行授权登录,微信小程序提供了登录授权的开放接口.乍一看文档,感觉文档上讲的非常有道理,但是实现起来又真的是摸不着头脑,不知道如何管理和维护登录态.本文就来手把手 ...

  8. MSDN性能分析相关网站收集

    Visual Studio 性能分析初学者指南 | Microsoft Docshttps://docs.microsoft.com/zh-cn/visualstudio/profiling/begi ...

  9. sphinx with discuz

    安装sphinx: sudo apt-get install sphinxsearch 配置: source discuz { type = mysql sql_host = xx.xx.xx.xx ...

  10. Linker scripts之SECTIONS

    1 Purpose The linker script describes how the sections in the input files should be mapped into the ...