题意:

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.

思路:

等价转换关系

整除质数<==>有这个质因子

[lo,hi]<==>[1,hi]  - [1,lo-1]

首先考虑直接整除的。

对于区间[1,x],个数为x/divisor个

其次考虑后面的量

注意到之间是乘法关系。所以每个量都可以考虑。

假设n-k可以被divisor整除,那么相当与n是在每个divisor整除数的后k位。

这里有一个性质:这些n间隔也是divisor

考虑重复问题:如果%divisor相等,则可能重复。

k越小,可包含的区间越大。所以从小到大处理k就好。如果取模出现过,就不计算。因为一定已经被之前的计算过了。

至于计算区间 对于[0,x], k

就是[k+1,x].(如果k>x那就=0,是k+1的原因是题目要求>0,即不能从0偏移k得到。)

个数就是(x-k)/divisor

代码

#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define REP(I,N) FOR(I,0,N)
#define ALL(A) (A).begin(), (A).end() class SparseFactorialDiv2
{
public:
long long getCount(long long lo, long long hi, long long divisor)
{
int vis[];
REP(i, ) vis[i] = ;
long long ans = ;
for (long long i = ; i*i<hi; i++) {
long long mod = (i*i)%divisor;
if (!vis[mod]) {
vis[mod] = ;
long long nlo = ;
if (lo- > i*i) nlo = (lo--i*i)/divisor;
long long nhi = ;
nhi = (hi-i*i)/divisor;
ans += nhi-nlo;
}
}
return ans;
}
};
 

TopCoder SRM596 DIV2 1000: SparseFactorialDiv2的更多相关文章

  1. Topcoder Srm 673 Div2 1000 BearPermutations2

    \(>Topcoder \space Srm \space 673 \space Div2 \space 1000 \space BearPermutations2<\) 题目大意 : 对 ...

  2. Topcoder Srm 671 Div2 1000 BearDestroysDiv2

    \(>Topcoder \space Srm \space 671 \space Div2 \space 1000 \space BearDestroysDiv2<\) 题目大意 : 有一 ...

  3. SRM 146 DIV2 1000

    Problem Statement      A well-known riddle goes like this: Four people are crossing an old bridge. T ...

  4. TopCoder SRM500 Div1 1000 其他

    原文链接https://www.cnblogs.com/zhouzhendong/p/SRM500-1000.html SRM500 Div1 1000 设 \(v_1,v_2,\cdots ,v_9 ...

  5. TopCoder SRM502 Div1 1000 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/SRM502-1000.html SRM502 Div1 1000 题意 从 [0,n-1] 中选择 k 个不同的 ...

  6. topcoder 649 DIV2

    8 A:模拟 9:B:终于看懂题目... 题意:最多分解K次 每分钟一个数可以分解成两个数 或者-1: 关键字:DP,记忆花搜索. DP[I][J]=min(dp[i][j],1+max(dp[ii] ...

  7. SRM 595 DIV2 1000

    数位DP的感觉,但是跟模版不是一个套路的,看的题解,代码好理解,但是确实难想. #include <cstdio> #include <cstring> #include &l ...

  8. TC SRM 593 DIV2 1000

    很棒的DP,不过没想出,看题解了..思维很重要. #include <iostream> #include <cstdio> #include <cstring> ...

  9. TC SRM 591 DIV2 1000

    很不错的一题,非常巧妙的用DP顺序解决这个问题... 可以发现,只和A里面最小的有关系... #include <cstdio> #include <cstring> #inc ...

随机推荐

  1. poj1654 Area

    题目描述: vjudge POJ 题解: 本以为是水题结果是神题 计算几何求多边形面积. 考虑到结果一定是整数或者整数/2,我们应该用long long 来存…… 用double会死…… 还有日常只能 ...

  2. Git基本操作笔记:初始化,用户设置,撤销修改

    1. Git 初始化 git init git  remote add repos_name repos_url git add . git commit -m 'commit message' gi ...

  3. 一个form表单对应多个submit

    一个form表单多个submit 在平时项目开发过程中,经常会遇到一个form表单对应多个submit提交的情况,那么 ,这种情况应该怎么解决呢,也很简单,这时候就不能用submit来提交了,可以通过 ...

  4. Mac中文乱码问题

    在终端切换到文档所在的目录,输入下面的命令: iconv -c -f GB2312 -t UTF-8 乱码的文件名 >> 新文件的名称

  5. Aizu - 1386 Starting a Scenic Railroad Service (思维乱搞)

    给你n个区间,求: 1:最多有多少区间与同一个区间相交. 2:相交部分的最大区间数目. Sample Input 1 4 1 3 1 3 3 6 3 6 Sample Output 1 2 2 Sam ...

  6. German Collegiate Programming Contest 2015 计蒜课

    // Change of Scenery 1 #include <iostream> #include <cstdio> #include <algorithm> ...

  7. cf 1020 C

    C. Elections time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  8. Mysql主键一致时,可以进行在元数据上的操作

    insert into daliy_hit_counter(day,slot,cnt) values(12,12,1) on duplicate key update cnt = cnt +1 dal ...

  9. tornado中文教程

    http://docs.pythontab.com/tornado/introduction-to-tornado/ch2.html#ch2-1 python的各种库的中文教程 http://docs ...

  10. webstorm自带debugger服务器

    打开webstorm->settings->Build,Execution,Deployment->Debugger->把端口Port改成8089或者其他80端口,按确定就可以 ...