UVa 10214 (莫比乌斯反演 or 欧拉函数) Trees in a Wood.
题意:
这道题和POJ 3090很相似,求|x|≤a,|y|≤b 中站在原点可见的整点的个数K,所有的整点个数为N(除去原点),求K/N

分析:
坐标轴上有四个可见的点,因为每个象限可见的点数都是一样的,所以我们只要求出第一象限可见的点数然后×4+4,即是K。
可见的点满足gcd(x, y) = 1,于是将问题转化为x∈[1, a], y∈[1, b],求gcd(x, y) = 1的个数。
类比HDU 1695可以用莫比乌斯反演来做,我还写了普通的和分块加速的两份代码,交上去发现运行时间相差并不是太多。
#include <cstdio>
#include <algorithm>
typedef long long LL; const int maxn = ;
int mu[maxn+], vis[maxn+], prime[maxn]; void Mobius()
{
mu[] = ;
int cnt = ;
for(int i = ; i <= maxn; ++i)
{
if(!vis[i])
{
mu[i] = -;
prime[cnt++] = i;
}
for(int j = ; j < cnt && (LL)i*prime[j] <= maxn; ++j)
{
vis[i*prime[j]] = ;
if(i % prime[j] != ) mu[i*prime[j]] = -mu[i];
else
{
mu[i*prime[j]] = ;
break;
}
}
}
//计算前缀和,用于分块加速
for(int i = ; i <= ; ++i) mu[i] += mu[i-]; } int main()
{
Mobius();
int a, b;
while(scanf("%d%d", &a, &b) == )
{
if(a == && b == ) break;
LL K = , N = (LL)(a*+) * (b*+) - ;
if(a > b) std::swap(a, b);
for(int i = , j; i <= a; i = j + )
{
j = std::min(a/(a/i), b/(b/i));
K += (LL)(mu[j] - mu[i-]) * (a/i) * (b/i);
}
//for(int i = 1; i <= a; ++i) K += (LL) mu[i] * (a/i) * (b/i);
K = (K+)*; printf("%.7f\n", (double) K / N);
} return ;
}
代码君
也可以按照紫书上的思路求欧拉函数,因为a的范围比较小,所以可以逐列统计。不过这个方法要比上面的莫比乌斯反演慢得多,试验了下,不管是打表还是单独计算时间都差不多
#include <cstdio>
#include <cmath>
typedef long long LL; int phi(int n)
{
int m = sqrt(n + 0.5);
int ans = n;
for(int i = ; i <= m; ++i) if(n % i == )
{
ans = ans / i * (i-);
while(n % i == ) n /= i;
}
if(n > ) ans = ans / n * (n-);
return ans;
} int gcd(int a, int b)
{
return b == ? a : gcd(b, a%b);
} int main()
{
int a, b;
while(scanf("%d%d", &a, &b) == && a)
{
LL N = (LL)(a*+) * (b*+) - ;
LL K = ;
for(int x = ; x <= a; ++x)
{
int k = b / x;
K += phi(x) * k;
for(int y = k*x+; y <= b; y++)
if(gcd(x, y) == ) K++;
}
K = (K+)*;
printf("%.7f\n", (double)K / N);
} return ;
}
代码君二
UVa 10214 (莫比乌斯反演 or 欧拉函数) Trees in a Wood.的更多相关文章
- BZOJ 2818 Gcd (莫比乌斯反演 或 欧拉函数)
2818: Gcd Time Limit: 10 Sec Memory Limit: 256 MB Submit: 2534 Solved: 1129 [Submit][Status][Discu ...
- 【BZOJ2226】[Spoj 5971] LCMSum 莫比乌斯反演(欧拉函数?)
[BZOJ2226][Spoj 5971] LCMSum Description Given n, calculate the sum LCM(1,n) + LCM(2,n) + .. + LCM(n ...
- 【BZOJ2818】Gcd(莫比乌斯反演,欧拉函数)
题意:给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对 1<=N<=10^7 思路:莫比乌斯反演,同BZOJ2820…… ; ..max]of ...
- UVA 11424 GCD - Extreme (I) (欧拉函数+筛法)
题目:给出n,求gcd(1,2)+gcd(1,3)+gcd(2,3)+gcd(1,4)+gcd(2,4)+gcd(3,4)+...+gcd(1,n)+gcd(2,n)+...+gcd(n-1,n) 此 ...
- UVa 10820 (打表、欧拉函数) Send a Table
题意: 题目背景略去,将这道题很容易转化为,给出n求,n以内的有序数对(x, y)互素的对数. 分析: 问题还可以继续转化. 根据对称性,我们可以假设x<y,当x=y时,满足条件的只有(1, 1 ...
- UVA 11426 GCD - Extreme (II) (欧拉函数+筛法)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/O 题意是给你n,求所有gcd(i , j)的和,其中 ...
- UVA 11426 GCD - Extreme (II) 欧拉函数
分析:枚举每个数的贡献,欧拉函数筛法 #include <cstdio> #include <iostream> #include <ctime> #include ...
- UVA 11426 GCD - Extreme (II)(欧拉函数打表 + 规律)
Given the value of N, you will have to find the value of G. The definition of G is given below:Here ...
- LightOJ 1375 - LCM Extreme 莫比乌斯反演或欧拉扩展
题意:给出n [1,3*1e6] 求 并模2^64. 思路:先手写出算式 观察发现可以化成 那么关键在于如何求得i为1~n的lcm(i,n)之和.可以知道lcm(a,b)为ab/gcd(a,b) 变换 ...
随机推荐
- centos install(160112更新)
centos安装之后: 更新 yum update 新增用户: useradd myuser passwd myuser 添加sudo: usermod -a -G wheel myuser //vi ...
- Microsoft Visual Studio Ultimate 2012 旗舰版 有效注册密钥
Microsoft Visual Studio Ultimate 2012 旗舰版 有效注册密钥: YKCW6-BPFPF-BT8C9-7DCTH-QXGWC 已经过本人测试 本着分享的精神,希望大家 ...
- gulp前端自动化构建工具入门篇
现在我们通过这3个问题来学习一下: 1.什么是gulp? 2.为什么要用gulp? 3.怎么用? 什么是gulp 答:是一个前端自动化的构建工具,直白点说,如果没有这个工具,我们利用人工依旧可以做 ...
- Nginx配置文件变量大全
$args # 这个变量等于请求行中的参数. $binary_remote_addr # 远程地址的二进制表示 $body_bytes_sent # 已发送的消息体字节数 $content_lengt ...
- 2016 系统设计第一期 (档案一)MVC 相关控件整理
说明:前者是MVC,后者是boostrap 1.form 表单 @using (Html.BeginForm("Create", "User", FormMet ...
- struts2与velocity的整合有两种方式
1.以struts2为主.struts2内置了对velocity的支持,只要在<result name="success"?type="velocity" ...
- 1024: [SCOI2009]生日快乐 - BZOJ
Description windy的生日到了,为了庆祝生日,他的朋友们帮他买了一个边长分别为 X 和 Y 的矩形蛋糕.现在包括windy,一共有 N 个人来分这块大蛋糕,要求每个人必须获得相同面积的蛋 ...
- hdu 4662
将U全部转化为I 因为 I 的个数一定是2的n次方 有可能消除了一定数量的 2U 所以I的个数加上一个6的整数倍是2的n次方 #include <iostream> #includ ...
- Codeforces Round #240 (Div. 2)(A -- D)
点我看题目 A. Mashmokh and Lights time limit per test:1 secondmemory limit per test:256 megabytesinput:st ...
- HDU4548+素数
简单题. /* */ #include<stdio.h> #include<string.h> #include<stdlib.h> #include<alg ...