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 ...
随机推荐
- iOS 倒计时NSTimer
项目中可能会遇到有些倒计时的地方 比方 手机验证的时候,验证码一般都会有一个时间限制,此时在输入验证码的地方就须要展示一个倒计时 详细实现方式是使用了iOS 自带的 NSTimer 上代码 首先新建 ...
- ubuntu下调试ffmpeg程序出现undefined reference to pthread_once ,undefined reference to uncompress错误
Ubuntu(版本16.04)下默认配置编译Ffmpeg(版本4.1.3configure 添加选项--enable-threads),将编译好的ffmpeg库添加到程序 中进行编译出现undefin ...
- MacBook Pro使用初体验之Mac快捷键汇总(持续更新中)
我于近日购置了一台13寸的MacBook Pro高配,打算開始进行iOS开发的学习.Pro的配置情况例如以下: (1)OS X Yosemite ,版本号10.10.3 (2)Retina显示屏,13 ...
- 从S3中导入数据到Dynamodb
本节如果你已经从Dynamodb中导出过数据,而且导出的文件以及被存入S3.文件内部结构会在Verify Data Export File 中描写叙述. 我们称之前导出数据的原始表为source ta ...
- iOS开发者必备:四款后端服务工具
本文转载至 http://mobile.51cto.com/iphone-411917.htm 对于开发者来说,连接后端数据或许是一件特别痛苦的事情.但后端服务却能够帮助开发人员以更快的速度构建移动应 ...
- 【BZOJ3007】拯救小云公主 二分+几何+对偶图
[BZOJ3007]拯救小云公主 Description 英雄又即将踏上拯救公主的道路…… 这次的拯救目标是——爱和正义的小云公主. 英雄来到boss的洞穴门口,他一下子就懵了 ...
- 【BZOJ3566】[SHOI2014]概率充电器 期望+树形DP
[BZOJ3566][SHOI2014]概率充电器 Description 著名的电子产品品牌 SHOI 刚刚发布了引领世界潮流的下一代电子产品——概率充电器:“采用全新纳米级加工技术,实现元件与导线 ...
- 【BZOJ2561】最小生成树 最小割
[BZOJ2561]最小生成树 Description 给定一个边带正权的连通无向图G=(V,E),其中N=|V|,M=|E|,N个点从1到N依次编号,给定三个正整数u,v,和L (u≠v),假设现在 ...
- Collection of Boot Sector Formats for ISO 9660 Images
http://bazaar.launchpad.net/~libburnia-team/libisofs/scdbackup/view/head:/doc/boot_sectors.txt Colle ...
- 浏览器访问配置完成的ftp服务器
在浏览器的地址栏输入: ftp://testuser:testuser@192.168.10.4 testuser 是ftp的用户名和密码: 192.168.10.4 是ftp服务器的IP地址. 亲测 ...