HDU 2601 An easy problem(暴力枚举/质因子分解)

An easy problem

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 7963    Accepted Submission(s): 1920

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

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(0<i<=j)的i、j的种数。

第一种方法:首先这个等式可以化成(n+1)=(i+1)*(j+1),所以只要求出(n+1)的约数的种数即可。同时注意到i与j呈负相关,同时i小于等于j,所以只需要从2到sqrt(n+1)枚举(从2开始是因为i最小为1,我们枚举的是(i+1))。但是,好暴力啊。所以我们可以用筛法先保存1e5以内的素数,再通过质因子分解求出约数数量。

第二种方法:观察等式n=i*j+i+j,可以转化成n-i=(i+1)*j,发现暴力枚举i,判断(n-i)%(n+1)==0判断整数j是否存在也是可行的,同样是因为之前说过的的原因,只需要从1到sqrt(n)枚举,同时判断i<=(n-i)/(i+1),不符合即则跳出循环,因为题目规定i<=j。

/*
(n+1)=(i+1)*(j+1)
Memory: 1568 KB		Time: 2184 MS
Language: G++		Result: Accepted
*/
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma commment(linker,"/STACK: 102400000 102400000")
#define lson a,b,l,mid,cur<<1
#define rson a,b,mid+1,r,cur<<1|1
using namespace std;
typedef long long LL;
const double eps=1e-6;
const int MAXN=210;

LL n;

int main()
{
    int tcase;
    scanf("%d",&tcase);
    while(tcase--)
    {
        scanf("%lld",&n);
        n++;
        int ans=0;
        for(int i=2; (LL)i*i<=n; i++)
        {
            if(n%i==0)
                ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}
/*
n-i=(i+1)*j
Memory: 1572 KB		Time: 2059 MS
Language: G++		Result: Accepted
*/
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma commment(linker,"/STACK: 102400000 102400000")
#define lson a,b,l,mid,cur<<1
#define rson a,b,mid+1,r,cur<<1|1
using namespace std;
typedef long long LL;
const double eps=1e-6;
const int MAXN=210;

LL n;

int main()
{
    int tcase;
    scanf("%d",&tcase);
    while(tcase--)
    {
        scanf("%lld",&n);
        if(n==1||n==2)
        {
            printf("0\n");
            continue;
        }
        if(n==3)
        {
            printf("1\n");
            continue;
        }
        int ans=0;
        double s=sqrt(n);
        for(LL i=1; i<=s; i++)
        {
            if(i>(n-i)/(i+1))
                break;
            if((n-i)%(i+1)==0)
                ans++;
        }
        printf("%d\n",ans);
    }
    return 0;
}
/*
质因子分解求出约数数量
Memory: 2004 KB		Time: 78 MS
Language: G++		Result: Accepted
*/
#include<stack>
#include<queue>
#include<cmath>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma commment(linker,"/STACK: 102400000 102400000")
#define lson a,b,l,mid,cur<<1
#define rson a,b,mid+1,r,cur<<1|1
using namespace std;
typedef long long LL;
const double eps=1e-6;
const int MAXN=1e5+20;

int prime[MAXN],vis[MAXN],cnt;
LL n;

int getPrime()
{
    cnt=1;
    vis[1]=1;
    for(int i=2;i<=MAXN;i++)
    {
        if(vis[i])
            continue;
        prime[cnt++]=i;
        if((LL)i*i>MAXN)
            continue;
        for(int j=i*i;j<MAXN;j+=i)
            vis[j]=1;
    }
}

int main()
{
    getPrime();
    int tcase;
    scanf("%d\n",&tcase);
    while(tcase--)
    {
        scanf("%lld",&n);
        int ans=1;
        n++;
        for(int i=1;(LL)prime[i]*prime[i]<=n;i++)
        {
            if(n%prime[i]==0)
            {
                int s=0;
                while(n%prime[i]==0)
                {
                    s++;
                    n/=prime[i];
                }
                ans*=(s+1);
            }
            if(n==1)
                break;
        }
        if(n!=1)
            ans*=2;
        printf("%d\n",(ans+1)/2-1);
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-20 01:47:31

HDU 2601 An easy problem(暴力枚举/质因子分解)的相关文章

hdu 4932 Miaomiao&#39;s Geometry 暴力枚举

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4932 Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 694    Accepted Submission(s): 180 Problem Description There are N point

HDU 4930 Fighting the Landlords(暴力枚举+模拟)

HDU 4930 Fighting the Landlords 题目链接 题意:就是题中那几种牌型,如果先手能一步走完,或者一步让后手无法管上,就赢 思路:先枚举出两个人所有可能的牌型的最大值,然后再去判断即可 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; struct Player { int rank[15]; } p1, p2; int t, h

HDU 5371 Hotaru&#39;s problem(manacher + 枚举啊)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5371 Problem Description Hotaru Ichijou recently is addicated to math problems. Now she is playing with N-sequence. Let's define N-sequence, which is composed with three parts and satisfied with the foll

HDU 4770 Lights Against Dudely 暴力枚举+dfs

又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ czy Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1360    Accepted Subm

hdu 4445 Crazy Tank (暴力枚举)

Crazy Tank Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4562    Accepted Submission(s): 902 Problem Description Crazy Tank was a famous game about ten years ago. Every child liked it. Time f

数据结构(主席树):HDU 4729 An Easy Problem for Elfness

An Easy Problem for Elfness Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1148    Accepted Submission(s): 234 Problem Description Pfctgeorge is totally a tall rich and handsome guy. He plans t

hdu4282A very hard mathematic problem 暴力枚举

//给出k //找x,y,z使得x^z+y^z+x*y*z = k //x,y,z都为正整数x<y,z>1问有多少种方法 //当z = 2时,可以看到左边是一个完全平方 //而当z>=3时,可以暴力枚举x,y //由于k<2^31所以x<2^(31/3)枚举复杂度可以过 #include<cstdio> #include<cstring> #include<iostream> #include<cmath> using name

hdu3448Bag Problem 暴力枚举

//给k个数,问最多取n个,所取的数的和不大于m的最大的和 //暴力枚举所有情况 #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std ; const int maxn = 110 ; typedef int ll; ll ans = 0 ; ll a[maxn] ; ll n , m ; int k ; void dfs(i

hdu 2055 An easy problem (java)

问题: 开始尝试用字符做数组元素,但是并没有用.在判断语句时把a z排出了. An easy problem Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 16516    Accepted Submission(s): 11096 Problem Description we define f(A) = 1, f(a) = -1,