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[11];

LL calc(int n,int m)    //1~n中和 m互质的数字个数
{
    if(n==0) return 0LL;
    int tmp = m;
    int tot = 0;
    for(int i=2;i*(LL)i<=(LL)m;i++)
    {
        if(tmp%i==0)
        {
            while(tmp%i==0) tmp/=i;
            num[++tot] = i;
        }
    }
    if(tmp>1) num[++tot] = tmp;
    LL ans = 0;
    for(int S=0;S<(1<<tot);S++)
    {
        int tmp = 1,k = 1;
        for(int i=0;i<tot;i++) if(bit(i)&S) tmp *= num[i+1], k = -k;
        ans += k * (n/tmp);
    }
    return ans % P;
}

LL calc(int t)
{
    if(t == 0) return 0LL;
    LL ans = 0;
    for(int d=1;d*(LL)d<=(LL)a;d++)
        if(a%d==0)
        {
            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 0;
}

时间: 2024-10-13 12:43:32

counting the numbers的相关文章

Lintcode31 Partition Array solution题解

[题目描述] Given an array nums of integers and an int k, partition the array (i.e move the elements in "nums") such that:All elements < k are moved to the left;All elements >= k are moved to the right;Return the partitioning index, i.e the fir

LintCode-Partition Array

Given an array "nums" of integers and an int "k", Partition the array (i.e move the elements in "nums") such that, * All elements < k are moved to the left * All elements >= k are moved to the right Return the partition

高精度练习

题目地址:http://acm.hust.edu.cn/vjudge/problem/30812 Xiao Ming's Hope Description Xiao Ming likes counting numbers very much, especially he is fond of counting odd numbers. Maybe he thinks it is the best way to show he is alone without a girl friend. The

HDU 4349 Xiao Ming&#39;s Hope 找规律

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4349 Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1723    Accepted Submission(s): 1144 Problem Description Xiao Ming likes coun

Partition Array

Given an array nums of integers and an int k, partition the array (i.e move the elements in "nums") such that: All elements < k are moved to the left All elements >= k are moved to the right Return the partitioning index, i.e the first ind

《算法竞赛入门经典——训练指南》第二章题库

UVa特别题库 UVa网站专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=442 注意,下面注有"extra"的习题并没有在书中出现,但在上面的特别题库中有,属于附加习题. 基础练习 (Basic Problems) UVa11388 GCD LCM UVa11889 Benefit UVa10943 How do y

hdu 4349 Xiao Ming&#39;s Hope 规律

Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Xiao Ming likes counting numbers very much, especially he is fond of counting odd numbers. Maybe he thinks it is the best way to

HDU 4349 Xiao Ming&#39;s Hope lucas定理

Xiao Ming's Hope Time Limit:1000MS     Memory Limit:32768KB Description Xiao Ming likes counting numbers very much, especially he is fond of counting odd numbers. Maybe he thinks it is the best way to show he is alone without a girl friend. The day 2

lintcode-medium-Partition Array

Given an array nums of integers and an int k, partition the array (i.e move the elements in "nums") such that: All elements < k are moved to the left All elements >= k are moved to the right Return the partitioning index, i.e the first ind