【vijos】1543 极值问题(数论+fib数)

https://vijos.org/p/1543

好神奇的一题。。

首先我竟然忘记n可以求根求出来,sad。

然后我打了表也发现n和m是fib数。。

严格证明(鬼知道为什么这样就能对啊,能代换怎么就能保证最大呢?):

(n^2-mn-m^2)^2=1

(m^2+mn-n^2)^2=1

(m(m+n)-n^2)^2=1

((m+n-n)(m+n)-n^2)^2=1

((m+n)^2-n(m+n)-n^2)^2=1

因为(n'^2-m'n'-m'^2)^2=1

取n'=m+n, m'=n使式子成立。。

所以每一次我们都能找到一个n'>n m'>m(假设了n>m),那么显然我们每次一直向上取,取到不能取为止

其实这题只需要找是否存在不是fib的数使得这个成立即可。

若n和m满足题意,那么gcd(n, m)=1,否则原式可以将d=gcd(n, m)提出来变为d*d(...)^2=1,此时(...)成了分数,显然不符合题意

而假设n>m,那么根据辗转相减有gcd(n, m)=gcd(m, n-m)

所以这样推下去,显然这些数就是fib数。

然后就是暴力就行了

#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const ll max(const ll &a, const ll &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }

ll k, a, b, ans;

int main() {
	read(k);
	ll n=1, m=0;
	while(n+m<=k) {
		ll c=m;
		m=n;
		n+=c;
	}
	printf("%lld\n", m*m+n*n);
	return 0;
}

 

 


 

 

背景

小铭的数学之旅2。

描述

已知m、n为整数,且满足下列两个条件:
① m、n∈1,2,…,K
② (n^ 2-mn-m^2)^2=1
编一程序,对给定K,求一组满足上述两个条件的m、n,并且使m^2+n^2的值最大。例如,若K=1995,则m=987,n=1597,则m、n满足条件,且可使m^2+n^2的值最大。

格式

输入格式

输入仅一行,K的值。

输出格式

输出仅一行,m^2+n^2的值。

样例1

样例输入1[复制]

1995

样例输出1[复制]

3524578

限制

每个测试点1秒。

提示

Source:
汕头市FXOI组
Phoeagon
ThanX2 Sivon
For TripleY

posted @ 2014-10-07 11:08  iwtwiioi  阅读(340)  评论(0编辑  收藏  举报