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
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.
題目大意:
解題思路:
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;
}