HDU-2841 Visible Trees

题意:就是给定一个坐标(n,m),求(1,1)到(n,m)区间内x与y互质的坐标数。

思路:利用容斥从2到n,遍历与m互质的个数。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2841

 

View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <string>
 7 #include <algorithm>
 8 using namespace std;
 9 #define LL __int64
10 #define N 100005
11 
12 int t,n,m;
13 LL prime[N];
14 
15 int dfs(LL x,LL n){
16     int ct=0;
17     for(int i=2;i*i<=x;i++)
18         if(x%i==0){
19             prime[ct++]=i;
20             while(x%i==0) x/=i;
21         }
22     if(x>1) prime[ct++]=x;
23     LL sum=0;
24     for(int i=1;i<1<<ct;i++){
25         LL val=1,k=0;
26         for(int j=0;j<ct;j++){
27             if(i&1<<j){
28                 val*=prime[j];
29                 k++;
30             }
31         }
32         if(k&1) sum+=n/val;
33         else sum-=n/val;
34     }
35     return n-sum;
36 }
37 
38 int main(){
39     
40 //    freopen("data.in","r",stdin);
41 //    freopen("data.out","w",stdout);
42     
43     scanf("%d",&t);
44     while(t--){
45         scanf("%d%d",&n,&m);
46         LL sum=m;
47         for(LL i=2;i<=n;i++)
48             sum+=dfs(i,m);
49         printf("%I64d\n",sum);
50     }
51     return 0;
52 } 

转载于:https://www.cnblogs.com/Hug-Sea/articles/2719573.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值