• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Hug_Sea
博客园    首页    新随笔    联系   管理    订阅  订阅

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 } 
posted @ 2012-10-11 10:56  Hug_Sea  阅读(196)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3