hdu 2841 Visible Trees (容斥原理)

给出一个矩阵问在点(0,0)的人最多能看得到多少点。发现规律:对于这些点的坐标,只要坐标互质就可以看到。那么就转化为求区间(1,n)和(1,m)是互质的数对的对数。容斥原理。

#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define B(x) (1<<(x))
void cmax(int& a,int b){ if(b>a)a=b; }
void cmin(int& a,int b){ if(b<a)a=b; }
typedef long long ll;
const int oo=0x3f3f3f3f;
const ll OO=1LL<<61;
const int MOD=1000007;
const int maxn=100005;
int p[maxn][15],f[maxn];
int num[maxn];

void Init(){
    for(int i=2;i<maxn;i++){
        if(f[i])continue;
        for(int j=i;j<maxn;j+=i){
            f[j]=1;
            p[j][num[j]++]=i;
        }
    }
}

ll dfs(int s,int x,int a){
    ll res=0;
    for(int i=s;i<num[a];i++){
        res+=x/p[a][i]-dfs(i+1,x/p[a][i],a);
    }
    return res;
}

int main(){
    //freopen("E:\\read.txt","r",stdin);
    int n,m,T;
    ll ans;
    Init();
    scanf("%d",&T);
    while(T--){
        scanf("%d %d",&n,&m);
        ans=0;
        for(int i=1;i<=m;i++){
            ans+=n-dfs(0,n,i);
        }
        printf("%I64d\n",ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值