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\n", a[i]);
    }
    return 0;
}
View Code

 

posted on 2013-06-30 17:15  Still_Raining  阅读(553)  评论(0编辑  收藏  举报