No more tricks, Mr Nanguo

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

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
依题可得x^2-ny^2=1,所以此题解法为佩尔方程+矩阵快速幂
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#pragma comment(linker, "/stck:1024000000,1024000000")
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.1415926535897932384626433832
#define ios() ios::sync_with_stdio(true)
#define INF 0x3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll n,k,x,y;
const ll maxn=;
struct matrix
{
ll a[][];
};
void serach(ll n,ll &x,ll &y)
{
y=;
while()
{
x=(1ll)*sqrt(n*y*y+);
if(x*x-n*y*y==) break;
y++;
}
}
matrix mulitply(matrix ans,matrix pos)
{
matrix res;
memset(res.a,,sizeof(res.a));
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
for(int k=;k<;k++)
{
res.a[i][j]+=(ans.a[i][k]*pos.a[k][j])%maxn;
res.a[i][j]%=maxn;
}
}
}
return res;
}
matrix quick_pow(ll m)
{
matrix ans,pos;
for(int i=;i<;i++)
for(int j=;j<;j++)
ans.a[i][j]=(i==j);
pos.a[][]=x%maxn;
pos.a[][]=n*y%maxn;
pos.a[][]=y%maxn;
pos.a[][]=x%maxn;
while(m)
{
if(m&) ans=mulitply(ans,pos);
pos=mulitply(pos,pos);
m>>=;
}
return ans;
}
int main()
{
while(scanf("%lld%lld",&n,&k)!=EOF)
{
ll m=sqrt(n);
if(m*m==n) {printf("No answers can meet such conditions\n");continue;}
serach(n,x,y);
matrix ans=quick_pow(k);
printf("%lld\n",ans.a[][]);
}
return ;
}

hdu 3292 No more tricks, Mr Nanguo的更多相关文章

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

    快速幂模+佩尔方程 #include <iostream> #include <cstdio> #include <algorithm> #include < ...

  4. hdu3293(pell方程+快速幂)

    裸的pell方程. 然后加个快速幂. No more tricks, Mr Nanguo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: ...

  5. 多校3- RGCDQ 分类: 比赛 HDU 2015-07-31 10:50 2人阅读 评论(0) 收藏

    RGCDQ Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practic ...

  6. HDU 3634 City Planning (离散化)

    City Planning Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  7. hdu 3624 City Planning(暴力,也可扫描线)

    City Planning Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  8. 多校赛3- Painter 分类: 比赛 2015-07-29 19:58 3人阅读 评论(0) 收藏

    D - Painter Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status P ...

  9. 每日英语:Best Ways to Ramp Up to A Marathon

    For the record number of American runners who completed an official race event last year, the questi ...

随机推荐

  1. poj1363——Rails

    Description There is a famous railway station in PopPush City. Country there is incredibly hilly. Th ...

  2. node08---Express框架

    一.Express框架 Express框架是后台的Node框架,所以和jQuery.zepto.yui(雅虎的).bootstrap都不一个东西. Express在后台的受欢迎的程度,和jQuery一 ...

  3. matplotlib 可视化 —— matplotlib.patches

    官方帮助文档 patches - Matplotlib 1.5.1 documentation patches 下主要包含的常用图形类有: Eclipse Circle Wedge 1. plt.gc ...

  4. 15-11-23:system指令

    CMD命令:开始->运行->键入cmd或command(在命令行里可以看到系统版本.文件系统版本) 1. appwiz.cpl:程序和功能 2. calc:启动计算器 3. certmgr ...

  5. ApacheFlink简介

    对无界数据集的连续处理 在我们详细介绍Flink之前,让我们从更高的层面上回顾处理数据时可能遇到的数据集的类型以及您可以选择处理的执行模型的类型.这两个想法经常被混淆,清楚地区分它们是有用的. 首先, ...

  6. jqGrid多级表格的实现

    原博主链接:http://blog.csdn.net/dreamstar613/article/details/54616503 jqGrid多级表格(可N级) 主要用的方法: subGridRowE ...

  7. 使用JS方法使页面滚动到指定元素+优化+API介绍(动画)

    前言 当页面最上部有顶部菜单是,使用锚点跳转的方法很容易挡住想要呈现的内容(如下图技能两个字被挡住了一半),为避免出现这样的问题,故滚动到指定元素使用用JS的方法来实现. 目录 使用的API简介 初版 ...

  8. Glidar: 一个基于OpenGL的开源实时3D传感器仿真器

    1 简介 这篇博文将介绍一个简单易用的3D传感器仿真器,可以用来模拟Lidars,立体视觉,基于时间飞行技术的ToF相机和微软的Kinect实时产生3D点云数据.Glidar仿真器并不是针对特定的某一 ...

  9. while循环,格式化输出%,运算符,数据类型的转换,编码的初识,

    1.内容总览 while循环 格式化输出 运算符 and or not 编码的初识 2. 具体内容 while 循环 where:程序中:你需要重复之前的动作,输入用户名密码时,考虑到while循环. ...

  10. 【BZOJ4071】【APIO2015】巴邻旁之桥

    题意: Description 一条东西走向的穆西河将巴邻旁市一分为二,分割成了区域 A 和区域 B. 每一块区域沿着河岸都建了恰好 1000000001 栋的建筑,每条岸边的建筑都从 0 编号到 1 ...