Time Limit: 10 second
Memory Limit: 2 MB
问题描述
已知m,n为整数,且满足下列两个条件:
1、m、n∈{1,2…,k},即1≤m,n≤k;
2、(n^2-m*n-m^2)^2=1。
编程输入正整数k(1≤k≤10^9),求一组满足上述两个条件的m,n,并且使得n^2+m^2的值最大。
Input
输入正整数k
Output
输出满足最大值的m和n
Sample Input
100
Sample Output
m=55
n=89
【题目链接】:http://noi.qz5z.com/viewtask.asp?id=9308
【题解】
(n^2-m*n-m^2)^2=1
(m^2+m*n-n^2)^2=1
(m^2+2*m*n+n^2-m*n-2*n^2)^2=1
(m^2+2*m*n+n^2-m*n-n^2-n^2)^2=1
(m^2+2*m*n+n^2-n*(m+n)-n^2)^2=1
((m+n)^2-n*(m+n)-n^2)^2=1
则令n’=(m+n),m’=n;
(n’^2-m’*n’-m’^2)^2=1
所以如果n和m是符合要求的解;
则n=(m+n)和m=n也是方程的解;
而由观察可知n=1,m=1是方程的解
所以n=2,m=1也是方程的解;
则n=3;m=2也是方程的解。。
就是斐波那契数列了;
递推下就好;
【完整代码】
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stack>
#include <string>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se secondtypedef pair<int,int> pii;
typedef pair<LL,LL> pll;void rel(LL &r)
{r = 0;char t = getchar();while (!isdigit(t) && t!='-') t = getchar();LL sign = 1;if (t == '-')sign = -1;while (!isdigit(t)) t = getchar();while (isdigit(t)) r = r * 10 + t - '0', t = getchar();r = r*sign;
}void rei(int &r)
{r = 0;char t = getchar();while (!isdigit(t)&&t!='-') t = getchar();int sign = 1;if (t == '-')sign = -1;while (!isdigit(t)) t = getchar();while (isdigit(t)) r = r * 10 + t - '0', t = getchar();r = r*sign;
}//const int MAXN = x;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);LL sqr(LL x)
{return x*x;
}LL k;int main()
{// freopen("F:\\rush.txt","r",stdin);cin >> k;LL a = 1,b = 1,c;while ((a+b)<=k){c = a+b;a = b;b = c;}cout << "m="<<a<<endl;cout << "n="<<b<<endl;return 0;
}