poj 2417 lightoj 1325 求解离散对数 BABY STEP

47 篇文章 0 订阅
20 篇文章 0 订阅
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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值