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 (Java/Others)
Total Submission(s): 576    Accepted Submission(s): 390

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

Author

B.A.C

Source

2010 “HDU-Sailormoon” Programming Contest

Recommend

lcy

pell方程的的入门题

求第k大

先求特解 然后根据

用矩阵快速幂就好了

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 8191
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 10010, INF = 0x7fffffff;
int tot = 1;
struct Matrix
{
    LL v[110][110];
    Matrix()
    {
        memset(v, 0, sizeof(v));
    }
    Matrix operator *(const Matrix B)
    {
        int i, j, k;
        Matrix C;
        for(i = 0; i <= tot; i ++)
            for(j = 0; j <= tot; j ++)
                for(k = 0; k <= tot; k ++)
                {
                    C.v[i][j] = (C.v[i][j] + v[i][k] * B.v[k][j]) % MOD;
                }
        return C;
    }
};

Matrix mtPow(Matrix A, int k)
{
    int i;
    Matrix B;
    for(i = 0; i <= tot; i ++)
    {
        B.v[i][i] = 1;
    }
    while(k)
    {
        if(k & 1) B = B * A;
        A = A * A;
        k >>= 1;
    }
    return B;
}

int main()
{
    LL n, k;
    while(cin >> n >> k)
    {
        LL t = sqrt(n);
        if(t * t == n)
        {
            cout << "No answers can meet such conditions" << endl;
            continue;
        }

        LL x, y = 1;
        while(1)
        {
            x = sqrt(n * y * y + 1);
            if(x * x == n * y * y + 1) break;
            y++;
        }
        Matrix A;
        A.v[0][0] = A.v[1][1] = x % MOD;
        A.v[0][1] = (n * y) % MOD, A.v[1][0] = y % MOD;
        Matrix B = mtPow(A, k - 1);
        cout << ((B.v[0][0] * x) % MOD + (B.v[0][1] * y) % MOD) % MOD << endl;

    }

    return 0;
}

原文地址:https://www.cnblogs.com/WTSRUVF/p/10294315.html

时间: 2024-11-14 13:37:13

No more tricks, Mr Nanguo HDU - 3292(pell + 矩阵快速幂)的相关文章

Hdu 4965(矩阵快速幂)

题目链接 Fast Matrix Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 87    Accepted Submission(s): 39 Problem Description One day, Alice and Bob felt bored again, Bob knows Alice is a

ZOJ 3690 &amp; HDU 3658 (矩阵快速幂+公式递推)

ZOJ 3690 题意: 有n个人和m个数和一个k,现在每个人可以选择一个数,如果相邻的两个人选择相同的数,那么这个数要大于k 求选择方案数. 思路: 打表推了很久的公式都没推出来什么可行解,好不容易有了想法结果WA到天荒地老也无法AC.. 于是学习了下正规的做法,恍然大悟. 这道题应该用递推 + 矩阵快速幂. 我们设F(n) = 有n个人,第n个人选择的数大于k的方案数: G(n) = 有n个人,第n个人选择的数小于等于k的方案数: 那么递推关系式即是: F(1)=m?k,G(1)=k F(n

HDU 1575 &amp;&amp; 1757 矩阵快速幂&amp;&amp;构造矩阵入门

HDU 1575 Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2912    Accepted Submission(s): 2167 Problem Description A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input 数据的第一行是一个T,表示有T组数据.每组

hdu 5667 Sequence 矩阵快速幂

题目链接:hdu 5667 Sequence 思路:因为fn均为a的幂,所以: 这样我们就可以利用快速幂来计算了 注意: 矩阵要定义为long long,不仅仅因为会爆,还会无限超时 要对a%p==0特判,以为可能出现指数%(p-1)==0的情况,那么在快速幂的时候返回的结果就是1而不是0了 /************************************************************** Problem:hdu 5667 User: youmi Language:

hdu 2276(矩阵快速幂)

Kiki & Little Kiki 2 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2301    Accepted Submission(s): 1176 Problem Description There are n lights in a circle numbered from 1 to n. The left of li

HDU - 2604 Queuing(矩阵快速幂或直接递推)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2604 题意:给出字符串长度L,并且字符串只由'f','m'构成,有2^L种情况,问在其中不包含'fmf','fff'的字符串有多少个. 1.直接递推,虽然过了,但是数据稍微大点就很可能TLE,因为代码上交上去耗时还是比较长的(感觉数据有点水)╭(′▽`)╭(′▽`)╯( 1 #include <iostream> 2 #include <cstdio> 3 #include <c

HDU 5667 Sequence(矩阵快速幂+费马小定理)

题意:不好复制,直接上链接http://acm.hdu.edu.cn/showproblem.php?pid=5667 思路: 观察递推式我们可以发现,所有的f_if?i??都是aa的幂次,所以我们可以对f_if?i??取一个以aa为底的loglog,即g_i=log_a\ f_ig?i??=log?a?? f?i?? 那么递推式变成g_i=b+c*g_{i-1}+g_{i-2}g?i??=b+c∗g?i−1??+g?i−2??,这个式子可以矩阵乘法 这题有一个小trick,注意a\ mod\

hdu 6198(矩阵快速幂)

number number number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 119 暴力发现当4 12 33 88 232 和斐波那契数列对比  答案为 第2*k+3个数减1 直接用矩阵快速幂求的F[2*k+3]  然后减1 A=1,B=0; 然后矩阵快速幂2*k

HDU 1575-Tr A(矩阵快速幂)

Tr A Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3169    Accepted Submission(s): 2367 Problem Description A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input 数据的第一行是一个T,表示有T组数据. 每组数据的第一行有