Problem about GCD
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 476 Accepted Submission(s): 81
Problem Description
Given integer m. Find multiplication of all 1<=a<=m such gcd(a, m)=1 (coprime with m) modulo m.
Input
Input contains multiple tests, one test per line.
Last line contains -1, it should be skipped.
[Technical Specification]
m <= 10^18
Last line contains -1, it should be skipped.
[Technical Specification]
m <= 10^18
Output
For each test please output result. One case per line. Less than 160 test cases.
Sample Input
1 2 3 4 5 -1
Sample Output
0 1 2 3 4
Source
转题解:
结论:对于 1,2,4,P^n, 2*P^n,答案为 N-1,其余情况都是1。也就是说,1,2,4,以及只有一个质因子(奇数)或者它的1/2只有一个
质因子(偶数),答案是N-1.其余情况答案均是 1。
题意:给定一个数字,求出1 - n之间与他互质的数的乘积mod n
然后由于这题的n很大,也没法直接判定,可以这样搞,先去试10^6以内的素数,判断可不可以,如果不行,再利用米勒拉宾判下是否是素数,如果不是的话,把这个数字开根在平方,判断是不是完全平方数,这样做的原因是数字最大10^18,如果没有10^6以内的质因子,又不是质数的话,那么他最多只能包含2个质因子了,那么如果他不是一个完全平方数的话,那么就肯定不是了
#define DeBUG
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <stack>
#include <queue>
#include <string>
#include <set>
#include <sstream>
#include <map>
#include <list>
#include <bitset>
using namespace std ;
#define zero {0}
#define INF 0x3f3f3f3f
#define EPS 1e-6
#define TRUE true
#define FALSE false
typedef long long LL;
const double PI = acos(-1.0);
//#pragma comment(linker, "/STACK:102400000,102400000")
inline int sgn(double x)
{
return fabs(x) < EPS ? 0 : (x < 0 ? -1 : 1);
}
#define N 1000005
long long prime[N];
int PRnum = 0;
bool vis[N];
long long n;
void getPR()
{
memset(vis, 1, sizeof(vis));
int logn = 1005;
vis[0] = vis[1] = 0;
for (int i = 2; i <= logn; i++)
{
if (vis[i])
{
for (int j = 2 * i; j <= N; j += i)
vis[j] = 0;
}
}
for (int i = 2; i <= N; i++)
if (vis[i])
prime[PRnum++] = i;
}
LL mulmod(LL a, LL n, LL m) // a*n mod m
{
LL y = 0;
a %= m;
while (n)
{
if (n & 1) y += a;
if (y >= m) y -= m;
n >>= 1;
a <<= 1;
if (a >= m) a -= m;
}
return y;
}
LL powmod(LL a, LL n, LL m) // a^n mod m
{
LL y = 1;
while (n)
{
if (n & 1) y = y * a % m; //m大于int范围则 y=mulmod(y,a,m);
if (n >>= 1)a = a * a % m; //m大于int范围则 a=mulmod(a,a,m);
// if (n & 1) y = y * a % m; //int范围加速
// if (n >>= 1)a = a * a % m;
}
return y;
}
bool MR(LL n, LL a)
{
if (n == 2) return true;
LL d = n - 1, t;
if (d & 1) return false;
while (~d & 1) d >>= 1;
t = powmod(a, d, n);
while (d != n - 1 && t != 1 && t != n - 1)
{
d <<= 1;
t = mulmod(t, t, n);
}
return (t == n - 1) || (d & 1);
}
bool isp(LL n) // 1E16 (2^54) 范围
{
if (n <= (1LL << 54))
{
if (n == 1 || n == 46856248255981LL) return false; //唯一强伪素数
if (n == 2 || n == 3 || n == 7 || n == 61 || n == 24251) return true;
return MR(n, 2) && MR(n, 3) && MR(n, 7) && MR(n, 61) && MR(n, 24251);
}
else
{
if (n == 2 || n == 3 || n == 5 || n == 7 || n == 11 || n == 13
|| n == 17 || n == 19 || n == 23 ) return true;
return MR(n, 2) && MR(n, 3) && MR(n, 5) && MR(n, 7) && MR(n, 11) && MR(n, 13) && MR(n, 17) && MR(n, 19) && MR(n, 23) ;
}
}
///2
LL mul(LL a, LL b, LL mod)
{
LL ret = 0;
while (b > 0)
{
if (b & 1) ret = (ret + a) % mod;
b >>= 1;
a = (a << 1) % mod;
}
return ret;
}
LL pow_mod(LL x, LL k, LL mod)
{
LL ans = 1;
while (k)
{
if (k & 1) ans = mul(ans, x, mod);
x = mul(x, x, mod);
k >>= 1;
}
return ans;
}
bool mlrb (LL n)
{
if (n < 2) return false;
if (n == 2) return true;
if (n % 2 == 0) return false;
for (int i = 0; i < 10; i++)
{
LL a = rand() % (n - 1) + 1;
if (pow_mod(a, n - 1, n) != 1)
return false;
}
return true;
}
//
bool check(long long n)
{
for (int i = 0; i < PRnum; i++)
{
if (n % prime[i] == 0)
{
long long temp = n;
while (temp % prime[i] == 0)
{
temp /= prime[i];
}
if (temp == 1)
return true;
else
return false;
}
}
if (mlrb(n))return true;
long long m = (long long)sqrt(n * 1.0);
if (m * m == n)return true;
return false;
}
bool judge(long long n)
{
if (n == 1 || n == 2 || n == 4)return true;
if (n % 4 == 0)return false;
if (n % 2 && check(n))return true;
if (n % 2 == 0 && check(n / 2))return true;
return false;
}
int main()
{
#ifdef DeBUGs
freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
getPR();
// cout<<(1LL<<54)<<endl;
while (scanf("%I64d", &n), n != -1)
{
if (judge(n))
printf("%I64d\n", n - 1);
else
printf("1\n");
}
return 0;
}