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

 

None

Graph Theory

2-SAT

Articulation/Bridge/Biconnected Component

Cycles/Topological Sorting/Strongly Connected Component

Shortest Path

Bellman Ford

Dijkstra/Floyd Warshall

Euler Trail/Circuit

Heavy-Light Decomposition

Minimum Spanning Tree

Stable Marriage Problem

Trees

Directed Minimum Spanning Tree

Flow/Matching

Graph Matching

Bipartite Matching

Hopcroft–Karp Bipartite Matching

Weighted Bipartite Matching/Hungarian Algorithm

Flow

Max Flow/Min Cut

Min Cost Max Flow

DFS-like

Backtracking with Pruning/Branch and Bound

Basic Recursion

IDA* Search

Parsing/Grammar

Breadth First Search/Depth First Search

Advanced Search Techniques

Binary Search/Bisection

Ternary Search

Geometry

Basic Geometry

Computational Geometry

Convex Hull

Pick's Theorem

Game Theory

Green Hackenbush/Colon Principle/Fusion Principle

Nim

Sprague-Grundy Number

Matrix

Gaussian Elimination

Matrix Exponentiation

Data Structures

Basic Data Structures

Binary Indexed Tree

Binary Search Tree

Hashing

Orthogonal Range Search

Range Minimum Query/Lowest Common Ancestor

Segment Tree/Interval Tree

Trie Tree

Sorting

Disjoint Set

String

Aho Corasick

Knuth-Morris-Pratt

Suffix Array/Suffix Tree

Math

Basic Math

Big Integer Arithmetic

Number Theory

Chinese Remainder Theorem

Extended Euclid

Inclusion/Exclusion

Modular Arithmetic

Combinatorics

Group Theory/Burnside's lemma

Counting

Probability/Expected Value

Others

Tricky

Hardest

Unusual

Brute Force

Implementation

Constructive Algorithms

Two Pointer

Bitmask

Beginner

Discrete Logarithm/Shank's Baby-step Giant-step Algorithm

Greedy

Divide and Conquer

Dynamic Programming

Tag it!

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\n",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\n",m,ans);
}
return 0;
}

  

 

HDU 4342——History repeat itself——————【数学规律】的更多相关文章

  1. hdu 4342 History repeat itself(数学题)

    题目链接:hdu 4342 History repeat itself 题意: 让你找第a个非完全平方数m,并且求前m个数的开方向下取整的和. 题解: 第一个问题: 假设第a个非平方数是X,X前面有n ...

  2. 二分查找+数学 HDOJ 4342 History repeat itself

    题目传送门 题意:计算从1开始到第n个非完全平方数的开方和 分析:设第n个非完全平方数的值为a,x * x < a < (x+1) * (x+1),而且易得(tmp = sqrt (a) ...

  3. 多校-HDU 5351 MZL's Border 数学规律

    f[1] = 'b', f[2] = 'a', f[i] = f[i - 1] + f[i - 2] 斐波那契数列的字符串,给你n和m,前m位中,最长的前缀等于后缀的长度是多少.1≤n≤1000, 1 ...

  4. 2017ACM暑期多校联合训练 - Team 2 1011 HDU 6055 Regular polygon (数学规律)

    题目链接 **Problem Description On a two-dimensional plane, give you n integer points. Your task is to fi ...

  5. HDU 4342History repeat itself 数学

    C - History repeat itself Time Limit:1000MS     Memory Limit:32768KB      Description Tom took the D ...

  6. 2012 #5 History repeat itself

    History repeat itself Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  7. Codeforces 715A & 716C Plus and Square Root【数学规律】 (Codeforces Round #372 (Div. 2))

    C. Plus and Square Root time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  8. [wx]自然数学规律

    有趣的数学规律 椭圆 双曲线 抛物线都叫圆锥曲线 它们跟圆锥有着怎样的关系? 他们都是圆锥与平面在不同姿势下交配的产物. 参考 椭圆 抛物线 小结 e: 离线率 P: 任意一点 F: 焦点 准线: 一 ...

  9. HDU 3032 multi-sg 打表找规律

    普通NIM规则加上一条可以分解为两堆,标准的Multi-SG游戏 一般Multi-SG就是根据拓扑图计算SG函数,这题打表后还能发现规律 sg(1)=1 sg(2)=2 sg(3)=mex{0,1,2 ...

随机推荐

  1. 在GridView控件FooterTemplate内添加记录

    在GridView控件FooterTemplate内添加记录,想实现这个功能,有几点要清楚的,这个添加铵钮是在FooterTemplate内,还是在GridView控件外部,位置不同,某些处理逻辑会有 ...

  2. 市场上主流的BI产品的“答案之书”

    本文来自网易云社区. 从20世纪80年代开始,商业智能的定义出现在人们面前,早期商业智能十分基础和杂乱,不仅仅会把数据处理放进去.还包含有一些可视化方面内容等.这个时期的BI主要的功能是支持多维分析和 ...

  3. 入门GitHub

    Step 1: 创建一个我们自己的账号 我们先登录https://github.com,然后单击sign up for Github,我们输入用户名,密码和邮箱就可以有一个 属于我们自己的Github ...

  4. Ajax上传文件注意事项

    如图: 因为传进来的参数被预先处理了,所以才出现了illegal invocation报错,只要将processData的值改为false就能解决这个小问题: 因为JQ自动的帮我们设置了请求头,但是a ...

  5. Redis源码阅读---连接建立

    对于并发请求很高的生产环境,单个Redis满足不了性能要求,通常都会配置Redis集群来提高服务性能.3.0之后的Redis支持了集群模式. Redis官方提供的集群功能是无中心的,命令请求可以发送到 ...

  6. loj #2254. 「SNOI2017」一个简单的询问

    #2254. 「SNOI2017」一个简单的询问 题目描述 给你一个长度为 NNN 的序列 aia_ia​i​​,1≤i≤N1\leq i\leq N1≤i≤N,和 qqq 组询问,每组询问读入 l1 ...

  7. bootstrap添加模态窗后,再弹出消息提示框后,原先的滚动条消失

    设置需要滚动的模态框 overflow :scroll

  8. SEVERE: One or more listeners failed to start.

    Full details will be found in the appropriate container log file 错误日志在/home/dela/.IntelliJIdea2017.1 ...

  9. 在线作图工具 Flowchart Maker & Online Diagram Software & Visual Solution

    9款国内外垂直领域的在线作图工具:那些可以替代Visio的应用!-CSDN.NEThttps://www.csdn.net/article/2015-02-12/2823939 Documentsht ...

  10. POJ2528 Mayor's posters(线段树+离散化)

    题意 : 在墙上贴海报, n(n<=10000)个人依次贴海报,给出每张海报所贴的范围li,ri(1<=li<=ri<=10000000).求出最后还能看见多少张海报. 分析 ...