这道题的题意很简单,就是给你一个数S,问你是否存在一个数n使得n^n=S。如果不存在则输出-1,反之输出n。
这道题一开始做会想到大数用java,python。但是不会写java版的快速幂。。
但是何必那么麻烦呢,先把n的n次方在一个质数的模意义下打表保存在数组里面,同时对于S也转化在模意义下,那问题不久解决了吗?
当然,这个质数一定要大,不要问为什么是质数,因为这样可以使hash冲突减小(不是一定消除冲突)。如果不放心可以双哈希,用map存起来。
所以问题就解决啦!
#include <cstdio>
#include <iostream>
#include <cmath>
#include <map>
#include <queue>
#include <vector>
#include <ios>
#include <bitset>
#include <time.h>
#include <stack>
#include <cstring>
#include <iomanip>
#include <algorithm>
#define lowbit(x) ((x)&(-x))
#define mp make_pair
#define pb push_back
#define Pi acos(-1.0)
#define E exp(1.0)
#define sz(a) (int)(a).size()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ud;
typedef pair<int, int> PI;
const double eps = 1e-6;
const int mod = 1e9;
const int maxn = 5e5+5;
int Case = 1, n, m;
ll fa[maxn];
ll power(ll a, ll b) {
ll res = 1;
while(b) {
if(b&1) res = (res*a)%mod;
a = (a*a)%mod;
b = b>>1;
}
return res;
}
void solve() {
string ss;
cin>>ss;
int len = ss.size();
ll res = 0;
for(int i = 0; i < len; i++) {
res = (res*10%mod+ss[i]-'0')%mod;
}
for(int i = 1; i < maxn; i++) {
if(res == fa[i]) {
cout<<i<<endl;
return;
}
}
cout<<-1<<endl;
}
int main() {
ios::sync_with_stdio(false);
//freopen("in.txt", "r", stdin);
for(int i = 1; i < maxn; i++) {
fa[i] = power((ll)i, (ll)i);
}
cin>>Case;
while(Case--)
//while(cin>>n)
solve();
return 0;
}