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 ...
随机推荐
- Windows socket I/O模型 之 select(2)
在Windows socket I/O模型 之 select(1)中.我们仅仅是在console中简单的模拟了select的处理方法. 还有非常多特性不能改动.比方仅仅能写,不能读. 没使用线程.也 ...
- 转:scanf的用法
https://blog.csdn.net/u012421456/article/details/18501309 scanf()[通过键盘将数据输入到变量中] 它有两种用法: 用法一: scanf( ...
- python--内置函数补充
内置函数补充 基础数据类型相关 和数字相关:数据类型: bool 布尔 int 整型 float 浮点 complex 复数 只有在强转中会用进制转换: bin() 二进制转换 比如0b开头表示的是二 ...
- ListView优化总结(二)--Android
3.使用Activity和Delegate与适配器交互 这个内容是从书里看到的,通过托付模式帮助开发人员把全部的业务逻辑从适配器中移到Activity中. 以下是加入电话号码的样例,列表中每一行都有一 ...
- python staticmethod和classmethod(转载)
staticmethod, classmethod 分别被称为静态方法和类方法. staticmethod 基本上和一个全局函数差不多,只不过可以通过类或类的实例对象(python里只说对象总是容易产 ...
- UNIX网络编程卷1 时间获取程序client TCP 使用非堵塞connect
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie 1.当在一个非堵塞的 TCP 套接字(可使用 fcntl 把套接字变成非堵塞的)上调用 co ...
- android studio 程序真机执行中文显示乱码
代码里中文显示正常,真机执行后中文显示乱码,解决的方法: build.gradle中加入一句 android { compileOptions.encoding = "GBK" }
- OTL中文乱码 OTL UTF8
在用unixODBC连接MySQL的时候字符编码是由odbc支持的,不须要C++编译OTL的时候加上什么编译条件. 假设你的数据库使用的编码是UTF-8,你要从这个数据库读数据.并且还要将结果放到这个 ...
- php 中 isset 和empty 的区别
昨天终于开始学习php了,这个对于我来说听着很熟悉,但是学起来很陌生的东西,尤其是课上能听明白 但是真正写起了手生,都不知道手该往哪里放了,天哪~~~ 其中课上有讲到 isset和empty的区别,现 ...
- 2016/07/07 PHP的线程安全与非线程安全版本的区别
Windows版的PHP从版本5.2.1开始有Thread Safe(线程安全)和None Thread Safe(NTS,非线程安全)之分,这两者不同在于何处?到底应该用哪种?这里做一个简单的介绍. ...