FZU 2018 计数 快速幂取模

来源:互联网 发布:淘宝ted baker 编辑:程序博客网 时间:2024/04/29 23:06

题意:对于方程 x^x = a(mod p), PH想知道对于[0,p-1]内的数,有多少个这样的x满足这个方程。请注意,虽然对于0^0的值有争论,甚至不一定有意义,可是在本题中,PH认为0^0 = 1。

#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int MAXN = 1010;int p;char a[MAXN];int result(int a,int b,int m){    long long d,t;    d=1;    t=a;    while (b>0)    {        if (b%2==1)            d=(d*t)%m;        b/=2;        t=(t*t)%m;    }    return (int)d;}int main(){    int t;    scanf("%d%*c",&t);    while (t--){        scanf("%s %d",a,&p);        int ans = 0;        for (int i = 0; i < strlen(a); i++)            ans = (ans*10+(a[i]-'0')) % p;        if (strcmp(a,"1") == 0 && p == 1)            ans = 1;        int cnt = 0;        for (int i = 1; i < p; i++){            int sum = result(i,i,p);            if (sum == ans)                cnt++;          }        if (ans == 1)            cnt++;         printf("%d\n",cnt);    }    return 0;}