Visible Trees
数论:容斥原理
题意:
给定一个 m * n 的方格,左下角的点为(1,1),右上角的点(n,m),然后每个方格都是一棵树,然后一个人站在(0,0)点,往森林里看,当两棵树在一条线上的时候,它只能看到最前面的那个点,后面的树就不计了,计算能够看到多少颗树。
思路:
先筛选法求出所有素因子,再根据容斥原理求每一行的互质点的个数,最后求和。
#include <queue>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
using namespace std;
int p_rime[100005][20];//素因子
int c[100005];//计数
void init()
{
memset(c,0,sizeof(c));
for(int i=2;i<=100000;i++)
{
if(c[i])continue;
c[i]=1;
p_rime[i][0]=i;
for(int j=2;i*j<=100000;j++)
{
p_rime[i*j][c[i*j]++]=i;
}
}
}
LL rongchi(int m, int n, int i)
{
LL s = 0;
for(int j=i;j<c[m];j++)
s+=n/p_rime[m][j]-rongchi(m,n/p_rime[m][j],j+1);
return s;
}
int main()
{
init();
int t, n, m;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&m,&n);
LL ans = m;
for(int i=2;i<=n;i++)
ans+=m-rongchi(i,m,0);
printf("%lld\n",ans);
}
return 0;
}