【poj 2478】Farey Sequence(数论--欧拉函数 找规律求前缀和)
题意:定义 Fn 序列表示一串 <1 的分数,分数为最简分数,且分母 ≤n 。问该序列的个数。(2≤N≤10^6)
解法:先暴力找规律(代码见屏蔽处),发现 Fn 序列的个数就是 Φ(1)~Φ(n) 的和。于是用欧拉筛预处理就好了。
注意——求前缀和要用 long long 的类型。
1 #include<cstdio>
2 #include<cstdlib>
3 #include<cstring>
4 #include<iostream>
5 using namespace std;
6 #define N (int)1e6+10
7 typedef long long LL;
8
9 int pr=0;
10 int v[N],phi[N],prim[N];
11 LL sphi[N];
12
13 int gcd(int a,int b) {return b?gcd(b,a%b):a;}
14 void get_prime()
15 {
16 memset(v,0,sizeof(v));
17 for (int i=2;i<=N-10;i++)
18 {
19 if (!v[i]) prim[++pr]=i, phi[i]=i-1;
20 for (int j=1;j<=pr && i*prim[j]<=N-10;j++)
21 {
22 v[i*prim[j]]=1;
23 if (i%prim[j]==0)
24 {
25 phi[i*prim[j]]=phi[i]*prim[j];
26 break;
27 }
28 else phi[i*prim[j]]=phi[i]*phi[prim[j]];
29 }
30 }
31 sphi[1]=0;
32 for (int i=2;i<=N-10;i++)
33 sphi[i]=sphi[i-1]+phi[i];
34 }
35 int main()
36 {
37 get_prime();
38 int n;
39 while (1)
40 {
41 scanf("%d",&n);
42 if (!n) break;
43 /*int cnt=0,h=0;
44 for (int i=1;i<n;i++)
45 {
46 for (int j=i+1;j<=n;j++)
47 if (gcd(i,j)==1) cnt++;
48 h+=phi[i+1];
49 }
50 printf("%d %d\n",cnt,h);*/
51 printf("%lld\n",sphi[n]);
52 }
53 return 0;
54 }
【poj 2478】Farey Sequence(数论--欧拉函数 找规律求前缀和)的更多相关文章
- 【poj 3090】Visible Lattice Points(数论--欧拉函数 找规律求前缀和)
题意:问从(0,0)到(x,y)(0≤x, y≤N)的线段没有与其他整数点相交的点数. 解法:只有 gcd(x,y)=1 时才满足条件,问 N 以前所有的合法点的和,就发现和上一题-- [poj 24 ...
- POJ 2478 Farey Sequence(欧拉函数前n项和)
A - Farey Sequence Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u ...
- Farey Sequence (欧拉函数+前缀和)
题目链接:http://poj.org/problem?id=2478 Description The Farey Sequence Fn for any integer n with n >= ...
- Farey Sequence(欧拉函数板子题)
题目链接:http://poj.org/problem?id=2478 Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total S ...
- POJ2478 - Farey Sequence(法雷级数&&欧拉函数)
题目大意 直接看原文吧.... The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rat ...
- poj2478——Farey Sequence(欧拉函数)
Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18507 Accepted: 7429 D ...
- POJ_2478 Farey Sequence 【欧拉函数+简单递推】
一.题目 The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbe ...
- poj-2478 Farey Sequence(dp,欧拉函数)
题目链接: Farey Sequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14230 Accepted: ...
- poj 3090 && poj 2478(法雷级数,欧拉函数)
http://poj.org/problem?id=3090 法雷级数 法雷级数的递推公式非常easy:f[1] = 2; f[i] = f[i-1]+phi[i]. 该题是法雷级数的变形吧,答案是2 ...
随机推荐
- STP、PVST、MST协议
• STP:生成树协议 ○ 阻止环形链路的广播风暴 • PVST:VLAN生成树 ○ 是STP的进阶版不仅能阻止广播风暴,还可以做到基于VLAN进行流量均衡. ...
- Java 并发编程要点
使用线程 有三种使用线程的方法: 实现 Runnable 接口: 实现 Callable 接口: 继承 Thread 类. 实现 Runnable 和 Callable 接口的类只能当做一个可以在线程 ...
- Linux调整lvm逻辑分区大小
转载自:https://www.cnblogs.com/kevingrace/p/5825963.html 个人记录一下 Linux下对lvm逻辑卷分区大小的调整(针对xfs和ext4不同文件系 ...
- 手把手教你搭建一个跟vue官方同款文档(vuepress)
前言 VuePress 由两部分组成:第一部分是一个极简静态网站生成器 (opens new window),它包含由 Vue 驱动的主题系统和插件 API,另一个部分是为书写技术文档而优化的默认主题 ...
- 【Linux】快速创建文件的命令方法
[root@centos7 dir1]# ll total 0 -rw-r--r-- 1 root root 0 Aug 15 02:39 file1 -rw-r--r-- 1 root root 0 ...
- SGA: allocation forcing component growth分析
1.问题现象 20年12月31日,数据库应用人员反映2020-12-31 12:40:10存在告警,过了几分钟之后业务恢复正常. 表现的状态:Connect to database time out, ...
- 翻译 - ASP.NET Core 基本知识 - 中间件(Middleware)
翻译自 https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-5.0 中间件是集成 ...
- Atlas 2.1.0 实践(3)—— Atlas集成HIve
Atlas集成Hive 在安装好Atlas以后,如果想要使用起来,还要让Atlas与其他组件建立联系. 其中最常用的就是Hive. 通过Atlas的架构,只要配置好Hive Hook ,那么每次Hiv ...
- linux总线
编写驱动程序: 1 #include <linux/init.h> 2 #include <linux/module.h> 3 #include <linux/devic ...
- JavaScript中的异步函数
JavaScript中的异步函数 ES8 的 async/await 旨在解决利用异步结构组织代码的问题.为此, ECMAScript 对函数进行了扩展,为其增加了两个新关键字: async 和 aw ...