3 2 3 6
0 1 4
无限个2看上去没有头绪,但是我们可以一步一步看,首先是我们假设这一团为k,那么就是求k%p的值,我们把k中的指数利用降幂公式得出:2^(k1%phi[p]+phi[p])%p,然后对于k1%phi[p]递归求解就可以了。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
const int MAXN = 1e7+7;
LL p;
LL quick_mod(LL a,LL b,LL mod)
{
LL sum = 1,base = a;
while(b)
{
if(b&1)sum = sum*base%mod;
base = base*base%mod;
b >>= 1;
}
return sum;
}
//筛法求phi
int phi[MAXN];
void get_phi()
{
for(int i = 1; i < MAXN; ++i)phi[i] = i;
for(int i = 2; i < MAXN; ++i)
{
if(phi[i] == i)
{
for(int j = i; j < MAXN; j+=i)phi[j] = phi[j]/i*(i-1);
}
}
}
LL get_ans(LL x)
{
if(x == 1)return 0;
return quick_mod(2,get_ans(phi[x])+phi[x],x);
}
int main()
{
get_phi();
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lld",&p);
printf("%lld\n",get_ans(p));
}
return 0;
}
线性筛太快了有木有!!!
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long LL;
const int MAXN = 1e7+7;
LL p;
LL quick_mod(LL a,LL b,LL mod)
{
LL sum = 1,base = a;
while(b)
{
if(b&1)sum = sum*base%mod;
base = base*base%mod;
b >>= 1;
}
return sum;
}
bool vis[MAXN];
int prime[MAXN];
int phi[MAXN];
int tot;
void init()
{
memset(vis, false, sizeof(vis));
phi[1] = 1;
tot = 0;
for(int i = 2; i < MAXN; i ++)
{
if(!vis[i])
{
prime[tot ++] = i;
phi[i] = i - 1;
}
for(int j = 0; j < tot; j ++)
{
if(i * prime[j] >= MAXN) break;
vis[i * prime[j]] = true;
if(i % prime[j] == 0)
{
phi[i * prime[j]] = phi[i] * prime[j];
break;
}
else
{
phi[i * prime[j]] = phi[i] * (prime[j] - 1);
}
}
}
}
LL get_ans(LL x)
{
if(x == 1)return 0;
return quick_mod(2,get_ans(phi[x])+phi[x],x);
}
int main()
{
init();
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lld",&p);
printf("%lld\n",get_ans(p));
}
return 0;
}
956


&spm=1001.2101.3001.11974&articleId=76691511&d=1&t=3&u=71ac4d99e138468196f6d8bdf68ca6e1)

被折叠的 条评论
为什么被折叠?



