poj 3090 && poj 2478(法雷级数,欧拉函数)
http://poj.org/problem?id=3090
法雷级数的递推公式非常easy:f[1] = 2; f[i] = f[i-1]+phi[i]。
该题是法雷级数的变形吧,答案是2*f[i]-1。
#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
#define LL long long
#define _LL __int64
#define eps 1e-12
#define PI acos(-1.0)
using namespace std; const int maxn = 1100; int flag[maxn];
int prime[maxn];
int phi[maxn];
LL f[maxn]; void init()
{
memset(flag,0,sizeof(flag));
prime[0] = 0;
phi[1] = 1;
for(int i = 2; i < maxn; i++)
{
if(flag[i] == 0)
{
phi[i] = i-1;
prime[++prime[0]] = i;
}
for(int j = 1; j <= prime[0]&&prime[j]*i<maxn; j++)
{
flag[prime[j]*i] = 1;
if(i % prime[j] == 0)
phi[prime[j]*i] = phi[i] * prime[j];
else
phi[prime[j]*i] = phi[i] * (prime[j] - 1);
}
}
f[1] = 2;
for(int i = 2; i <= 1000; i++)
f[i] = f[i-1] + phi[i];
} int main()
{
init();
int test;
scanf("%d",&test);
for(int item = 1; item <= test; item++)
{
int x;
scanf("%d",&x);
printf("%d %d %lld\n",item,x,f[x]*2-1);
}
return 0;
}
id=2478">http://poj.org/problem? id=2478
更简单了,直接求法雷级数。基于素数筛的欧拉函数。
#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
#define LL long long
#define _LL __int64
#define eps 1e-12
#define PI acos(-1.0)
using namespace std; const int maxn = 1000010; int flag[maxn];
int prime[maxn];
int phi[maxn];
LL f[maxn]; void init()
{
memset(flag,0,sizeof(flag));
prime[0] = 0;
phi[1] = 1;
for(int i = 2; i < maxn; i++)
{
if(flag[i] == 0)
{
phi[i] = i-1;
prime[++prime[0]] = i;
}
for(int j = 1; j <= prime[0]&&prime[j]*i<maxn; j++)
{
flag[prime[j]*i] = 1;
if(i % prime[j] == 0)
phi[prime[j]*i] = phi[i] * prime[j];
else
phi[prime[j]*i] = phi[i] * (prime[j] - 1);
}
}
f[1] = 2;
for(int i = 2; i <= 1000010; i++)
f[i] = f[i-1] + phi[i];
} int main()
{
init();
int n;
while(~scanf("%d",&n)&&n)
{
printf("%lld\n",f[n]-2);
} return 0;
}
poj 3090 && poj 2478(法雷级数,欧拉函数)的更多相关文章
- POJ 3090 Visible Lattice Points | 其实是欧拉函数
题目: 给一个n,n的网格,点可以遮挡视线,问从0,0看能看到多少点 题解: 根据对称性,我们可以把网格按y=x为对称轴划分成两半,求一半的就可以了,可以想到的是应该每种斜率只能看到一个点 因为斜率表 ...
- 【POJ 2480】Longge's problem(欧拉函数)
题意 求$ \sum_{i=1}^n gcd(i,n) $ 给定 $n(1\le n\le 2^{32}) $. 链接 题解 欧拉函数 $φ(x)$ :1到x-1有几个和x互质的数. gcd(i,n) ...
- 【POJ】2480 Longge's problem(欧拉函数)
题目 传送门:QWQ 分析 题意就是求∑gcd(i, N) 1<=i <=N.. 显然$ gcd(i,n) = x $时,必然$x|n$. 所以我们枚举一下n的约数,对于每个约数x,显然$ ...
- POJ2478 - Farey Sequence(法雷级数&&欧拉函数)
题目大意 直接看原文吧.... The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rat ...
- [ACM] POJ 2154 Color (Polya计数优化,欧拉函数)
Color Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7630 Accepted: 2507 Description ...
- 【poj 1284】Primitive Roots(数论--欧拉函数 求原根个数){费马小定理、欧拉定理}
题意:求奇质数 P 的原根个数.若 x 是 P 的原根,那么 x^k (k=1~p-1) 模 P 为1~p-1,且互不相同. (3≤ P<65536) 解法:有费马小定理:若 p 是质数,x^( ...
- poj 2478 Farey Sequence(欧拉函数是基于寻求筛法素数)
http://poj.org/problem?id=2478 求欧拉函数的模板. 初涉欧拉函数,先学一学它主要的性质. 1.欧拉函数是求小于n且和n互质(包含1)的正整数的个数. 记为φ(n). 2. ...
- 欧拉函数 &【POJ 2478】欧拉筛法
通式: $\phi(x)=x(1-\frac{1}{p_1})(1-\frac{1}{p_2})(1-\frac{1}{p_3}) \cdots (1-\frac{1}{p_n})$ 若n是质数p的k ...
- POJ 2478 Farey Sequence(欧拉函数前n项和)
A - Farey Sequence Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
随机推荐
- get、post、put、delete、head请求方式
对资源的增,删,改,查操作,其实都可以通过GET/POST完成,不一定要用PUT和DELETE. 一:Jersey框架,实现了restful风格,常用的注解@GET.@POST.@PUT.@DELET ...
- Android APK瘦身之webp图片
webp格式是谷歌推出的一种有损压缩格式,这种图片格式相比png或者jpg格式的图片损失的质量几乎可以忽略不计,但是压缩后图片的体积却比png或者jpg要小很多.亲测一个100kb的png图片经过we ...
- 初始MongoDB------将MongoDB创建为Windows服务
上一遍我写的是关于基本的MongoDB的安装,可能不是很详细,也写得很不好,不过这次我们会详细的说说,如果将MongoDB部署在你的Windows电脑上. 1.配置环境变量 如果每次都要在CMD进入M ...
- Angular——内置过滤器
基本介绍 在AngularJS中使用过滤器格式化展示数据,在“{{}}”中使用“|”来调用过滤器,使用“:”传递参数. 基本使用 过滤器可以串起来使用,只要用 | 来衔接就行了,可以将上次返回的 ...
- 带有空格或tab的字符串的判断
class test { public static void main(String[] args) { String a = " "; //带有空格的字符串 if ( a.is ...
- Thread stack overrun
ERROR 1436 (HY000): Thread stack overrun: 6448 bytes used of a 131072 byte stac k, and 128000 bytes ...
- Anniversary Cake
Anniversary Cake Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15704 Accepted: 5123 ...
- CAD从二制流数据中加载图形(com接口Delphi语言)
主要用到函数说明: _DMxDrawX::ReadBinStream 从二制流数据中加载图形,详细说明如下: 参数 说明 VARIANT varBinArray 二制流数据,是个byte数组 BSTR ...
- BZOJ5314: [Jsoi2018]潜入行动 (树形DP)
题意:一棵树选择恰好k个结点放置监听器 每个监听器只能监听相邻的节点 问能使得所有节点被监听的种类数 题解:反正就是很well-known的树形DP了 至于时间复杂度为什么是nk 不会不学 很好想到四 ...
- ubuntu解压zip文件出现乱码情况解决方法
使用 unzip datastructure.zip 出现下面的情况: extracting: └╧╗╞/╗·╞ў╤з╧░╝п╜ї/╩¤╛▌╜с╣╣╙ы╦у╖и/╩¤╛▌╜с╣╣╙ы╦у╖иги2гй ...