码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 3292

时间:2018-05-29 01:42:49      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:tor   input   lis   eof   content   lld   comm   cas   cst   

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=8191;
struct matrix
{
    ll a[2][2];
};
void serach(ll n,ll &x,ll &y)
{
    y=1;
    while(1)
    {
        x=(1ll)*sqrt(n*y*y+1);
        if(x*x-n*y*y==1) break;
        y++;
    }
}
matrix mulitply(matrix ans,matrix pos)
{
    matrix res;
    memset(res.a,0,sizeof(res.a));
    for(int i=0;i<2;i++)
    {
        for(int j=0;j<2;j++)
        {
            for(int k=0;k<2;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=0;i<2;i++)
        for(int j=0;j<2;j++)
            ans.a[i][j]=(i==j);
    pos.a[0][0]=x%maxn;
    pos.a[0][1]=n*y%maxn;
    pos.a[1][0]=y%maxn;
    pos.a[1][1]=x%maxn;
    while(m)
    {
        if(m&1) ans=mulitply(ans,pos);
        pos=mulitply(pos,pos);
        m>>=1;
    }
    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[0][0]);
    }
    return 0;
}

 

hdu 3292

标签:tor   input   lis   eof   content   lld   comm   cas   cst   

原文地址:https://www.cnblogs.com/shinianhuanniyijuhaojiubujian/p/9102987.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!