TopCoder SRM596 DIV2 1000: SparseFactorialDiv2
题意:
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的更多相关文章
- Topcoder Srm 673 Div2 1000 BearPermutations2
\(>Topcoder \space Srm \space 673 \space Div2 \space 1000 \space BearPermutations2<\) 题目大意 : 对 ...
- Topcoder Srm 671 Div2 1000 BearDestroysDiv2
\(>Topcoder \space Srm \space 671 \space Div2 \space 1000 \space BearDestroysDiv2<\) 题目大意 : 有一 ...
- SRM 146 DIV2 1000
Problem Statement A well-known riddle goes like this: Four people are crossing an old bridge. T ...
- TopCoder SRM500 Div1 1000 其他
原文链接https://www.cnblogs.com/zhouzhendong/p/SRM500-1000.html SRM500 Div1 1000 设 \(v_1,v_2,\cdots ,v_9 ...
- TopCoder SRM502 Div1 1000 动态规划
原文链接https://www.cnblogs.com/zhouzhendong/p/SRM502-1000.html SRM502 Div1 1000 题意 从 [0,n-1] 中选择 k 个不同的 ...
- topcoder 649 DIV2
8 A:模拟 9:B:终于看懂题目... 题意:最多分解K次 每分钟一个数可以分解成两个数 或者-1: 关键字:DP,记忆花搜索. DP[I][J]=min(dp[i][j],1+max(dp[ii] ...
- SRM 595 DIV2 1000
数位DP的感觉,但是跟模版不是一个套路的,看的题解,代码好理解,但是确实难想. #include <cstdio> #include <cstring> #include &l ...
- TC SRM 593 DIV2 1000
很棒的DP,不过没想出,看题解了..思维很重要. #include <iostream> #include <cstdio> #include <cstring> ...
- TC SRM 591 DIV2 1000
很不错的一题,非常巧妙的用DP顺序解决这个问题... 可以发现,只和A里面最小的有关系... #include <cstdio> #include <cstring> #inc ...
随机推荐
- Vue基础指令集锦
v-model双向绑定数据 <input type="text" v-model="msg"> {{msg}} ###v-on 事件 <div ...
- Linux远程传输命令scp
指令:scp在不同的linux主机间复制文件带有Security的文件copy,基于ssh登录. 有些linux发行版没有自带scp,因此需要安装scp# yum -y install openssh ...
- leetcode-9-basic-binary search
278. First Bad Version You are a product manager and currently leading a team to develop a new produ ...
- AD采样求平均STM32实现
iADC_read(, &u16NTC_1_Sample_Val_ARR[]); == ui8FirstSampleFlag) { ; i<; i++) { u16NTC_1_Sampl ...
- POJ:3228-Gold Transportation(要求最小生成树最大边最小)
Gold Transportation Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3079 Accepted: 1101 D ...
- UVa 1407 树形背包 Caves
这道题可以和POJ 2486 树形背包DP Apple Tree比较着来做. 参考题解 #include <iostream> #include <cstdio> #inclu ...
- clr(Windows 运行时和公共语言运行时)
Windows 运行时 编译器使用 COM 引用计数机制来确定对象是否不再使用并可以删除. 因为从 Windows 运行时接口派生的对象实际上是 COM 对象,所以这是可行的. 在创建或复制对象时 ...
- [python] 求大神解释下 面向对象中方法和属性
面向对象中 类方法 实例方法 类属性 实例属性该如何理解呢?
- mvc “System.NullReferenceException”类型的异常在 App_Web_zo44wdaq.dll 中发生,但未在用户代码中进行处理 其他信息: 未将对象引用设置到对象的实例。
“System.NullReferenceException”类型的异常在 App_Web_zo44wdaq.dll 中发生,但未在用户代码中进行处理 其他信息: 未将对象引用设置到对象的实例. 解决 ...
- ccna 闫辉单臂路由 和 acl access control list
ccna 闫辉单臂路由 和 acl access control list 一单臂路由 当前园区网设计很少用到 成本低 小型的.局域网可用 二ACL acc ...