[luoguP2158] [SDOI2008]仪仗队(数论)
2024-08-30 22:23:54
可以看出 (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;
}
最新文章
- C和指针 第七章 习题
- Mysql 与 Python socket
- 从Wep page到Application
- 替代jquery
- ADUM1201在隔离RS232中的应用 【瓦特芯收藏】
- c语言指针字符串与字符数组字符串的区别
- 28、Jquery 页面效果
- TextView实现多个TextView对象的走马灯效果
- Zookeeper Watcher 解析
- cors解决ajax请求跨域问题
- C语言第二次博客作业---分支结构 陈张鑫
- python全栈开发day59-Django基础
- spring+springMvc+struts的SSH框架整合
- numpy 介绍
- IOS返回go(-1)
- 介绍一个axios调试好用的工具:axios-mock-adapter
- JS 01 变量_数据类型_分支循环_数组
- Android通知栏沉浸式/透明化完整解决方案
- Unity如何内置Visual Studio
- 关于hibernate中的session与数据库连接关系以及getCurrentSession 与 openSession() 的区别