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 ...
随机推荐
- poj3071
题目大意,1<<n个球队比赛赛程是这样的 1 1 1 1 1 1 1 另dp[i][k]为k队进入第i场的概率 #include<iostream> #includ ...
- C++中多重继承构造函数执行顺序
代码1: #include <cstdio> #include <iostream> using namespace std; class A{ public: A(){ co ...
- XP 安装
提供一下裝系統的詳細步驟,盡量詳細到每一步都有,希望能對樓主有所幫助,不盡之處還請樓主不吝指出!謝謝 装XP的步骤如下: 开机时,按del键, 进入bios界面,一般选左侧第二项,(Advanced ...
- pendingIntent初步_什么是pendingIntent
pendingIntent字面意义:等待的,未决定的Intent. 要得到一个pendingIntent对象,使用方法类的静态方法 通过getActivity(Context context, int ...
- JSONP的客户端的具体实现
JSONP的客户端的具体实现 1.远程调用一个js,代码如下: <script type="text/javascript"> var ...
- mysql windows 下导入大文件
先进入你的mysql bin目录 cd D:/php/mysql/bin 输入命令 mysql -u 用户名 -p 密码 数据库名 < 文件路径 ...
- python学习第十天 -- 函数
稍微学过其他编程语言的人都应该了解函数的概念.在这里就不做过多的介绍. Python内置了很多有用的函数,我们可以也直接调用. 可以直接从Python的官方网站查看文档: http://docs.py ...
- vsftpd允许root用户登录
Linux下安装vsftpd之后,默认的配置是 匿名用户可以登录,匿名帐户有两个: 用户名:anonymous 密码:空 用户名:ftp 密码:ftp 如果要用匿名进行上传删除等操作需要配置其它参数. ...
- poj2425--A Chess Game
题意:给定一棵有向图的树,有些节点上有石子,每次可以取一个石子向一个有向边移动,不能移动者负. Ans:树上nim,叶子节点nim为0,父亲节点递归儿子得到sg值,答案就是每个石子所在点的sg值异或和 ...
- poj2068--Nim
题意:给你2n个人,两方各n个人,交叉坐,每个人可以取的石子有一个最大限制,总共有S颗石子,哪一方取了最后一颗石子就输了,问先取石子的这一方是否有必胜策略. DP,dp[i][j]代表第i个人还有J个 ...