博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #174 (Div. 2) Cows and Primitive Roots(数论)
阅读量:7229 次
发布时间:2019-06-29

本文共 1692 字,大约阅读时间需要 5 分钟。

Cows and Primitive Roots

time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The cows have just learned what a primitive root is! Given a prime p, a primitive root is an integer x (1 ≤ x < p) such that none of integers x - 1, x2 - 1, ..., xp - 2 - 1 are divisible by p, but xp - 1 - 1 is.

Unfortunately, computing primitive roots can be time consuming, so the cows need your help. Given a prime p, help the cows find the number of primitive roots .

Input

The input contains a single line containing an integer p (2 ≤ p < 2000). It is guaranteed that p is a prime.

Output

Output on a single line the number of primitive roots .

Sample test(s)
Input
3
Output
1
Input
5
Output
2
Note

The only primitive root is 2.

The primitive roots are 2 and 3.

可使用蛮力法,但需要注意两点:

1、不要每次都用pow计算x的n次方,由于x^n=x*X^(n-1),设一个变量m储存x^(n-1),那么x^n=m*x.

2、如果直接算出m,就算用64位也会溢出,利用性质(a-b)%p=(a%p-b%p)%p,则只需保留m%p的值。

AC Code:

1 #include 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 #include
14 using namespace std;15 #define ll long long16 #define cti const int17 #define ctll const long long18 #define dg(i) cout << '*' << i << endl;19 20 int main()21 {22 ll p, x;23 ll m;24 int ans;25 bool ok;26 while(scanf("%I64d", &p) != EOF)27 {28 ans = 0;29 for(x = 1; x < p; x++)30 {31 m = 1;32 ok = true;33 for(int i = 1; i < p - 1; i++)34 {35 m *= x;36 m %= p;37 if((m - 1) % p == 0)38 {39 ok = false;40 break;41 }42 }43 if(ok && ((m * x) - 1) % p == 0) ans++;44 }45 printf("%d\n", ans);46 }47 return 0;48 }

 

转载地址:http://erbfm.baihongyu.com/

你可能感兴趣的文章
go append函数以及写入
查看>>
关于Java中分层中遇到的一些问题
查看>>
配置 PM2 实现代码自动发布
查看>>
android百种动画侧滑库、步骤视图、TextView效果、社交、搜房、K线图等源码
查看>>
iOS仿今日头条、壁纸应用、筛选分类、三方微博、颜色填充等源码
查看>>
诡异!React stopPropagation失灵
查看>>
Python_OOP
查看>>
个人博客开发系列:评论功能之GitHub账号OAuth授权
查看>>
mongodb--安装和初步使用教程
查看>>
ES6简单总结(搭配简单的讲解和小案例)
查看>>
text-decoration与color属性
查看>>
如何使用Mybatis第三方插件--PageHelper实现分页操作
查看>>
PyCharm搭建GO开发环境(GO语言学习第1课)
查看>>
Android交互
查看>>
提醒我喝水chrome插件开发指南
查看>>
列表数据转树形数据
查看>>
Java新版本的开发已正式进入轨道,版本号18.3
查看>>
从零开始的webpack生活-0x009:FilesLoader装载文件
查看>>
在electron中实现跨域请求,无需更改服务器端设置
查看>>
gitlab-ci配置详解(一)
查看>>