3094. 【NOIP2012模拟11.7】Hash函数

来源:互联网 发布:如何购买淘宝小号 编辑:程序博客网 时间:2024/06/14 18:16

Description

明明觉得hash是个好算法,代码短、效率高。某天,他碰到了一个求正方形个数的问题,于是很淡定地枚举对角线然后用hash判存在,妥妥的搞定,但是提交后却wa了几个点。仔细观察其hash函数为: h=x*y+x+y 。为了让明明知道这个函数存在什么问题,对于给出一个h值,请你来告诉他有多少对(x,y)满足上述式子(max(x,y)<=h;h,x,y都为非负整数)。
Input
多组测试数据,第一行为测试点的个数T,接下来每一行一个整数h,意义如上。
Output
一共T行,每行一个整数,分别表示有多少组(x,y)满足要其对应的h值。
Sample Input
3
1
3
4
Sample Output
2
3
2

Data Constraint

Hint

【样例解释】
(1,0),(0,1)
(0,3),(1,1),(3,0)
(4,0),(0,4)
【数据范围】
对于30%数据 h<=20,000 , T<=1000
对于100%数据 h<=100,000,000 , T<=10000;

因为h=x*y+x+y
so h+1=(x+1)(y+1)

所以用线筛求出满足条件的因数对数数量即可。。
乱搞。。
比赛是打了分段。。

Code

#include <cstdio>#include <iostream>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#define fo(i,a,b) for (int i=a;i<=b;i++)#define fd(i,a,b) for (int i=a;i>=b;i--)#define INF 2147483647#define Hmax 20000#define N 10005#define M 1505 using namespace std;int T,h=0,tot=0;int Prime_Number[M],a[M];bool bz[N];inline int read(int &n){    int x=0,w=1;    char ch=getchar();    while(ch>'9'||ch<'0'){if(ch=='-') w=-1;ch=getchar();}    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}    n=x*w;    return n;}void Pretreatment(){    fo(i,2,N)    {        if (!bz[i]) Prime_Number[++tot]=i;        else continue;        for (int j=i;j<=N;j+=i) bz[j]=true;    }}int Calculation(int x){    int ans=0;    fo(i,1,x)        if (!(x%i)) ans++;    return ans;}int Calculation2(int x){    int ans=1;    fo(i,1,tot)    {        a[i]=0;        while (!(x%Prime_Number[i])) x/=Prime_Number[i],a[i]++;        ans*=(a[i]+1);        if (x<Prime_Number[i]) break;    }    if (x!=1) ans*=2;    return ans;}int main(){    read(T);    Pretreatment();    while (T--)    {        h=0;        read(h);        if(h<=Hmax) printf("%d\n",Calculation(h+1));        else printf("%d\n",Calculation2(h+1));    }    return 0;}
1 0