UVA10820 交表 Send a Table
2024-09-07 02:28:05
\(\Large\textbf{Description:} \large{输入n,求有多少个二元组(x,y)满足:1\leqslant x,y\leqslant n,且x和y互素。}\)
\(\Large\textbf{Solution:} \large{不难发现除了(1,1)外,其他二元组的x,y都不相等。设满足x < y的二元组有g(n)个,那么答案为2\times g(n) + 1。}\)
\(\large{其中g(n)=\sum_{i=2}^n\Phi(i)}。\)
\(\Large\textbf{Code:}\)
#include <cmath>
#include <cstdio>
#include <algorithm>
#define LL long long
#define gc() getchar()
#define rep(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std;
const int N = 5e4 + 5;
int n, cnt, prime[N], phi[N];
LL sum[N];
bool vis[N];
inline int read() {
char ch = gc();
int ans = 0;
while (ch > '9' || ch < '0') ch = gc();
while (ch >= '0' && ch <= '9') ans = (ans << 1) + (ans << 3) + ch - '0', ch = gc();
return ans;
}
inline void euler() {
phi[1] = 1;
rep(i, 2, N) {
if (!vis[i]) prime[++cnt] = i, phi[i] = i - 1;
for (int j = 1; j <= cnt && i * prime[j] <= N; ++j) {
vis[prime[j] * i] = true;
if (i % prime[j] == 0) {
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
else phi[i * prime[j]] = phi[i] * (prime[j] - 1);
}
}
}
int main() {
euler();
rep(i, 1, N) sum[i] = sum[i - 1] + phi[i];
n = read();
while (n) {
printf("%lld\n", 2 * sum[n] - 1);
n = read();
}
return 0;
}
\(\large\color{pink}{by\quad Miraclys}\)
最新文章
- 适配iOS10 的相关权限设置
- SQL SERVER 内存分配及常见内存问题 DMV查询
- C++程序设计(一)
- shell判断一个变量是否为空
- Java泛型:泛型类、泛型接口和泛型方法
- @ContextConfiguration注解说明
- Sending e-mail with Spring MVC---reference
- 【USACO 3.2.5】魔板
- hadoop1.2.1+hbase0.90.4+nutch2.2.1+elasticsearch0.90.5配置(伪分布式)
- How many integers can you find(容斥+dfs容斥)
- C++Primer学习——类
- NIO之FileChannel类的理解和使用
- Activity简说
- get() got an unexpected keyword argument
- Mysql 函数使用记录(一)——DATEDIFF、CONCAT
- HTML的实际演练1
- freemarker时间转换
- USACO 4.3 Street Race
- DBA_实践指南系列5_Oracle Erp R12日常运维和管理(案例)
- python itertools模块练习
热门文章
- 利用DFS算出有多少个连通图
- swiper选项卡还可以左右滑动,最后一个直接跳转链接
- centos将uwsgi添加为系统服务
- 吴裕雄--天生自然ORACLE数据库学习笔记:用户管理与权限分配
- C 语言入门---第十一章---C语言重要知识点补充
- java.lang.ClassCastException: android.app.Application cannot be cast to
- UCOS-III API函数
- Linux centos7 LAMP架构介绍、 MySQL、MariaDB介绍、MySQL安装
- 使用mybase、Typora搭配坚果云实现个人云笔记
- Nginx多站点虚拟主机实现单独启动停止php-fpm、单独控制权限设置