Codeforces Round #174 (Div. 2) Cows and Primitive Roots(数论)
2013-03-22 13:46
786 查看
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
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/07/86d0eb8bb7fc84f247c1a481ad51d69b.png)
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
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/07/86d0eb8bb7fc84f247c1a481ad51d69b.png)
.
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
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/07/86d0eb8bb7fc84f247c1a481ad51d69b.png)
.
Sample test(s)
Input
Output
Input
Output
Note
The only primitive root
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/07/628c13ffc441f8ffe962075002b6ccb7.png)
is 2.
The primitive roots
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/07/693579f4ec66ea96811058a155d2b5df.png)
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:
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
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/07/86d0eb8bb7fc84f247c1a481ad51d69b.png)
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
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/07/86d0eb8bb7fc84f247c1a481ad51d69b.png)
.
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
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/07/86d0eb8bb7fc84f247c1a481ad51d69b.png)
.
Sample test(s)
Input
3
Output
1
Input
5
Output
2
Note
The only primitive root
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/07/628c13ffc441f8ffe962075002b6ccb7.png)
is 2.
The primitive roots
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/07/693579f4ec66ea96811058a155d2b5df.png)
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:
#include <iostream> #include <fstream> #include <string> #include <set> #include <map> #include <vector> #include <stack> #include <queue> #include <cmath> #include <cstdio> #include <cstring> #include <algorithm> #include <utility> using namespace std; #define ll long long #define cti const int #define ctll const long long #define dg(i) cout << '*' << i << endl; int main() { ll p, x; ll m; int ans; bool ok; while(scanf("%I64d", &p) != EOF) { ans = 0; for(x = 1; x < p; x++) { m = 1; ok = true; for(int i = 1; i < p - 1; i++) { m *= x; m %= p; if((m - 1) % p == 0) { ok = false; break; } } if(ok && ((m * x) - 1) % p == 0) ans++; } printf("%d\n", ans); } return 0; }
相关文章推荐
- Codeforces Round #174 (Div. 2)---C. Cows and Sequence(操作序列)
- Codeforces Round #174 (Div. 2) B. Cows and Poker Game(简单)
- Codeforces Round #174 (Div. 2) Cows and Sequence(线段树)
- Codeforces Round #304 (Div. 2) D. Soldier and Number Game 数论 筛表法
- Codeforces Round #324 (Div. 2) D. Dima and Lisa 数论 三素数定理
- Codeforces Round #430 (Div. 2) B. Gleb And Pizza(数论)
- Codeforces Round #312 (Div. 2)——C暴力技巧——Amr and Chemistry
- Codeforces Round #201 (Div. 2) C 数论
- Codeforces Round #294 (Div. 2)D - A and B and Interesting Substrings 字符串
- Codeforces Round #330 (Div. 2)C. Warrior and Archer
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(想法题,构造题)
- Codeforces Round #397(Div. 1 + Div. 2 combined)D. Artsem and Saunders【思维+乱搞】
- Codeforces Round #212 (Div. 2) D. Fools and Foolproof Roads
- Codeforces Round #253 (Div. 1) A Borya and Hanabi
- Codeforces Round #324 (Div. 2) D. Dima and Lisa(哥德巴赫猜想)
- Codeforces Round #396 (Div. 2) C. Mahmoud and a Message
- 【Codeforces Round #430 (Div. 2) D】 D. Vitya and Strange Lesson ("带lazy" 的字典树)
- Codeforces Round #186 (Div. 2) D. Ilya and Roads(区间类动态规划)
- Codeforces Round #261 (Div. 2)——Pashmak and Buses
- Codeforces Round #260 (Div. 2) B. Fedya and Maths