HDU 3292 No more tricks, Mr Nanguo(佩尔方程,矩阵快速幂)

2024-05-29 07:32

本文主要是介绍HDU 3292 No more tricks, Mr Nanguo(佩尔方程,矩阵快速幂),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目: LINK

题意可以理解为: y^2 - n*x^2 == 1 已知n,求(x, y)的第k小的解.
这个式子可以用佩尔方程定理来解,可以把用到的前29个最小的解先打表。至于求第k小解,k比较大,可以用矩阵快速幂来做。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define INF 1000000000
typedef __int64 LL;
#define mod 8191
LL n, k;
LL a[44][2] = {-1,-1,3,2,2,1,-1,-1,9,4,5,2,8,3,3,1,-1,-1,19,6,10,3,7,2,649,180,15,4,4,1,-1,-1,33,8,17,4,170,39,9,2,55,12,197,42,24,5,5,1,-1,-1,51,10,26,5,127,24,9801,1820};
struct node
{LL mat[4][4];
};
node mul(node x, node y)
{node ret;memset(ret.mat, 0, sizeof(ret.mat));for(int i=1; i<=2; i++) {for(int j=1; j<=2; j++) {for(int k = 1; k<=2; k++) {ret.mat[i][j] += x.mat[i][k]*y.mat[k][j];ret.mat[i][j] %= mod;}}}return ret ;
}
node pow_(node x, LL y)
{node ret ;ret.mat[1][1]= ret.mat[2][2]= 1;ret.mat[1][2]= ret.mat[2][1]= 0;while(y) {if(y&1) {ret = mul(ret, x);}y>>=1;x = mul(x, x);}return ret;
}
int main()
{
#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGEwhile(scanf("%I64d%I64d", &n, &k) != EOF) {if(a[n-1][0] == -1) {printf("No answers can meet such conditions\n");continue;}node ans;ans.mat[1][1]= a[n-1][0]; ans.mat[1][2] = a[n-1][1];ans.mat[2][1]= a[n-1][1]*n; ans.mat[2][2]= a[n-1][0];ans = pow_(ans, k-1);LL out = 0;out += a[n-1][0] * ans.mat[1][1];out %= mod;out += a[n-1][1] * ans.mat[2][1];out %= mod;printf("%I64d\n", out);}return 0;
}


这篇关于HDU 3292 No more tricks, Mr Nanguo(佩尔方程,矩阵快速幂)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1013029

相关文章

SpringBoot快速接入OpenAI大模型的方法(JDK8)

《SpringBoot快速接入OpenAI大模型的方法(JDK8)》本文介绍了如何使用AI4J快速接入OpenAI大模型,并展示了如何实现流式与非流式的输出,以及对函数调用的使用,AI4J支持JDK8... 目录使用AI4J快速接入OpenAI大模型介绍AI4J-github快速使用创建SpringBoot

使用Python快速实现链接转word文档

《使用Python快速实现链接转word文档》这篇文章主要为大家详细介绍了如何使用Python快速实现链接转word文档功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 演示代码展示from newspaper import Articlefrom docx import

shell脚本快速检查192.168.1网段ip是否在用的方法

《shell脚本快速检查192.168.1网段ip是否在用的方法》该Shell脚本通过并发ping命令检查192.168.1网段中哪些IP地址正在使用,脚本定义了网络段、超时时间和并行扫描数量,并使用... 目录脚本:检查 192.168.1 网段 IP 是否在用脚本说明使用方法示例输出优化建议总结检查 1

Rust中的Option枚举快速入门教程

《Rust中的Option枚举快速入门教程》Rust中的Option枚举用于表示可能不存在的值,提供了多种方法来处理这些值,避免了空指针异常,文章介绍了Option的定义、常见方法、使用场景以及注意事... 目录引言Option介绍Option的常见方法Option使用场景场景一:函数返回可能不存在的值场景

电脑桌面文件删除了怎么找回来?别急,快速恢复攻略在此

在日常使用电脑的过程中,我们经常会遇到这样的情况:一不小心,桌面上的某个重要文件被删除了。这时,大多数人可能会感到惊慌失措,不知所措。 其实,不必过于担心,因为有很多方法可以帮助我们找回被删除的桌面文件。下面,就让我们一起来了解一下这些恢复桌面文件的方法吧。 一、使用撤销操作 如果我们刚刚删除了桌面上的文件,并且还没有进行其他操作,那么可以尝试使用撤销操作来恢复文件。在键盘上同时按下“C

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :