zoukankan      html  css  js  c++  java
  • 考试整理

    T2_极值问题

    Description

    已知m、n为整数,且满足下列两个条件: 

    (1)m、n∈1,2,3,……,k 
    (2)(n^2-mn-m^2)^2=1 

    对给定的k,求m^2+n^2的最大值 

    Sample Input 

    1995 

    Sample Output 

    m=987 

    n=1597

    打表找规律好评(大雾

    但是肯定是有证明的吗(要不然大佬们怎么做啊qwq

    由条件2:(n2-mn-m2)2=1

    故而:      (m2 + mn- n2)2=1

    继续化简:m2+mn-n2=(m+n)2-mn-2n2 

                                                    =(m+n)2-(m+n)n-n2 
    即:          (n2-mn-m2)2=[(m+n)2-(m+n)n-n2]2

    我们观察上述最后的等式,我们可以发现

    n->m+n (第一个平方)

    m->n,n->m+n(中间的因式)

    m->n(第二个平方)

    这时我们发现这是我们熟悉的斐波那契数列,这样,这一题的突破口很明显了,m、n都是在K(包括K)之内的最大的两个满足斐波那契数列的数;

    Code:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<iomanip>
    #include<string>
    #include<algorithm>
    #include<cstdlib>
    #include<queue>
    #include<stack>
    using namespace std;
    inline int read() 
    {
        int X=0,w=1; 
        char c=getchar();
        while(c<'0'||c>'9')
        { 
            if (c=='-')
            {
                w=-1; 
            } 
            c=getchar();
        }
        while(c>='0'&&c<='9')
        {
            X=(X<<3)+(X<<1)+c-'0';
            c=getchar();
        } 
        return X*w;
    }
    int n=1,m=1;
    int main()
    {
        freopen("mn.in","r",stdin);
        freopen("mn.out","w",stdout);
        int k;
        k=read();
        while(n+m<=k)
        {
            int pos=m;
            m=n+m;
            n=pos;
        }
        printf("%d %d",n,m);
        return 0;
    }
  • 相关阅读:
    springboot缓存-Ehcache
    springboot+spring data jpa 多数据源配置
    vue+element 上传图片控件
    springboot下载文件
    easyPoi导入带图片的excel
    内外网同时使用(宽带内网无线内网)
    mysql 8.0 安装
    搭建一个Vue前端项目
    mybatis反向代理自动生成mapper文件
    【1】idea Live Templates
  • 原文地址:https://www.cnblogs.com/gongcheng456/p/11158452.html
Copyright © 2011-2022 走看看