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. zzulioj--1600--直线与圆(简单数学几何)

     1600: 直线与圆 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 360  Solved: 73 SubmitStatusWeb Board ...

  2. [JZOJ 5888] [NOIP2018模拟9.29] GCD生成树 解题报告 (最大生成树+公约数)

    题目链接: http://172.16.0.132/senior/#main/show/5888 题目: 题解: 思路是这样的:两个数的最大公约数一定不会比这两个数的任意一个数大.因此我们把权值相等的 ...

  3. java 后台实现ajax post跨域请求传递json格式数据获取json数据问题

    参考大神:http://blog.csdn.net/chunqiuwei/article/details/19924821 java后台: public String ajaxProxy(Intege ...

  4. gui编程实战——qq聊天界面1

    public class testDemo_2 extends JFrame{ JTextArea jta=null; //多行文本框组件 JScrollPane jsp=null; //滚动文本框 ...

  5. pic16F1938

    1.中断自动保存寄存器:W.STATUS.BSR.FSR和PCLATH,而且如果中断中需要改变这些寄存器,在Bank31中修改这些寄存器的影子寄存器即可. 2.RAM有1024字节,分为N个bank, ...

  6. swift语言点评十-Value and Reference Types

    结论:value是拷贝,Reference是引用 Value and Reference Types Types in Swift fall into one of two categories: f ...

  7. MySQL数据库学习记录

    SELECT子句顺序

  8. iF.svnadmin 安装遇到的坑

    iF.svnadmin 官网:http://svnadmin.insanefactory.com/ 安装配置iF.svnadmin : http://blog.linhere.com/archives ...

  9. (转)Java 虚拟机体系结构

    来源:http://hxraid.iteye.com/blog/676235 众所周知,Java源代码被编译器编译成class文件.而并不是底层操作系统可以直接执行的二进制指令(比如Windows O ...

  10. vue项目input的placeholder根据用户的选择改变

    html部分 <el-input :placeholder="holder" v-model="searchKey"> <el-select ...