Codeforces Round #716 (Div. 2) C. Product 1 Modulo N
Posted 小哈里
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #716 (Div. 2) C. Product 1 Modulo N相关的知识,希望对你有一定的参考价值。
problem
C. Product 1 Modulo N
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Now you get Baby Ehab’s first words: “Given an integer n, find the longest subsequence of [1,2,…,n−1] whose product is 1 modulo n.” Please solve the problem.
A sequence b is a subsequence of an array a if b can be obtained from a by deleting some (possibly all) elements. The product of an empty subsequence is equal to 1.
Input
The only line contains the integer n (2≤n≤105).
Output
The first line should contain a single integer, the length of the longest subsequence.
The second line should contain the elements of the subsequence, in increasing order.
If there are multiple solutions, you can print any.
Examples
inputCopy
5
outputCopy
3
1 2 3
inputCopy
8
outputCopy
4
1 3 5 7
Note
In the first example, the product of the elements is 6 which is congruent to 1 modulo 5. The only longer subsequence is [1,2,3,4]. Its product is 24 which is congruent to 4 modulo 5. Hence, the answer is [1,2,3].
C.产品1模数N
每次测试的时限1秒
每个测试的内存限制256 MB
输入标准输入
输出标准输出
现在,您得到Baby Ehab的第一句话:“给定整数n,找到乘积为1模n的最长[1,2,…,n-1]子序列。”请解决问题。
如果可以通过删除某些(可能是全部)元素从a获得b,则序列b是数组a的子序列。空子序列的乘积等于1。
输入
仅一行包含整数n(2≤n≤105)。
输出
第一行应包含一个整数,即最长子序列的长度。
第二行应按升序包含子序列的元素。
如果有多种解决方案,则可以打印任何解决方案。
例子
inputCopy
5
outputCopy
3
1 2 3
inputCopy
8
outputCopy
4
1 3 5 7
笔记
在第一个示例中,元素的乘积是6,等于1模5。唯一长的子序列是[1,2,3,4]。它的乘积是24,等于4模5。因此,答案是[1,2,3]。
solution
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int mod = 1e9+7;
int gcd(int a, int b){
return !b?a:gcd(b,a%b);
}
int main(){
ios::sync_with_stdio(false);
int n; cin>>n;
vector<int>vc;
LL ans=1;
for(int i = 1; i <= n-1; i++){
if(gcd(i,n)==1){
vc.push_back(i);
ans *= i;
ans %= n;
}
}
while(vc.size()>1 && ans%n!=1){
ans /= vc.back();
vc.pop_back();
}
cout<<vc.size()<<"\\n";
for(int i = 0; i < vc.size(); i++)
cout<<vc[i]<<" ";
return 0;
}
以上是关于Codeforces Round #716 (Div. 2) C. Product 1 Modulo N的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #716 (Div. 2) C. Product 1 Modulo N
Codeforces Round #716 (Div. 2), B. AND 0, Sum Big, 快速幂结论题
Codeforces Round #716 (Div. 2) C. Product 1 Modulo N
7.26-Codeforces Round #372 (Div. 2)