hdu 5663 Hillan and the girl 莫比乌斯反演

Hillan and the girl

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)

Problem Description

“WTF!
While everyone has his girl(gay) friend, I only have my keyboard!”
Tired of watching others‘ affair, Hillan burst into scream, which made
him decide not to hold it back.
“All right, I am giving you a
question. If you answer correctly, I will be your girl friend.” After
listening to Hillan, Girl replied, “What is the value of ∑ni=1∑mj=1f(i,j), where f(i,j)=0 if gcd(i,j) is a square number and f(i,j)=1 if gcd(i,j) is not a square number(gcd(i,j) means the greatest common divisor of x and y)?”
But Hillan didn‘t have enough Intelligence Quotient to give the right answer. So he turn to you for help.

Input

The first line contains an integer T(1≤T≤10,000)——The number of the test cases.
For each test case, the only line contains two integers n,m(1≤n,m≤10,000,000) with a white space separated.

Output

For each test case, the only line contains a integer that is the answer.

Sample Input

2
1 2333333
10 10

Sample Output

0
33

Hint

In the first test case, obviously $f\left(i,j\right)$ always equals to 0, because $i$ always equals to 1 and $\gcd\left(i,j\right)$ is always a square number(always equals to 1).

Source

BestCoder Round #79 (div.2)

min(n,m) min(n/k,m/k)

思路:首先推到∑     ∑ mu(d)  * [n/k/d] * [m/k/d];  k为完全平方数;

      k=1   d=1

   令T=k*d;

   可得:

      min(n,m)                    

      ∑   [n/T] * [m/T]  ∑   mu(T/k)  ;

      T          k|T

      令gg数组等于 ∑   mu(T/k)  相当于原来的mu函数;

             k|T

      和原来一样分块即可;

#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define esp 0.00000000001
#define pi 4*atan(1)
const int N=1e7+10,M=1e7+10,inf=1e9+10,mod=1e9+7;
int mu[N], p[N], np[N], cnt, sum[N];
ll gg[N];
void init() {
    mu[1]=1;
    for(int i=2; i<N; ++i) {
        if(!np[i]) p[++cnt]=i, mu[i]=-1;
        for(int j=1; j<=cnt && i*p[j]<N; ++j) {
            int t=i*p[j];
            np[t]=1;
            if(i%p[j]==0) { mu[t]=0; break; }
            mu[t]=-mu[i];
        }
    }
    for(int i=1;i*i<N;i++)
    {
        for(int t=i*i;t<N;t+=(i*i))
        sum[t]+=mu[t/i/i];
    }
    for(int i=1;i<N;i++)
    gg[i]=gg[i-1]+sum[i];

}
ll getans(ll b,ll d)
{
    if(b>d)swap(b,d);
    ll ans=0;
    for(ll L=1,R=0;L<=b;L=R+1)
    {
        R=min(b/(b/L),d/(d/L));
        ans+=(b/L)*(d/L)*(gg[R]-gg[L-1]);
    }
    return ans;
}
int main()
{
    int T;
    init();
    scanf("%d",&T);
    while(T--)
    {
        ll b,d;
        scanf("%I64d%I64d",&b,&d);
        printf("%I64d\n",(b*d)-getans(b,d));
    }
    return 0;
}
时间: 2024-08-02 18:57:05

hdu 5663 Hillan and the girl 莫比乌斯反演的相关文章

E - GuGuFishtion HDU - 6390(欧拉函数 / 莫比乌斯反演)

GuGuFishtion (HDU - 6390) 题意: 定义\(G_u (a,b)=\frac{\phi(ab)}{\phi(a)\phi(b)}\). 求\((\sum\limits_{a=1}^m\sum\limits_{b=1}^nG_u (a,b))\pmod p\). 题解: 考虑\(\phi(x) = x*(1-\frac{1}{p_1})*(1-\frac{1}{p_2})...*(1-\frac{1}{p_n})\). 将\(G_u (a,b)\)的分子与分母按上述分解.约分

HDU 1695 GCD(容斥 or 莫比乌斯反演)

这题可以用容斥做,然而效率并不高.. 于是学了下莫比乌斯反演(资料百度找) 求出mo数组后 设f(x)为gcd为x的种数 F(x)为gcd为x倍数的种数 那么显然F(x) = (b / x) * (d / x) 莫比乌斯反演之后,得到f(x) = sum(mo[i] * F(i)). 然后还要容斥减去对称重复的.对称重复的情况为min(b, d)小的中,求一遍除2,(因为存在x = y的情况只有(1,1)一种) 最后还要注意特判下k == 0的情况 代码: #include <cstdio>

HDU 4675 GCD of Sequence(莫比乌斯反演 + 打表注意事项)题解

题意: 给出\(M\)和\(a数组\),询问每一个\(d\in[1,M]\),有多少组数组满足:正好修改\(k\)个\(a\)数组里的数使得和原来不同,并且要\(\leq M\),并且\(gcd(a_1,a_2,\dots,a_n)=d\). 思路: 对于每一个\(d\),即求\(f(d)\):修改\(k\)个后\(gcd(a_1,a_2,\dots,a_n)=d\)的对数. 那么假设\(F(d)\):修改\(k\)个后\(gcd(a_1,a_2,\dots,a_n)\)是\(d\)倍数的对数.

hdu 1695 莫比乌斯反演

hdu 1695 莫比乌斯反演 题意: 给出a,b,c,d,k, 求满足a <= x <= b && c <= y <= d && gcd(x,y)=k 的数对(x,y)的对数. 限制: a=c=1; 0 < b,c <= 1e5; (n1,n2) 和 (n2,n1) 算为同种情况 思路: 其实是求满足1 <= x <= b/k && 1 <= y <= d/k && gcd(x,y

hdu 1695 容斥原理或莫比乌斯反演

GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5310    Accepted Submission(s): 1907 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y)

HDU 4746 Mophues (莫比乌斯反演应用)

Mophues Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327670/327670 K (Java/Others) Total Submission(s): 980    Accepted Submission(s): 376 Problem Description As we know, any positive integer C ( C >= 2 ) can be written as the multiply of

HDU - 6715 - 算术 = 莫比乌斯反演

http://acm.hdu.edu.cn/showproblem.php?pid=6715 题意: 求:\(\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}\mu(lcm(i,j))\),其中n,m是1e6范围内,10组. 不会,想了很久,也不知道假在哪里.大概是一开始方向就错了. 正解: 所求:\(\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}\mu(lcm(i,j))\) 即:\(\sum\limits_{i=1}^

HDU 1695 (莫比乌斯反演) GCD

题意: 从区间[1, b]和[1, d]中分别选一个x, y,使得gcd(x, y) = k, 求满足条件的xy的对数(不区分xy的顺序) 分析: 虽然之前写过一个莫比乌斯反演的总结,可遇到这道题还是不知道怎么应用. 这里有关于莫比乌斯反演的知识,而且最后的例题中就有这道题并给出了公式的推导. 1 #include <cstdio> 2 #include <algorithm> 3 typedef long long LL; 4 5 const int maxn = 1000000

hdu.5212.Code(莫比乌斯反演 &amp;&amp; 线性筛)

Code Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 300    Accepted Submission(s): 124 Problem Description WLD likes playing with codes.One day he is writing a function.Howerver,his computer b