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 ...
随机推荐
- postgresql遇到的性能问题
问题SQL scwksmlcls.wk_cls_c , scwklrgcls.wk_lrg_cls_nm , scwkmdlcls.wk_mdl_cls_nm , scwksmlcls.wk_sml_ ...
- webSql的简单小例子
初始化websql数据库的参数信息 var config = { name: 'my_plan', version: '', desc: 'manage my plans', size: 20 * 1 ...
- Visual Studio 2017 无法连接到Web服务器"IIS Express"
.net core2.2 无法连接到Web服务器"IIS Express" 解决方案: 用命令提示符输入以下命令 sc config http start= auto 重启计算机, ...
- struts2配置文件加载顺序
struts2配置文件加载顺序: struts-default.xml/ struts-plugin.xml/ struts.xml/ struts.properties/ web.xml
- Java代码实现WORD转PDF
第一步: 安装OpenOffice 在此良心提供windows版本安装文件 链接:https://pan.baidu.com/s/17pPCkcS1C46VtLhevqSgPw 密码:vmlu ...
- 梦想Android版CAD控件2019.01.23更新
下载地址:http://www.mxdraw.com/ndetail_10121.html?tdsourcetag=s_pcqq_aiomsg1. 增加异步读取CAD,DWG文件函数,MxFuncti ...
- A useful logger function in C project.
#cat log.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include < ...
- 数组array的常用方法简介
数组方法简介 数组总共有22种方法,本文将其分为以下几类来进行详细介绍. 原数组变化:push() pop() shift() unshift() reverse() sort() splice() ...
- java 访问对象私有变量
Captcha captcha = getCaptcha(captchaId); // 通过反射获取验证码值 Class<?> classType = captcha.getClass() ...
- 【Codeforces 159B】Matchmaker
[链接] 我是链接,点我呀:) [题意] 笔和笔盖 笔有颜色和直径 笔盖也有颜色和直径 笔盖和笔如果直径相等 那么称为good 如果笔盖和笔直径相等.颜色也相等,那么称为very good 问你怎样安 ...