counting the numbers
题意:
给定$a,b,c$ ,求解满足 $1 \leq m \leq b, 1 \leq n \leq c, a | mn$ 的 $(m,n)$ 数对个数。
$a \leq INTMAX$, $b \leq LONGLONGMAX$
解法
原问题相当于求解 $mn \ mod \ a \equiv 0$ 的数对个数。
$(m \ mod \ a) n \ mod \ a \equiv 0$
这样$m$ ,实际有效的是 $m \ mod \ a$。
这样我们可以将原问题中的 $m$ 分为 $[\frac{b}{a}]$ 段 $m \equiv 1\ to \ a (mod \ a)$,
与一段 $m \equiv 1 \ to(b \mod \ a) (mod \ a)$
考虑求解 $m ∈ [1,t]$ 的 $(m,n)$ 数对个数 $F(t)$。
这样有$$ans = [b/a]F(a) + F(b \ mod \ a)$$
$$F(t) = \sum_{m=1}^t { [\frac{c}{ \frac{a}{(a,m)} }] }$$
记 $d = (m,a)$
$$F(t) = \sum_{d|a}{ [\frac{c}{ \frac{a}{d} }] (the\ cnt\ of\ m\ that (m,a) = d) }$$
$$F(t) = \sum_{d|a}{ [\frac{c}{ \frac{a}{d} }] (the\ cnt\ of\ m\ that (\frac{m}{d},\frac{a}{d}) = 1) }$$
$$F(t) = \sum_{d|a}{ [\frac{c}{ \frac{a}{d} }] (the\ cnt\ of\ i\ that (i,\frac{a}{d}) = 1 and i \leq [\frac{t}{d}]) }$$
后者可以通过容斥$O(\sqrt {\frac{a}{d}})$ 求
#include <bits/stdc++.h> #define LL long long
#define bit(x) (1<<(x))
#define P 1000000007LL using namespace std; LL b,c;
int a,num[]; LL calc(int n,int m) //1~n中和 m互质的数字个数
{
if(n==) return 0LL;
int tmp = m;
int tot = ;
for(int i=;i*(LL)i<=(LL)m;i++)
{
if(tmp%i==)
{
while(tmp%i==) tmp/=i;
num[++tot] = i;
}
}
if(tmp>) num[++tot] = tmp;
LL ans = ;
for(int S=;S<(<<tot);S++)
{
int tmp = ,k = ;
for(int i=;i<tot;i++) if(bit(i)&S) tmp *= num[i+], k = -k;
ans += k * (n/tmp);
}
return ans % P;
} LL calc(int t)
{
if(t == ) return 0LL;
LL ans = ;
for(int d=;d*(LL)d<=(LL)a;d++)
if(a%d==)
{
int tmpd = d;
ans += (c / (a/tmpd)) % P * calc(t/tmpd,a/tmpd) % P;
if(ans >= P) ans -= P;
if(d*d != a)
{
tmpd = a/d;
ans += (c / (a/tmpd)) % P * calc(t/tmpd,a/tmpd) % P;
if(ans >= P) ans -= P;
}
}
return ans;
} int main()
{
while(cin>>a>>b>>c)
{
LL ans = (b/a)%P * calc(a)%P + calc(b%(LL)a)%P;
cout << ans % P << endl;
}
return ;
}
counting the numbers的更多相关文章
- HDU 4349 Xiao Ming's Hope lucas定理
Xiao Ming's Hope Time Limit:1000MS Memory Limit:32768KB Description Xiao Ming likes counting nu ...
- Lintcode: Partition Array
Given an array "nums" of integers and an int "k", Partition the array (i.e move ...
- Hdu4349 Xiao Ming's Hope
Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Partition Array
Given an array nums of integers and an int k, partition the array (i.e move the elements in "nu ...
- hdu 4349 Xiao Ming's Hope 规律
Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Weekly Contest 128
1012. Complement of Base 10 Integer Every non-negative integer N has a binary representation. For e ...
- HDU 4349——Xiao Ming's Hope——————【Lucas定理】
Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4349 Xiao Ming's Hope 找规律
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4349 Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/ ...
- lintcode刷题笔记(一)
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. L ...
随机推荐
- 数据挖掘 与 Web开发何去何从
(0)引子 以下以现实生活中的一个实例引出本博客的探究点.或许类似的情况正发生在你的身边. 小弟工作5年了,近期有点迷茫. 上一份工作在一家比較大的门户站点做web开发和移动互联网数据挖掘(人手比較紧 ...
- 【网络协议】TCP的流量控制机制
一般来说,我们总是希望传输数据的更快一些,但假设发送方把数据发送的非常快.而接收方来不及接收,这就可能造成数据的丢失.流量控制就是让发送方的发送速率不要太快.让接收方来得及接收. 对于成块数据流,TC ...
- 后台运行命令:&和nohup command & 以及关闭、查看后台任务
当我们在终端或控制台工作时.可能不希望由于执行一个作业而占住了屏幕,由于可能还有更重要的事情要做,比方阅读电子邮件. 对于密集訪问磁盘的进程,我们更希望它可以在每天的非负荷高峰时间段执行(比如凌晨). ...
- Linux学习笔记——例说makefile 综合案例
0.前言 从学习C语言開始就慢慢開始接触makefile,查阅了非常多的makefile的资料但总感觉没有真正掌握makefile,假设自己动手写一个makefile总认为非常吃力.所以特意借 ...
- slidemenu
1. 在github上有一个效果不错的开源库,SlidingMenu 最新的代码下载下来后,会报错: No resource found that matches the given name: at ...
- html的dtd声明
其实DOCTYPE声明,因为很多时候团队里没有做规范应该用哪个,而且几种不同的编辑工具新建出的html页面标准也不同:这就可能一个jsp页面写了几百行甚至上千行了,然后发现某个样式必须要改DOCTYP ...
- xlua学习过程遇到的问题,以后通了之后可能就不是问题了。但是还是有记录的必要。
//2.加载lua文件,这里这种方式只能够加载Resources文件夹下面的,并且是lua.txt类型的文件,感觉没啥乱用. //文档你说的是Resources文件夹下面的才需要加txt后缀,那么就是 ...
- RTSPClient工具EasyRTSPClient支持H.265,支持海思等各种芯片平台
EasyRTSPClient是EasyDarwin开源流媒体团队开发.提供的一套非常稳定.易用.支持重连的RTSPClient工具,接口调用非常简单,再也不用像调用live555那样处理整个RTSP ...
- GO 入门(一)
1.下载安装go环境 https://golang.org/dl/ 2.检查环境变量配置情况,安装过程中会自动配置:GOROOT 和 Path 3.建立go工作区,并配置 ...
- mongoose基于mongodb的数据评论设计
var CommentSchema = { data:{type: ObjectId, ref:'Data'}, //Data数据表,此处存数据id from:{type: ObjectId, ref ...