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;
}

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

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

    An easy problem Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  2. hdu 5475 An easy problem(暴力 || 线段树区间单点更新)

    http://acm.hdu.edu.cn/showproblem.php?pid=5475 An easy problem Time Limit: 8000/5000 MS (Java/Others ...

  3. HDU 5475 An easy problem 线段树

    An easy problem Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

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

    An Easy Problem for Elfness Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65535/65535 K (J ...

  5. HDU 2132 An easy problem

    http://acm.hdu.edu.cn/showproblem.php?pid=2132 Problem Description We once did a lot of recursional ...

  6. HDOJ(HDU) 2132 An easy problem

    Problem Description We once did a lot of recursional problem . I think some of them is easy for you ...

  7. hdu 2055 An easy problem (java)

    问题: 開始尝试用字符做数组元素.可是并没实用. 在推断语句时把a z排出了. An easy problem Time Limit: 1000/1000 MS (Java/Others)    Me ...

  8. HDU 5475:An easy problem 这题也能用线段树做???

    An easy problem Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  9. HDU 2098 分拆素数和

    HDU 2098 分拆素数和 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768K (Java/Others) [题目描述 ...

随机推荐

  1. 指定字符串 s,返回 s 所有可能的子串,每个子串必须是一个回文(指顺读和倒读都一样的字符串)

    Given a string s, partition s such that every substring of the partition is a palindrome Return all ...

  2. Python升级版本2.6到2.7

    CentOS 6 系统默认 Python 版本是:2.6.6 平时在使用中遇到很多的库要求是 2.7.x 版本的库,比如使用 ConfigParser 库,在 2.6 版本库就不支持没有 value ...

  3. poj 1061 青蛙的约会(二元一次不定方程)

      Description 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要 ...

  4. Nk 1214 Relatives(欧拉函数)

    Time Limit: 1500 ms    Memory Limit: 10000 kB   Total Submit : 234 (77 users)   Accepted Submit : 10 ...

  5. K大数查询(bzoj 3110)

    Description 有N个位置,M个操作.操作有两种,每次操作如果是1 a b c的形式表示在第a个位置到第b个位置,每个位置加入一个数c如果是2 a b c形式,表示询问从第a个位置到第b个位置 ...

  6. javascript 日期处理类库 moment.js

  7. JS设置页面中方法执行一次的思想

    思想:在JS中定义一全局变量,在方法执行的时候根据全局变量的值判断是否需要执行,在方法中修改全局变量的值,可以使得方法只执行一次.: 例如: 定义全局变量: var isLoad = false;// ...

  8. Golang 入门 : 等待 goroutine 完成任务

    Goroutine 是 Golang 中非常有用的功能,但是在使用中我们经常碰到下面的场景:如果希望等待当前的 goroutine 执行完成,然后再接着往下执行,该怎么办?本文尝试介绍这类问题的解决方 ...

  9. windows symbol server调试

    linux下gdb强大的调试功能让人印象深刻,一直以为windows下调试可执行程序非常让人头痛.经一些高人指点后知道原来windows下还有symbol server这种调试工具 参见下面两个文档 ...

  10. jquery的固定定位效果

    今天做了个固定定位的效果.比如对导航需要进行固定定位效果: 当没有滚动到导航下面,导航正常显示. 当滚动到导航下面,导航就固定到顶部. 这个效果使用了jquery的方法实现,具体思路为: 1)首先获取 ...