码迷,mamicode.com
首页 > 其他好文 > 详细

hdoj 2601(判断N=i*j+i+j)

时间:2014-08-06 11:40:21      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:des   http   java   os   io   for   ar   div   

Problem E

Time Limit : 6000/3000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 16   Accepted Submission(s) : 9

Font: Times New Roman | Verdana | Georgia

Font Size: ← →

Problem Description

When Teddy was a child , he was always thinking about some simple math problems ,such as “What it’s 1 cup of water plus 1 pile of dough ..” , “100 yuan buy 100 pig” .etc..
bubuko.com,布布扣

One day Teddy met a old man in his dream , in that dream the man whose name was“RuLai” gave Teddy a problem :

Given an N , can you calculate how many ways to write N as i * j + i + j (0 < i <= j) ?

Teddy found the answer when N was less than 10…but if N get bigger , he found it was too difficult for him to solve.
Well , you clever ACMers ,could you help little Teddy to solve this problem and let him have a good dream ?

Input

The first line contain a T(T <= 2000) . followed by T lines ,each line contain an integer N (0<=N <= 1010).

Output

For each case, output the number of ways in one line.

Sample Input

2
1
3

Sample Output

0
1

易知用两个循环嵌套判断n=i*j+i+j必然超时,可以推得n+1=i*j+i+j+1,即为n+1=(i+1)*(j+1);
题中提示j>=i>0;那么j+1>=i+1,用x代替(i+1),将x从2遍历到sqrt(n+1),在此条件下判断(n+1)%x,若为0,说明
存在(j+1),使得(i+1)*(j+1)=n+1,由于是在(i+1)<=sqrt(n+1)的条件下的结果,必然有(j+1)>=(i+1)成立。
时间复杂度由O(n)变为O(sqrt(n)).

hdoj 2601(判断N=i*j+i+j),布布扣,bubuko.com

hdoj 2601(判断N=i*j+i+j)

标签:des   http   java   os   io   for   ar   div   

原文地址:http://www.cnblogs.com/weiyikang/p/3894112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!