****HDU

来源:互联网 发布:稀疏编码算法 详解 编辑:程序博客网 时间:2024/06/06 07:21

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

Hint

题意

一条直线上的点不能被看到 求能看到的树的数目

题解:

可以发现gcd(x,y)!=1的点都不能被看到 所以用容斥定理来做

AC代码

#include <cstdio>#include <iostream>#include <map>#include <set>#include <vector>#include <cstring>#include <algorithm>using namespace std;typedef long long LL;const int N = 1e5+5;int prime[N],phi[N];vector<int > yinzi[N];int tot = 0;void init(){    phi[1] = 1;    for (int i = 2; i < N; ++i){        if (!phi[i]){            phi[i]=i-1;            prime[tot++]=i;        }        for (int j = 0; j < tot&&1ll*prime[j]*i<N; ++j){            if (i%prime[j]) phi[i*prime[j]] = phi[i]*(prime[j]-1);             else {                phi[i*prime[j]] = phi[i]*prime[j];                break;             }        }    }    for (int i = 2; i < N; ++i){        int tmp = i;        for (int j = 0;(LL)prime[j]*prime[j]<=tmp; ++j){            if (tmp%prime[j]==0){                yinzi[i].push_back(prime[j]);                tmp/=prime[j];                while(tmp%prime[j]==0) tmp/=prime[j];            }            if (tmp == 1) break;        }        if (tmp>1) yinzi[i].push_back(tmp);    }}LL dfs(int x,int n,int now){    LL res = 0;    for (int i = x; i < yinzi[now].size(); ++i){        res += n/yinzi[now][i]-dfs(i+1,n/yinzi[now][i],now);    }    return res;}int main(){    init();    int t;    scanf("%d",&t);    while (t--){        int n,m;        scanf("%d%d",&n,&m);        if (n>m) swap(n,m);        LL ans = 0;        /*欧拉函数是小于n所有与n互质的数目 这个包括1 但这一题1 3互质与 3 1互质是两个点*/        for (int i = 1; i <= n; ++i){            if (i!=1) ans+=phi[i]*2;            else ans+=phi[i];        }/*对于求1~n与1~n之间的互质的种数(与点对数不同 因为gcd(x,y)==1和gcd(y,x)==1算是一种) 所以要看清问的是什么 虽然欧拉函数是问的小于n所有与n互质的数目 但由于前面所说 所以1~n欧拉函数求和即可算出互质的种数*/         for (int j = n+1; j <= m; ++j){            ans += n-dfs(0,n,j);        }        printf("%lld\n",ans);    }    return 0;}/*比如对于phi[6] = 2  1 6互质 5 6互质 题目问题是求互质的点对数 那么除了1 6 实际还有6 1这个点 1 1点只有一对*/

纯容斥写法 之前一直wa在1e5这个边界上

#include <cstdio>#include <map>#include <cmath>#include <cstring>#include <vector>#include <algorithm>using namespace std;typedef long long LL;const int N = 1e5+10;int phi[N];int prime[N];vector<int> yinzi[N];int tot = 0;void init(){    phi[1] = 1;    for (int i = 2; i < N; ++i){        if (!phi[i]){            prime[tot++] = i;            phi[i] = i-1;        }        for (int j = 0;j < tot&&1ll*i*prime[j]<N; ++j){            if (i%prime[j]) phi[i*prime[j]] = phi[i]*(prime[j]-1);            else {                phi[i*prime[j]] = phi[i]*prime[j];                break;            }        }    }    for (int i = 1; i <= 1e5; ++i){        int tmp = i;        for (int j = 0; (LL)prime[j]*prime[j]<=tmp; ++j ){            if (tmp%prime[j]==0){                yinzi[i].push_back(prime[j]);                while (tmp%prime[j]==0) tmp/=prime[j];            }            if (tmp==1) break;        }if (tmp>1) yinzi[i].push_back(tmp);    }}LL ronc(int a,int b,int now){    LL res = 0;    for (int i = a; i < yinzi[now].size(); ++i){        res += b/yinzi[now][i]-ronc(i+1,b/yinzi[now][i],now);    }    return res;}int main(){    init();    int t;    scanf("%d",&t);    while (t--){        LL n,m;        scanf("%lld%lld",&n,&m);        if (n>m) swap(n,m);        LL ans = 0;        for (int i = 1;i <= m; ++i){            ans += n-ronc(0,n,i);        }        printf("%lld\n",ans);    }    return 0;}

??这题还可以莫比乌斯反演啊啊啊

原创粉丝点击