裸的pell方程。 然后加个快速幂.

No more tricks, Mr Nanguo

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 320    Accepted Submission(s): 207

Problem Description
Now Sailormoon girls want to tell you a ancient idiom story named “be there just to make up the number”. The story can be described by the following words.
In the period of the Warring States (475-221 BC), there was a state called Qi. The king of Qi was so fond of the yu, a wind instrument, that he had a band of many musicians play for him every afternoon. The number of musicians is just a square number.Beacuse a square formation is very good-looking.Each row and each column have X musicians.
The king was most satisfied with the band and the harmonies they performed. Little did the king know that a member of the band, Nan Guo, was not even a musician. In fact, Nan Guo knew nothing about the yu. But he somehow managed to pass himself off as a yu player by sitting right at the back, pretending to play the instrument. The king was none the wiser. But Nan Guo's charade came to an end when the king's son succeeded him. The new king, unlike his father, he decided to divide the musicians of band into some equal small parts. He also wants the number of each part is square number. Of course, Nan Guo soon realized his foolish would expose, and he found himself without a band to hide in anymore.So he run away soon.
After he leave,the number of band is Satisfactory. Because the number of band now would be divided into some equal parts,and the number of each part is also a square number.Each row and each column all have Y musicians.
 
Input
There are multiple test cases. Each case contains a positive integer N ( 2 <= N < 29). It means the band was divided into N equal parts. The folloing number is also a positive integer K ( K < 10^9).
 
Output
There may have many positive integers X,Y can meet such conditions.But you should calculate the Kth smaller answer of X. The Kth smaller answer means there are K – 1 answers are smaller than them. Beacuse the answer may be very large.So print the value of X % 8191.If there is no answers can meet such conditions,print “No answers can meet such conditions”.
 
Sample Input
2 999888
3 1000001
4 8373
 
Sample Output
7181
600
No answers can meet such conditions
//
// main.cpp
// hdu3292
//
// Created by 陈加寿 on 15/12/1.
// Copyright (c) 2015年 陈加寿. All rights reserved.
// #include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <string>
#include <math.h>
using namespace std; #define MOD 8191 void matrix_mul(long long s[][],long long t[][])
{
long long tmp[][];
memset(tmp,,sizeof(tmp));
for(int i=;i<;i++)
for(int j=;j<;j++)
for(int k=;k<;k++)
tmp[i][j]=(tmp[i][j]+s[i][k]*t[k][j])%MOD;
for(int i=;i<;i++)
for(int j=;j<;j++)
s[i][j]=tmp[i][j];
} int main(int argc, const char * argv[]) {
long long n,k;
while(cin>>n>>k)
{
long long x=sqrt( (double)n );
if(x*x==n)
{
printf("No answers can meet such conditions\n");
continue;
}
//然后开始寻找第一个解
long long x0,y0;
for(long long i=;;i++)
{
x=sqrt((double)(i*i*n+));
if(x*x==i*i*n+)
{
x0=x;
y0=i;
break;
}
}
long long mat[][],ans[][];
mat[][]=x0; mat[][]=y0;
mat[][]=n*y0; mat[][]=x0;
memset(ans,,sizeof(ans));
ans[][]=; ans[][]=;
k-=;
while(k)
{
if(k&) matrix_mul(ans,mat);
k>>=;
matrix_mul(mat,mat);
}
long long ans1;
ans1 = (x0*ans[][]+y0*ans[][])%MOD;
cout<<ans1<<endl;
}
return ;
}

hdu3293(pell方程+快速幂)的更多相关文章

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

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

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

  3. hdu3483 A Very Simple Problem 非线性递推方程2 矩阵快速幂

    题目传送门 题目描述:给出n,x,mod.求s[n]. s[n]=s[n-1]+(x^n)*(n^x)%mod; 思路:这道题是hdu5950的进阶版.大家可以看这篇博客hdu5950题解. 由于n很 ...

  4. Pell方程及其一般形式

    一.Pell方程 形如x^2-dy^2=1的不定方程叫做Pell方程,其中d为正整数,则易得当d是完全平方数的时候这方程无正整数解,所以下面讨论d不是完全平方数的情况. 设Pell方程的最小正整数解为 ...

  5. POJ 1320 Street Numbers Pell方程

    http://poj.org/problem?id=1320 题意很简单,有序列 1,2,3...(a-1),a,(a+1)...b  要使以a为分界的 前缀和 和 后缀和 相等 求a,b 因为序列很 ...

  6. HDU 4471 矩阵快速幂 Homework

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...

  7. bzoj 2242: [SDOI2011]计算器 BSGS+快速幂+扩展欧几里德

    2242: [SDOI2011]计算器 Time Limit: 10 Sec  Memory Limit: 512 MB[Submit][Status][Discuss] Description 你被 ...

  8. CodeForces 185A 快速幂

    一开始找矩阵快速幂的题来做时就看到了这题,题意就是让你求出如图所示的第n个三角形中指向向上的小三角形个数.从图中已经很容易看出递推关系了,我们以f[n]表示第n个大三角形中upward的小三角形个数, ...

  9. [HDOJ2604]Queuing(递推,矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2604 递推式是百度的,主要是练习一下如何使用矩阵快速幂优化. 递推式:f(n)=f(n-1)+f(n- ...

随机推荐

  1. 设计模式之装饰器模式(PHP实现)

    /** * 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. * 这种模式创建了一个 ...

  2. java内存溢出分析工具:jmap使用实战

    在一次解决系统tomcat老是内存撑到头,然后崩溃的问题时,使用到了jmap. 1 使用命令 在环境是linux+jdk1.5以上,这个工具是自带的,路径在JDK_HOME/bin/下 jmap -h ...

  3. applicationContext.xml文件如何调用外部properties等配置文件

    只需要在applicationContext.xml文件中添加一行: <!-- 导入外部的properties文件 --> <context:property-placeholder ...

  4. Tomcat:Java Web服务器配置详解

    一.Tomcat概述 1.tomcat简介 tomcat是基于JDK的web服务器,其能运行Servlet和JSP规范总.Tomcat 5支持最新的Servlet 2.4 和JSP 2.0规范.实际上 ...

  5. UserAgent伪装浏览器

    经常逛论坛的朋友经常会遇到这样的问题:论坛个性签名里的JS代码把个人浏览器信息等被人一览无余,我并不想他们得到我的这类信息. 咋办?很简单的办法就是伪装,怎么伪装?对于chrome.firefox等这 ...

  6. Solr6.6.0 用 SimplePostTool索引文件

    一.背景介绍 Solr启动并运行之后,并不包含任何数据,在solr的安装目录下的bin目录中,有一个post工具,我们可以使用这个工具往solr上传数据,这个工具必须在命令行中执行,post工具是一个 ...

  7. 格式化HDFS

    格式化HDFS 查看hdfs-site.xml 将 dfs.namenode.name.dir和dfs.datanode.data.dir 目录中文件删除 <configuration> ...

  8. Linux学习之一-从三个重要人物的故事和一张思维导图说起

    Linux是一套自由加开放源代码的类Unix操作系统,诞生于1991年10月5日(第一次正式向外公布),由芬兰学生Linus Torvalds和后来陆续加入的众多爱好者共同开发完成. Linux是一个 ...

  9. 8.使用JPA保存数据【从零开始学Spring Boot】

    转载:http://blog.csdn.net/linxingliang/article/details/51636989 在看这一篇文档的话,需要先配置好JPA – hibernate. 总体步骤: ...

  10. 数据库设计--数据流图(DFD)

    1.数据流图的定义 数据流图(DFD)是结构化分析方法中使用的工具,它以图形的方式描绘数据在系统中流动和处理的过程, 因为它仅仅反映系统必须完毕的逻辑功能.所以它是一种功能模型. 在结构化开发方法中. ...