HDU

来源:互联网 发布:外贸通软件 编辑:程序博客网 时间:2024/05/21 22:37

莫比乌斯反演裸题,描述很明确,就不翻译了。
Description

There are many trees forming a m * n grid, the grid starts from (1,1). Farmer Sherlock is standing at (0,0) point. He wonders how many trees he can see.

If two trees and Sherlock are in one line, Farmer Sherlock can only see the tree nearest to him.

Input
The first line contains one integer t, represents the number of test cases. Then there are multiple test cases. For each test case there is one line containing two integers m and n(1 ≤ m, n ≤ 100000)

Output
For each test case output one line represents the number of trees Farmer Sherlock can see.

Sample Input
2
1 1
2 3

Sample Output
1
5

#include <bits/stdc++.h>using namespace std;typedef long long ll;#define mem(s,t) memset(s,t,sizeof(s))#define D(v) cout<<#v<<" "<<v<<endl#define inf 0x3f3f3f3fconst int maxn =100000+5;ll tot=0,n,m;int miu[maxn],prime[maxn];bool isPrime[maxn];void getmiu(){    mem(isPrime,true);    miu[1]=1;    for(int i=2;i<=maxn;i++){        if(isPrime[i]) prime[tot++]=i,miu[i]=-1;        for(int j=0;j<=tot;j++){            if(i*prime[j]>=maxn) break;            isPrime[i*prime[j]]=false;            if(i%prime[j]==0){                miu[i*prime[j]]=0;                break;            }else miu[i*prime[j]]=-1*miu[i];        }    }}void solve(){    int T;scanf("%d",&T);    while(T--){        ll ans=0;        scanf("%d%d",&m,&n);        int minn=min(m,n);        for(ll i=1;i<=minn;i++){            ans+=(ll)(m/i)*(n/i)*miu[i];        }        printf("%lld\n",ans);    }}int main(){#ifdef LOCAL    freopen("in.txt","r",stdin);    freopen("out.txt","w",stdout);#endif    getmiu();    solve();    return 0;}
1 0
原创粉丝点击