zoukankan      html  css  js  c++  java
  • HDU 4342——History repeat itself——————【数学规律】

    History repeat itself

    Time Limit: 1000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 4342
    64-bit integer IO format: %I64d      Java class name: Main
    Type: 
    None
     
     
    Tom took the Discrete Mathematics course in the 2011,but his bad attendance angered Professor Lee who is in charge of the course. Therefore, Professor Lee decided to let Tom face a hard probability problem, and announced that if he fail to slove the problem there would be no way for Tom to pass the final exam.
    As a result , Tom passed.
    History repeat itself. You, the bad boy, also angered the Professor Lee when September Ends. You have to faced the problem too.
    The problem comes that You must find the N-th positive non-square number M and printed it. And that's for normal bad student, such as Tom. But the real bad student has to calculate the formula below.

    So, that you can really understand WHAT A BAD STUDENT YOU ARE!!
     

    Input

    There is a number (T)in the first line , tell you the number of test cases below. For the next T lines, there is just one number on the each line which tell you the N of the case.
    To simplified the problem , The N will be within 231 and more then 0.
     

    Output

    For each test case, print the N-th non square number and the result of the formula.
     

    Sample Input

    4
    1
    3
    6
    10

    Sample Output

    2 2
    5 7
    8 13
    13 28

    Source

     
    解题思路1:找规律。
     
    #include<bits/stdc++.h>
    using namespace std;
    typedef __int64 INT;
    INT dfs(INT c){
        INT cc=(INT)sqrt(c);
        if(cc==0){
            return 0;
        }
        INT ret=(c-cc*cc+1)*cc;
        return ret+dfs(cc*cc-1);
    }
    int main(){
        int t;
        INT n,m;
        scanf("%d",&t);
        while(t--){
            scanf("%I64d",&n);
            INT nn=(INT)sqrt(n);
            m=n+nn;
            INT mm=(INT)sqrt(m);
            if(mm>nn)    //这条件开始写错了
                m++;
            INT ans=dfs(m);
            printf("%I64d %I64d
    ",m,ans);
        }
        return 0;
    }    
    

      

    思路2:数学推导。http://www.cnblogs.com/kuangbin/archive/2012/08/08/2628794.html

    #include<bits/stdc++.h>
    using namespace std;
    typedef __int64 INT;
    INT dfs(INT c){
        INT cc=(INT)sqrt(c);
        if(cc==0){
            return 0;
        }
        INT ret=(c-cc*cc+1)*cc;
        return ret+dfs(cc*cc-1);
    }
    int main(){
        int t;
        INT n,m;
        scanf("%d",&t);
        while(t--){
            scanf("%I64d",&n);
            INT mm=(INT)ceil((1+sqrt(1+4*n))/2)-1;
            m=mm+n;
            INT ans=dfs(m);
            printf("%I64d %I64d
    ",m,ans);
        }
        return 0;
    }
    

      

     
  • 相关阅读:
    NOI2015 品酒大会
    网络流算法进阶详解
    ZROJ 1267 我要打数论
    后缀自动机构建图解
    WC集训DAY2笔记 组合计数 part.1
    最小割的数学模型
    二次剩余学习笔记
    Hadoop:eclipse配置hadoop-eclipse-plugin(版本hadoop2.7.3)
    MapReduce:实现文档倒序排序,且字符串拼接+年+月+日
    MapReduce:汇总学生表和成绩表为----学生成绩表
  • 原文地址:https://www.cnblogs.com/chengsheng/p/4766050.html
Copyright © 2011-2022 走看看