poj3090欧拉函数求和
Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
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
题目大意:给你一个n*n的网格,任意一点和(0,0)连线,可以组成一条直线,前面的点可以挡住后面的点,问你能看到的点到底有多少个
思路分析:题目实际上就是问在这个网格上有多少种不同的斜率,边上的两点我们先不管,然后将整个正方形分成上三角和下三角两部分,
由对称性,两边可以看到的点的数目肯定一样多,以下三角为例进行研究,我们会发现,对于所有能看到的点,他们有着一个共同的特征,
那就是gcd(x,y)=1,若不为1,则他前面肯定有一个点挡住了这个点,那么本题就转变成了一个求欧拉函数和的简单题目,注意不要将分界线
上的点加,记t=phi[1]+phi[2]+.......+phi[n],则ans=(t-1)*2+1+2=2*t+1
代码:
#include<iostream>
#include<cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn=;
int prime[maxn];
int phi[maxn];
bool check[maxn];
int tot;
void make_prime()
{
phi[]=;
memset(check,true,sizeof(check));
tot=;
for(int i=;i<=maxn;i++)
{
if(check[i])
{
prime[tot++]=i;
phi[i]=i-;
}
for(int j=;j<tot&&i*prime[j]<=maxn;j++)
{
check[i*prime[j]]=false;
if(i%prime[j]==)
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
else phi[i*prime[j]]=phi[i]*(prime[j]-);
}
}
}
int kase;
int main()
{
int T;
make_prime();
scanf("%d",&T);
kase=;
ll num;
while(T--)
{
int n;
scanf("%d",&n);
ll ans=;
for(int i=;i<=n;i++)
{
ans+=phi[i];
}
printf("%d %d %lld\n",++kase,n,ans*+);
}
}
poj3090欧拉函数求和的更多相关文章
- 【BZOJ4805】欧拉函数求和(杜教筛)
[BZOJ4805]欧拉函数求和(杜教筛) 题面 BZOJ 题解 好久没写过了 正好看见了顺手切一下 令\[S(n)=\sum_{i=1}^n\varphi(i)\] 设存在的某个积性函数\(g(x) ...
- BZOJ4805: 欧拉函数求和(杜教筛)
4805: 欧拉函数求和 Time Limit: 15 Sec Memory Limit: 256 MBSubmit: 614 Solved: 342[Submit][Status][Discus ...
- HDU2824-The Euler function-筛选法求欧拉函数+求和
欧拉函数: φ(n)=n*(1-1/p1)(1-1/p2)....(1-1/pk),其中p1.p2-pk为n的所有素因子.比如:φ(12)=12*(1-1/2)(1-1/3)=4.可以用类似求素数的筛 ...
- [BZOJ]4805: 欧拉函数求和
解题思路类似莫比乌斯函数之和 题目大意:求[1,n]内的欧拉函数$\varphi$之和.($n<=2*10^{9}$) 思路:令$ M(n)=\sum_{i=1}^{n}\varphi (i) ...
- 【bzoj3944/bzoj4805】Sum/欧拉函数求和 杜教筛
bzoj3944 题目描述 输入 一共T+1行 第1行为数据组数T(T<=10) 第2~T+1行每行一个非负整数N,代表一组询问 输出 一共T行,每行两个用空格分隔的数ans1,ans2 样例输 ...
- BZOJ 4805: 欧拉函数求和 杜教筛
https://www.lydsy.com/JudgeOnline/problem.php?id=4805 给出一个数字N,求sigma(phi(i)),1<=i<=N https://b ...
- 【BZOJ3944/4805】Sum/欧拉函数求和 杜教筛
[BZOJ3944]Sum Description Input 一共T+1行 第1行为数据组数T(T<=10) 第2~T+1行每行一个非负整数N,代表一组询问 Output 一共T行,每行两个用 ...
- Note -「因数的欧拉函数求和」
归档. 试证明:\(\sum \limits _{d | x} \varphi (d) = x\) Lemma 1. 试证明:\(\sum \limits _{d | p^k} \varphi (d) ...
- Bzoj4805: 欧拉函数求和
好久没写杜教筛了 练练手AC量刷起 # include <bits/stdc++.h> # define RG register # define IL inline # define F ...
随机推荐
- Html5所见即所得的几款框架
http://www.csdn.net/article/2013-10-21/2817243-8-useful-html5-frameworks
- RxJava 与 Retrofit 结合的最佳实践
转自:http://gank.io/post/56e80c2c677659311bed9841?from=timeline&isappinstalled=0&nsukey=g1D1Y6 ...
- bug经验
1.异常信息丢失导致定位问题困难. 2.findbugs工具 3.在某些if语句判断中return,可能会导致文件句柄无法关闭. 4.lastmodified()在win下和linux下的处理是不同的 ...
- Unity欢迎窗口的信息
1.Video Tutorials提供Unity相关的教程,包括用户手册.组件手册以及脚本手册等内容.2.Unity Basics提供Unity的基础知识,例如操作界面.工作流程.发布设置等内容.3. ...
- AMS1117典型电路
AMS1117(3.3V.5V) 封装: 常见应用连接: 1.输入旁路电容Input Bypass Capacitor:A 10uF tantalum on the input is a suitab ...
- 转:shell 经典, shell 十三问
原文链接:http://blog.csdn.net/freexploit/article/details/626660 我在 CU 的日子并不长,有幸在 shell 版上与大家结缘.除了跟众前辈学 ...
- logstash 通过type判断
[elk@zjtest7-frontend type]$ cat input.conf input { file { type => "type_a" path => ...
- 【转】NPAPI 插件无法在 Chrome 42 版及更高版本上正常运行
原文网址:https://support.google.com/chrome/answer/6213033 NPAPI 插件无法在 Chrome 42 版及更高版本上正常运行 您可以利用插件在浏览器中 ...
- C 编程调试集
gcc rw.c rw.c:75:6: warning: conflicting types for ‘process_conn_server’ void process_conn_server(in ...
- euctb