首页 > ACM题库 > HDU-杭电 > hdu 2601 An easy problem-数学[解题报告]C++
2014
02-10

hdu 2601 An easy problem-数学[解题报告]C++

An easy problem

问题描述 :

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..

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 ?

输入:

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

输出:

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

样例输入:

2
1
3

样例输出:

0
1

http://acm.hdu.edu.cn/showproblem.php?pid=2601

  题意:是将一个数 N 分解成 i * j + i + j 有多少种分解方式。

  思路:以 i 作为一个维度,j 作为第二维度进行循环,则 N = i * j + i + j = i * ( j + 1 ) + i ; 当 j = j + 1 时 N’ = i * ( j + 1 )  + i + j + 1 = N + i + 1; 所以只要进行一为循环就可以了,判定 ( N – i ) % ( i + 1 ) == 0 ? 就可以了。

  代码:

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <iostream>
using namespace std;

int main()
{
    int T;
    long long N;
    scanf( "%d", &T );
    while( T-- )
    {
        scanf( "%I64d", &N );
        long long lim = ( long long )sqrt( double ( N + 1 ) ) - 1, cnt = 0;
        for( long long i = 1; i <= lim; ++i )
        {
            if( ( N - i ) % ( i + 1 ) == 0  ) 
            {
                cnt++;
            }
        }    
        printf( "%I64d\n", cnt );
    }
    return 0;
}

 

解题转自:http://www.cnblogs.com/Lyush/archive/2011/12/02/2272650.html


  1. 题本身没错,但是HDOJ放题目的时候,前面有个题目解释了什么是XXX定律。
    这里直接放了这个题目,肯定没几个人明白是干啥