• UVA138 Street Numbers(数论)


    题目链接

    题意:

    找一个n,和一个m(m < n),求使得1~m的和等于m~n的和,找出10组m,n

    分析;

    列出来式子就是

    m*(m+1)/2 = (n-m+1)*(m+n)/2

    化简后为 m*m*2 = n*(n+1)

    可以枚举n,然后二分找m,不过这样大约会用10s多,可以打表。

    打表程序:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <queue>
    #include <algorithm>
    #include <cmath>
    #include <iomanip>
    
    using namespace std;
    
    typedef unsigned long long int LL;
    
    int main() {
            freopen("my.txt", "w", stdout);
            int cnt = 0;
    
            for(LL n=8; cnt < 10; n++) {
                LL l = 1, h = n;
                while(l <= h) {
                    LL mid = (l+h)/2;
                    LL t1 = 2*mid*mid, t2 = n*(n+1);
                    if(t1 == t2) {
                        cout << '"' << setw(10) << mid << setw(10) << n << '"' << ',';
                        cnt++;
                        break;
                    }
                    else if(t1 < t2) l = mid+1;
                    else h = mid-1;
                }
            }
    
        return 0;
    }
    View Code

    AC代码:

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    
    using namespace std;
    
    char a[][40] = {"         6         8","        35        49","       204       288","      1189      1681","      6930      9800","     40391     57121","    235416    332928",
    "   1372105   1940449","   7997214  11309768","  46611179  65918161"};
    
    int main() {
    
        for(int i=0; i<10; i++) {
            printf("%s
    ", a[i]);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    c#配置文件
    C#预处理指令
    C#面向对象详解
    231. Power of Two
    226. Invert Binary Tree
    C语言函数入参压栈顺序为什么是从右向左?
    对C++ 虚函数的理解
    悲观锁和乐观锁
    什么是索引
    CHAR 和VARCHAR的区别
  • 原文地址:https://www.cnblogs.com/tanhehe/p/3163684.html
Copyright © 2020-2023  润新知