poj 2417 HASH light oj 1325 map 红黑树硬搞
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<numeric>
#include<vector>
#include<string>
#include<set>
#include<iostream>
#include<map>
#include<cmath>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 71111;
const int mod = 77777;
const int hash_size = 175447;
struct node {
ll key;
int val, next;
node() {
}
node(ll key, int val, int next) :
key(key), val(val), next(next) {
}
};
template<typename T1,typename T2>
struct HashMap {
node edge[hash_size << 2];
int i, E;
int head[hash_size + 10];
void init() {
E = 0;
memset(head, -1, sizeof(head));
}
bool insert(T1 key, T2 val) {
int u;
u = key%hash_size;
for (i = head[u]; ~i; i = edge[i].next) {
if (edge[i].key == key)
return false;
}
edge[E] = node(key, val, head[u]);
head[u] = E++;
return true;
}
int find(T1 key) {
int u = key % hash_size;
for (i = head[u]; ~i; i = edge[i].next) {
if (edge[i].key == key)
return edge[i].val;
}
return -1;
}
};
HashMap<ll,int>Hash;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1;
y = 0;
return a;
}
ll d = exgcd(b, a % b, x, y);
ll t = x;
x = y;
y = t - (a / b) * y;
return d;
}
ll go(ll A, ll B, ll C) {
if (!B)
return -1;
if (B == 1)
return 0;
int m = (int) ceil(sqrt(C + 0.0));
int i;
ll t = 1;
Hash.init();
Hash.insert(1, 0);
for (i = 1; i <= m; ++i) {
t = t * A;
if (t >= C)
t %= C;
if (t == B)
return i;
Hash.insert(t, i);
}
ll p = 1, x, y;
for (i = 1; i <= m; ++i) {
p = p * t;
if (p >= C)
p %= C;
exgcd(p, C, x, y);
x *= B;
x = (x % C + C) % C;
y = Hash.find(x);
if (y >= 0)
return 1ll * i * m + y;
}
return -1;
}
int main() {
ios::sync_with_stdio(false);
ll A, B, C;
while (~scanf("%I64d%I64d%I64d", &A, &B, &B)) {
ll ans = go(A, B, C);
if (~ans)
printf("%I64d\n", ans);
else
puts("no solution");
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <map>
#include <set>
#include <string>
#include <sstream>
#include <iomanip>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 2111;
const int N = 111111;
const double eps = 1e-10;
const double pi = acos(-1.0);
const int mod = 100000007;
typedef long long ll;
typedef pair<ll, ll> pii;
typedef vector<int> VI;
typedef vector<int> VVI;
const int m = 10001;
ll POW(ll a, ll b) {
ll res = 1;
while (b) {
if (b & 1)
res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1;
y = 0;
return a;
}
ll d = exgcd(b, a % b, x, y);
ll t = x;
x = y;
y = t - (a / b) * y;
return d;
}
ll A[m + 10];
ll go(ll K, ll result) {
map<ll, int> mp;
ll x, y;
int i;
A[0] = 1;
if (result == 1)
return 0;
mp[1] = 0;
for (i = 1; i <= m; ++i) {
A[i] = A[i - 1] * K % mod;
if (A[i] == result)
return i;
mp[A[i]] = i;
}
ll AA = 1;
for (i = 1; i <= m; ++i) {
AA = AA * A[m] % mod;
exgcd(AA, mod, x, y);
x*=result;
x = (x % mod + mod) % mod;
if (mp.count(x))
return 1ll * i * m + mp[x];
}
return -1;
}
int main() {
ios::sync_with_stdio(false);
int T;
ll K, result;
scanf("%d", &T);
//cout<<23502611/m<<" "<<23502611%m<<endl;
for (int cas = 1; cas <= T; ++cas) {
scanf("%lld%lld", &K, &result);
ll ans=go(K,result);
printf("Case %d: %lld\n",cas,ans);
}
return 0;
}