HDU 2601An easy problem-素数的运用,暴力求解

An easy problem

Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u

Submit Status

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 <= 10 10).

Output

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

Sample Input

2
1
3 

Sample Output

0
1 

对于 N as i * j + i + j (0 < i <= j) ?

可以表示为N=i*j+i+j

所以可以化作N+1=(i+1)*(j+1);

如此就有两种思路去做,一暴力枚举,从这里可以看出来i<=sqrt(N+1);

所以一个循环就可以解决,第一份代码就是,但是耗时才一点就过题目提供的3s了,怎么办,是否还有

更好的解决方案呢,有的,N+1=(i+1)*(j+1)可以知道N+1是i+1以及j+1的倍数

如此就可以转换成求解约数的个数(约数是什么,请读者自己百度了解)

N+1=a1^p1*a2^p2*a3^p3....an^pn

其中ai是代表着质数,这个的意思是任何大于1的数都可以转换为有限个质数因子的乘积

如此,可以用排列组合来求,第一种有p1+1选择(可以选择0...p1)第二种有p2+1选择(可以选择0...p2)....第n种有pn+1选择(可以选择0...pn)

所以约数个数ans=(p1+1)*(p2+1)*(p3+1)*(p4+1)*....*(pn+1)(里面还有减去1和n因为他们不属于题目要求范围)

当N+1是完全平方数的话,那么除了1以及N+1本身外,唯有最中间的约数是只计算了一次,其他的数都重复的计算了两次,

当N+1不是完全平方数的话,那么除了1以及N+1本身外,其他的数都重复的计算了两次

所以可以分开判断输出,也可以直接转换输出就像代码中一样,(ans+1)/2-1,当为完全平方数时,我们需要加一除二才能使正确的结果

至于减去一,就是前面的去掉1和n这两个不符合条件的数

当为不完全平方数,ans/2-1就可以了,但是为了合成一个式子,(ans+1)/2-1,是可以代替ans/2-1的

为什么,因为(4+1)/2==4/2,这是不会影响最终结果的。

/*
Author: 2486
Memory: 1416 KB		Time: 2823 MS
Language: G++		Result: Accepted
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long LL;
const int maxn=1e5;
int t;
LL n;
int main() {
    scanf("%d",&t);
    while(t--) {
        scanf("%I64d",&n);
        if(n==0||n==1) {
            printf("0\n");
            continue;
        }
        int cnt=0;
        for(int i=1; i<=sqrt(n); i++) {
            if((n+1)%(i+1)==0&&(n+1)/(i+1)>=i+1)cnt++;
        }
        printf("%d\n",cnt);
    }
}
/*
Author: 2486
Memory: 1592 KB		Time: 46 MS
Language: G++		Result: Accepted
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn=100000+5;
LL prime[maxn];
bool vis[maxn];
int T,cnt;
LL N;
void primes() { //初始化素数列表
    cnt=0;
    for(int i=2; i<maxn; i++) {
        if(vis[i])continue;
        prime[cnt++]=i;
        for(int j=i*2; j<maxn; j+=i) {
            vis[j]=true;
        }
    }
}
void solve(LL n) {
    LL ans=1;
    for(int i=0; prime[i]*prime[i]<=n; i++) {
        if(n%prime[i]==0) {
            int s=0;
            while(n%prime[i]==0)n/=prime[i],s++;
            ans*=(s+1);
        }
        if(n==1)break;
    }
    if(n>1)ans*=2;
    printf("%I64d\n",(ans+1)/2-1);
}
int main() {
    primes();
    scanf("%d",&T);
    while(T--) {
        scanf("%I64d",&N);
        N++;
        solve(N);
    }
    return 0;
}

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

时间: 2024-10-12 17:48:44

HDU 2601An easy problem-素数的运用,暴力求解的相关文章

HDU 5475An easy problem 离线set/线段树

An easy problem Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description One day, a useless calculator was being built by Kuros. Let's assume that number X is showed on the screen of calculator. At first

hdu 5104 Primes Problem (素数+递推)

Problem Description Given a number n, please count how many tuple(p1, p2, p3) satisfied that p1<=p2<=p3, p1,p2,p3 are primes and p1 + p2 + p3 = n. Input Multiple test cases(less than 100), for each test case, the only line indicates the positive int

hdu 5104 Primes Problem (素数 打表 水)

http://acm.hdu.edu.cn/showproblem.php?pid=5104 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> using namespace std; int isp[10000+100]; int prime[10000]; int coun; bool isprime(int x)

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 p

数据结构(主席树):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

HDU 1431 素数回文【暴力求解】

/* 题目大意:找一个范围内的所有素数回文数 解题思路:打一个表将1亿以内所有的素数回文数找出来,大概有780个这样子 关键点:暴力求解 解题人:lingnichong 解题时间:2014-08-29 12:02:55 解题体会:如果按一般方法打个素数表,很容易超内存(MLE),所以就先将所有的素数回文全部算出来,再在这个数组里面找在题上那个范围的所有素数回文数 */ 素数回文 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 655

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,

HDU2123 An easy problem【水题】

An easy problem Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5922    Accepted Submission(s): 4122 Problem Description In this problem you need to make a multiply table of N * N ,just like th

HDU2132 An easy problem【水题】

An easy problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10149    Accepted Submission(s): 2689 Problem Description We once did a lot of recursional problem . I think some of them is easy