UVA138(数论问题二分打表)

来源:互联网 发布:耽美小说完结推荐知乎 编辑:程序博客网 时间:2024/05/17 06:55

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19591


解题思路:

题意是给你一个last number记为n,在1~n之间给出house number记为m,求1~m的和 == n ~ m的和,输出10个满足条件的m和n,每个数字宽度为10。

由求和公式,我们很明显可以推导出m和 n的关系为2 * m * m == n * (n + 1)。从8开始枚举n,然后在n内进行二分求m,如果遇到满足上式,则输出,计数器--。

中间涉及到乘方,所以开成long long防溢出。后来发现如果在程序中直接求的话会很慢,索性把结果打成一个表存在二维数组里,最后按格式输出。


完整代码:

#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <sstream>#include <iomanip>#include <numeric>#include <cstring>#include <climits>#include <cassert>#include <complex>#include <cstdio>#include <string>#include <vector>#include <bitset>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <list>#include <set>#include <map>using namespace std;#pragma comment(linker, "/STACK:102400000,102400000")typedef long long LL;typedef double DB;typedef unsigned uint;typedef unsigned long long uLL;/** Constant List .. **/ //{const int MOD = int(1e9)+7;const int INF = 0x3f3f3f3f;const LL INFF = 0x3f3f3f3f3f3f3f3fLL;const DB EPS = 1e-9;const DB OO = 1e20;const DB PI = acos(-1.0); //M_PI;/*void init () //打表{    LL n , m;    int cnt = 10;    for(LL n = 8 ; cnt > 0 ; n ++ )    {        LL l = 1 ;        LL r = n;        while(l <= r)        {            m = (l + r) / 2;            LL t1 = 2 * m * m;            LL t2 = n * (n + 1);            if(t1 == t2)            {                printf("%10lld%10lld\n",m , n);                cnt --;                break;            }            else if(t1 < t2)                l = m + 1;            else                r = m - 1;        }    }}*/LL d[10][2] = {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("%10lld%10lld\n" , d[i][0] , d[i][1]);    }}


0 0
原创粉丝点击