ACM学习历程—POJ3090 Visible Lattice Points(容斥原理 || 莫比乌斯)
Description
A lattice point (x, y) in the first quadrant (x and y are integers greater than or equal to 0), other than the origin, is visible from the origin if the line from (0, 0) to (x, y) does not pass through any other lattice point. For example, the point (4, 2) is not visible since the line from the origin passes through (2, 1). The figure below shows the points (x, y) with 0 ≤ x, y ≤ 5 with lines from the origin to the visible points.

Write a program which, given a value for the size, N, computes the number of visible points (x, y) with 0 ≤ x, y ≤ N.
Input
The first line of input contains a single integer C (1 ≤ C ≤ 1000) which is the number of datasets that follow.
Each dataset consists of a single line of input containing a single integer N (1 ≤ N ≤ 1000), which is the size.
Output
For each dataset, there is to be one line of output consisting of: the dataset number starting at 1, a single space, the size, a single space and the number of visible points for that size.
Sample Input
4
2
4
5
231
Sample Output
1 2 5
2 4 13
3 5 21
4 231 32549
题目大意就是求不同种斜率的个数。
第一反应的话枚举所有斜率,然后去掉相同斜率,而相同斜率的特征的就是,斜率的分子分母约分后和其中某个斜率一质。
于是,我只需要考虑分子分母互质的斜率即可。
于是就可以枚举斜率的分母或者分子,如果枚举斜率的分母,
比如x = 1,那么y只能取1
x = 2,那么y取[1, 2]与2互质的数,
x = 3, 那么y取[1, 3]与3互质的数
…
…
(注意x = n, y = 0和x = 0, y = 1也要加上。)
于是整个结果就是x取遍[1, n],y取遍[1, n]求互质的对数。
这和之前的一道莫比乌斯一样,不过k取1,这样就可以用容斥或者莫比乌斯解决了。
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define LL long long using namespace std; const int maxN = 1005;
int n;
int prime[maxN], u[maxN];
bool vis[maxN]; void mobius()
{
memset(vis, false,sizeof(vis));
u[1] = 1;
int cnt = 0;
for(int i = 2; i < maxN; i++)
{
if(!vis[i])
{
prime[cnt++] = i;
u[i] = -1;
}
for(int j = 0; j < cnt && i*prime[j] < maxN; j++)
{
vis[i*prime[j]] = true;
if(i%prime[j])
u[i*prime[j]] = -u[i];
else
{
u[i*prime[j]] = 0;
break;
}
}
}
} void work()
{
LL ans = 0;
for (int i = 1; i <= n; ++i)
ans += (LL)u[i]*(n/i)*(n/i);
printf("%I64d\n", ans+2);
} int main()
{
//freopen("test.in", "r", stdin);
mobius();
int T;
scanf("%d", &T);
for (int times = 1; times <= T; ++times)
{
scanf("%d", &n);
printf("%d %d ", times, n);
work();
}
return 0;
}
ACM学习历程—POJ3090 Visible Lattice Points(容斥原理 || 莫比乌斯)的更多相关文章
- POJ3090 Visible Lattice Points
/* * POJ3090 Visible Lattice Points * 欧拉函数 */ #include<cstdio> using namespace std; int C,N; / ...
- SPOJ 7001. Visible Lattice Points (莫比乌斯反演)
7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N lattice. One corner is at (0,0, ...
- [SPOJ VLATTICE]Visible Lattice Points 数论 莫比乌斯反演
7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N lattice. One corner is at (0,0, ...
- Visible Lattice Points (莫比乌斯反演)
Visible Lattice Points 题意 : 从(0,0,0)出发在(N,N,N)范围内有多少条不从重合的直线:我们只要求gcd(x,y,z) = 1; 的点有多少个就可以了: 比如 : 点 ...
- POJ3090 Visible Lattice Points 欧拉函数
欧拉函数裸题,直接欧拉函数值乘二加一就行了.具体证明略,反正很简单. 题干: Description A lattice point (x, y) in the first quadrant (x a ...
- ACM学习历程—HDU4717 The Moving Points(模拟退火 || 三分法)
Description There are N points in total. Every point moves in certain direction and certain speed. W ...
- POJ3090 Visible Lattice Points (数论:欧拉函数模板)
题目链接:传送门 思路: 所有gcd(x, y) = 1的数对都满足题意,然后还有(1, 0) 和 (0, 1). #include <iostream> #include <cst ...
- [POJ3090]Visible Lattice Points(欧拉函数)
答案为3+2*∑φ(i),(i=2 to n) Code #include <cstdio> int T,n,A[1010]; void Init(){ for(int i=2;i< ...
- ACM学习历程—HDU 5072 Coprime(容斥原理)
Description There are n people standing in a line. Each of them has a unique id number. Now the Ragn ...
随机推荐
- 自定义 ViewController 容器转场
本文转载至 http://blog.csdn.net/yongyinmg/article/details/40621463 在话题 #5 中,Chris Eidhof 向我们介绍了 iOS7 引入的新 ...
- C#通过代码彻底结束桌面进程explorer,解决自动重启问题
C# 通过代码 Process.Kill 方法杀死桌面进程后,会自动重启 其实可以通过 Taskkill 指令结束桌面进程, 在命令行查看 taskkill 帮助, TASKKILL [/S syst ...
- 九度OJ 1324:The Best Rank(最优排名) (排序)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:489 解决:126 题目描述: To evaluate the performance of our first year CS major ...
- Android笔记之WebView加载网页的进度回调
wv.setWebChromeClient(new WebChromeClient(){ @Override public void onProgressChanged(WebView view, i ...
- 自定义弹窗 VS AlertDialog分享弹窗
一.摘要 弹窗通常用于提示用户进行某种操作,比如:点击分享按钮,弹窗分享对话框:双击返回按钮,弹窗退出对话框:下载文件,提示下载对话框等等,分享对话框/退出对话框/下载对话框,都可以直接使用Alert ...
- java访问微信接口发送消息
最近在开发activiti流程的时候有个需求:流程到达每个审批节点后,需要向该节点的审批人发送一个消息,提示有审批需要处理. 参考了一下微信的开发者文档和网络上的一些技术博客,现在记录一下.以便后续继 ...
- 快照COW
What is Copy-on-write? Copy-on-write Copy-on-write (sometimes referred to as "COW") i ...
- mssql-在一个特定的会话停止出发器
用SET CONTEXT_INFO来实现 --在某个会话里设置 SET CONTEXT_INFO 0x8888 --在触发器里判断 ) SELECT @Cinfo = Context_Info() 原 ...
- Centos下MySQL数据库主从双向同步配置
MYSQL支持单向.异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器.主服务器将更新写入二进制日志文件,并维护日志文件的一个索引以跟踪日志循环.当一个从服务器连接到主服务 ...
- Linux平台下贪吃蛇游戏的运行
1.参考资料说明: 这是一个在Linux系统下实现的简单的贪吃蛇游戏,同学找帮忙,我就直接在Red Hat中调试了一下,参考的是百度文库中"maosuhan"仁兄的文章,结合自己的 ...