• 极值


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

            ① m、n∈{1,2,…,k},即1≤m,n≤k

            ②(n2-m*n-m2)2=1

    你的任务是:编程由键盘输入正整数k(1≤k≤109),求一组满足上述两个条件的m、n,并且使m2+n2的值最大。例如,从键盘输入k=1995,则输出:m=987   n=1597。

    今天咋遇到这么多奇怪题...

    先数学推导一波,发现m + n,n也能满足上式,因此答案就是一个斐波那契数列,求出<=k的最大的相邻两项即可

    复杂度O(logk)

    #include<cstdio>
    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<cstdlib>
    #include<ctime>
    using namespace std;
    inline int read()
    {
        int ans = 0,op = 1;
        char ch = getchar();
        while(ch < '0' || ch > '9')
        {
            if(ch == '-') op = -1;
            ch = getchar();
        }
        while(ch >= '0' && ch <= '9')
        {
            (ans *= 10) += ch - '0';
            ch = getchar();
        }
        return ans * op;
    }
    typedef int mainint;
    #define int long long 
    int a,b,c,k;
    mainint main()
    {
        k = read();
        a = b  = 1;
        c = a + b;
        while(c < k)
            a = b,b = c,c = a + b;
        cout << "m=" << a << endl << "n=" << b;
    }
  • 相关阅读:
    [HNOI2010]CITY 城市建设

    [HNOI2011]数学作业
    [NOI2012]美食节
    [HEOI2014]大工程
    [HEOI2013]ALO(待更)
    [HEOI2016/TJOI2016]序列
    贪食蛇(未完待续)
    [HEOI2016/TJOI2016]字符串
    bzoj 2437[Noi2011]兔兔与蛋蛋 黑白染色二分图+博弈+匈牙利新姿势
  • 原文地址:https://www.cnblogs.com/LM-LBG/p/10617402.html
Copyright © 2020-2023  润新知