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. php利用href进行页面传值的正确姿势

    首先在a.php中 <?php $a = "world"; echo "<a href='b.php?m=$a'>删除</a>"; ...

  2. bzoj1022: [SHOI2008]小约翰的游戏John(博弈SG-nim游戏)

    1022: [SHOI2008]小约翰的游戏John 题目:传送门 题目大意: 一道反nim游戏,即给出n堆石子,每次可以取完任意一堆或一堆中的若干个(至少取1),最后一个取的LOSE  题解: 一道 ...

  3. ios修改了coredata数据结构后,更新安装会闪退

    如果iOS App 使用到CoreData,并且在上一个版本上有数据库更新(新增表.字段等操作),那在覆盖安装程序时就要进行CoreData数据库的迁移,具体操作如下: 1.选中你的mydata.xc ...

  4. hdoj--1260--Tickets(简单dp)

    Tickets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  5. 关于markdown的使用

    首先: https://www.cnblogs.com/jordangong/p/9804777.html 注意:提交博客时需将 Markdown 源码粘贴到编辑器中,且编辑器没有实时预览,可以上传后 ...

  6. POJ 3268 Dijkstra+priority_queue或SPFA

    思路:正向建边,一遍Dijkstra,反向建边,再一遍Dijkstra.ans加在一起输出最大值. (SPFA也行--) // by SiriusRen #include <queue> ...

  7. 解决局域网内无法IP访问IIS已发布的网站

    在IIS上发布的网站,本地可以访问,但是局域网内其他电脑却访问不了,原来是防火墙的问题,关闭它就可以访问了. 上面是我的简单操作 后来又百度了一下,发现有个更详细的操作:http://jingyan. ...

  8. 【Java】新建的web项目的jsp页面报错的解决方法

    一.错误信息 当用Eclipse新建web项目后,在里面建个jsp页面可能出现如下图的错误,jsp文件有一个红叉: 主要原因是:依赖tomcat类库 解决方案:项目右击—>build path ...

  9. 安卓怎么不如ios运行流畅

    一.优先级别不同:iOS最先响应屏幕当我们使用iOS或者是Android手机时,第一步就是滑屏解锁找到相应程序点击进入.而这个时候往往是所有操控开始的第一步骤,iOS系统产品就表现出来了流畅的一面,但 ...

  10. SpringBoot学习笔记(15)----SpringBoot使用Druid

    直接访问Druid官网wiki,有详细教程,地址如下: SpringBoot支持Druid地址:https://github.com/alibaba/druid/tree/master/druid-s ...