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 ;
}
最新文章
- eCharts 数据转换json
- Cannot instantiate the type AppiumDriver
- kali 更改root密码
- hdu5878(枚举,打表)
- 【转】Android Studio系列教程一--下载与安装
- 【转】Android 使用Scroller实现绚丽的ListView左右滑动删除Item效果
- Linux流量监控工具使用总结 - iftop
- 一起看看2016中国第三届CSS开发者大会有哪些大咖演讲
- S5PV210中断处理
- linux下boost的安装与编译
- OOP的基本原则
- php密码对称encrypt加密
- 解决Git Revert操作后再次Merge代码被冲掉的问题
- maven 本地仓库无法更新到最新版本的jar包
- HAOI2016 简要题解
- java虚拟机的内存模型
- DevC++出现[Error] ld returned 1 exit status,如何解决才好呢?
- ;,&;,&;&;,shell,区别
- C语言实现输出杨辉三角
- C语言学习记录_2019.02.05