HDU 3292(No more tricks, Mr Nanguo 佩尔方程矩阵快速幂求解)

本文探讨了佩尔方程在解决古代音乐乐队分组问题中的应用,通过数学方法揭示了乐队成员数量与方阵排列之间的关系,深入解析了故事背景与数学原理,提供了解决方案并给出AC代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

No more tricks, Mr Nanguo

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


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
 

题目大意:

转化为方程等价于求方程X^2 - N * Y ^ 2 = 1.

解题思路:

若N是完全平方数,则肯定无解,否则用暴力法求出方程的特解,然后根据佩尔方程矩阵快速幂求法得解。


AC代码:

#include<iostream>
#include<cmath>
using namespace std;

int n;
int x,y;
typedef long long ll;
const int maxn = 4;
const int mod = 8191;

typedef struct
{
    int m[maxn][maxn];    
}Matrax;

Matrax a,per;

void sovle() //暴力法求特解 ,x,y为特解 
{
    y = 1;
    while(1)
    {
        x = (ll)sqrt(n * y * y  + 1);
        if(x * x - n * y * y == 1)
        {
            break;
        }
        y++;
    }
}

void init() //矩阵快速幂初始化 
{
    int i,j;
    a.m[0][0] = x % mod;
    a.m[0][1] = n * y % mod;
    a.m[1][0] = y % mod;
    a.m[1][1] = x % mod;
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            per.m[i][j] = (i == j);
        }
    }
}

Matrax multi(Matrax a,Matrax b) //矩阵乘法 
{
    Matrax c;
    int k,i,j;
    for(i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            c.m[i][j] = 0; //初始化 
            for(k=0;k<2;k++)
            {
                c.m[i][j] += a.m[i][k] * b.m[k][j];
            }
            c.m[i][j] %= mod;
        }
    }
    return c;
}

Matrax power(int k) //矩阵乘方 
{
    Matrax p,ans = per;
    p = a;
    while(k)
    {
        if(k & 1)
        {
            ans = multi(ans,p);
            k--;    
        }    
        k /= 2;
        p = multi(p,p);
    }    
    return ans;
}

int main()
{
    int k;
    //freopen("1.txt","r",stdin);
    while(scanf("%d%d",&n,&k) != EOF)
    {
        int yi = sqrt(n + 0.0); 
        if(yi * yi == n) //完全平方数不满足使用佩尔方程 
        {
            printf("No answers can meet such conditions\n");
            continue;
        }
        sovle(); //求一组特解x,y 
        init(); //初始化矩阵快速幂 
        a = power(k-1); //k - 1次方 
        x = (a.m[0][0] * x % mod + a.m[0][1] * y % mod) % mod; //求x 
        printf("%d\n",x);    
    } 
    return 0;
}



### HDU 2544 题目分析 HDU 2544 是关于最短路径的经典问题,可以通过多种方法决,其中包括基于邻接矩阵的 Floyd-Warshall 算法。以下是针对该问题的具体答。 --- #### 基于邻接矩阵的 Floyd-Warshall 实现 Floyd-Warshall 算法是一种动态规划算法,适用于计算任意两点之间的最短路径。它的时间复杂度为 \( O(V^3) \),其中 \( V \) 表示节点的数量。对于本题中的数据规模 (\( N \leq 100 \)),此算法完全适用。 下面是具体的实现方式: ```cpp #include <iostream> #include <algorithm> using namespace std; const int INF = 0x3f3f3f3f; int dist[105][105]; int n, m; void floyd() { for (int k = 1; k <= n; ++k) { // 中间节点 for (int i = 1; i <= n; ++i) { // 起始节点 for (int j = 1; j <= n; ++j) { // 结束节点 if (dist[i][k] != INF && dist[k][j] != INF) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } } } int main() { while (cin >> n >> m && (n || m)) { // 初始化邻接矩阵 for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (i == j) dist[i][j] = 0; else dist[i][j] = INF; } } // 输入边的信息并更新邻接矩阵 for (int i = 0; i < m; ++i) { int u, v, w; cin >> u >> v >> w; dist[u][v] = min(dist[u][v], w); dist[v][u] = min(dist[v][u], w); // 如果是有向图,则去掉这一行 } // 执行 Floyd-Warshall 算法 floyd(); // 输出起点到终点的最短距离 cout << (dist[1][n] >= INF ? -1 : dist[1][n]) << endl; } return 0; } ``` --- #### 关键点析 1. **邻接矩阵初始化** 使用二维数组 `dist` 存储每一对节点间的最小距离。初始状态下,设所有节点对的距离为无穷大 (`INF`),而同一节点自身的距离为零[^4]。 2. **输入处理** 对于每条边 `(u, v)` 和权重 `w`,将其存储至邻接矩阵中,并取较小值以防止重边的影响[^4]。 3. **核心逻辑** Floyd-Warshall 的核心在于三重循环:依次尝试通过中间节点优化其他两节点间的距离关系。具体而言,若从节点 \( i \) 到 \( j \) 可经由 \( k \) 达成更优,则更新对应位置的值[^4]。 4. **边界条件** 若最终得到的结果仍为无穷大(即无法连通),则返回 `-1`;否则输出实际距离[^4]。 --- #### 性能评估 由于题目限定 \( N \leq 100 \),因此 \( O(N^3) \) 的时间复杂度完全可以接受。此外,空间需求也较低,适合此类场景下的应用。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值