SRM 596 1000pt

来源:互联网 发布:淘宝不能发布游戏账号 编辑:程序博客网 时间:2024/05/01 12:28
Problem Statement
   

For an integer n, let F(n) = (n - 0^2) * (n - 1^2) * (n - 2^2) * (n - 3^2) * ... * (n - k^2), where k is the largest integer such that n - k^2 > 0. You are given three long longs lo, hi and divisor. It is guaranteed that divisor will be a prime number. Compute and return the number of integers n between lo and hi, inclusive, such that F(n) is divisible by divisor.

Constraints
-
lo will be between 1 and 1,000,000,000,000, inclusive.
-
hi will be between lo and 1,000,000,000,000, inclusive.
-
divisor will be between 2 and 997, inclusive.
-
divisor will be a prime number.


根据sample可以发现规律,但是一开始我把复杂度考虑错误了,应该是 10^6+10^6 ,我考虑从 10^6 * 10^6 了,这就一直没敢写=、=


实际上只需计算 f(hi)-f(lo-1)


PS:自己的分析能力越来越弱了 =、=


#include<iostream>#include<cstring>using namespace std;class SparseFactorialDiv2{public:int visit[1010];long long cal(long long x,long long divisor){long long ans=0,i;memset(visit,0,sizeof(visit));for(i=0;i*i<=x;i++){if(visit[(i*i) % divisor])continue;visit[(i*i) % divisor]=1;ans+=((x-i*i)/divisor);}return ans;}long long getCount(long long lo, long long hi, long long divisor){return cal(hi,divisor)-cal(lo-1,divisor);}};


原创粉丝点击