[luoguP2158] [SDOI2008]仪仗队(数论)
可以看出 (i, j) 能被看到,(i * k, j * k) 都会被挡住
暴力
所以 gcd(i, j) == 1 的话 ans ++
那么可以枚举一半(中轴对称),求解答案,只能拿30分
#include <cstdio>
#include <iostream> int n, ans; inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
} inline int gcd(int x, int y)
{
return !y ? x : gcd(y, x % y);
} int main()
{
int i, j;
n = read();
if(n == 1)
{
puts("0");
return 0;
}
for(i = 1; i < n; i++)
for(j = i + 1; j < n; j++)
if(gcd(i, j) == 1)
ans++;
printf("%d\n", ans * 2 + 3);
return 0;
}
正解
可以看出,gcd(i,j) == 1 才能对答案有贡献,也就是互质,想到什么?phi 值
其实上面的暴力过程仔细来看也就是 phi 值 的求解
#include <cstdio>
#include <iostream> int n, ans;
int phi[500001]; inline int read()
{
int x = 0, f = 1;
char ch = getchar();
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = -1;
for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
return x * f;
} inline void euler_phi()
{
int i, j;
phi[1] = 1;
for(i = 2; i < n; i++)
if(!phi[i])
for(j = i; j < n; j += i)
{
if(!phi[j]) phi[j] = j;
phi[j] = phi[j] / i * (i - 1);
}
} int main()
{
int i, j;
n = read();
if(n == 1)
{
puts("0");
return 0;
}
euler_phi();
for(i = 1; i < n; i++) ans += phi[i];
printf("%d\n", ans * 2 + 1);
return 0;
}
[luoguP2158] [SDOI2008]仪仗队(数论)的更多相关文章
- [LuoguP2158][SDOI2008]仪仗队
[LuoguP2158][SDOI2008]仪仗队(Link) 现在你有一个\(N \times N\)的矩阵,求你站在\((1,1)\)点能看到的点的总数. 很简洁的题面. 这道题看起来很难,但是稍 ...
- 【bzoj2190】: [SDOI2008]仪仗队 数论-欧拉函数
[bzoj2190]: [SDOI2008]仪仗队 在第i行当且仅当gcd(i,j)=1 可以被看到 欧拉函数求和 没了 /* http://www.cnblogs.com/karl07/ */ #i ...
- 【bzoj2190】[SDOI2008]仪仗队 数论 欧拉函数 筛法
http://www.lydsy.com/JudgeOnline/problem.php?id=2190 裸欧拉函数,先不计算对角线(a,a)的一列,然后算出1到n-1的所有欧拉函数相加*2,再加 ...
- BZOJ-2190 仪仗队 数论+欧拉函数(线性筛)
今天zky学长讲数论,上午水,舒爽的不行..后来下午直接while(true){懵逼:}死循全程懵逼....(可怕)Thinking Bear. 2190: [SDOI2008]仪仗队 Time Li ...
- BZOJ 2190: [SDOI2008]仪仗队( 欧拉函数 )
假设C君为(0, 0), 则右上方为(n - 1, n - 1). 一个点(x, y) 能被看到的前提是gcd(x, y) = 1, 所以 answer = ∑ phi(i) * 2 + 2 - 1 ...
- BZOJ 2190: [SDOI2008]仪仗队
2190: [SDOI2008]仪仗队 Time Limit: 10 Sec Memory Limit: 259 MBSubmit: 2689 Solved: 1713[Submit][Statu ...
- [SDOI2008]仪仗队
P2158 [SDOI2008]仪仗队 题目描述 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线 ...
- P2158 [SDOI2008]仪仗队
P2158 [SDOI2008]仪仗队图是关于y=x对称的,横纵坐标一定是互质的否则在之前就被扫过了,所以就可以用欧拉函数再*2就完了. #include<iostream> #inclu ...
- 洛谷 P2158 [SDOI2008]仪仗队 解题报告
P2158 [SDOI2008]仪仗队 题目描述 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视线 ...
随机推荐
- bzoj 1087: [SCOI2005]互不侵犯King【状压dp】
显然是状压,设f[i][j][k]为1到i行选j个king,并且第i行状态为k的方案数,判断是否可行然后枚举转移即可 先把可行状态预处理出来会变快 #include<iostream> # ...
- centos docker 安装mysql 8.0
centos 版本 CentOS Linux release 7.5.1804 (Core) 内核版本: 3.10.0-862.el7.x86_64 下载最新版mysql docker pull m ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 Minimum Distance in a Star Graph
In this problem, we will define a graph called star graph, and the question is to find the minimum d ...
- Linux系统下强制踢掉登录用户
1,利用who命令,找出用户登录的终端代号 who root pts/0 2017-03-14 22:30 (223.1.1.1) root pts/1 2 ...
- 选择语言之switch case
程序语言-选择语言之switch case 多选一,类似if else if else if else 模版: Switch(选择条件) { Case(条件一)//相当于if Conso ...
- 多路开关模式的switch语句
在实例10中,将break语句去掉之后,会将符合检验条件后的所有语句都输出.利用这个特点,可以设计多路开关模式的switch语句,例如:在平年一年12个月,1.3.5.7.8.10.12月是31天,4 ...
- HDU_3732_(多重背包)
Ahui Writes Word Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 扩增子分析QIIME2-3数据导出Exporting data
# 激活工作环境 source activate qiime2-2017.8 # 建立工作目录 mkdir -p qiime2-exporting-tutorial cd qiime2-exporti ...
- netstat查看服务器连接数端口并发数
简介 Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Member ...
- 【转】上传并解析excel
[转载自]http://blog.csdn.net/u011563331/article/details/51322523 通过解析excel,将数据存储到数据库中.现在将方法保存下来. 使用的是ap ...