UVALive 7457 Discrete Logarithm Problem
2016-08-17 18:45
441 查看
题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5479
题意:给出a,b,p。求( a ^ x ) % p = b中的x。
思路:由于p不超过1W,那么%p后的答案也是小于p的,所以我们乘1W次如果能找到符合条件的x,那么就是存在的;否则无解。
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;
#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)
#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 0x7fffffff
#define mod 100000007
const int maxn = 10009;
int p;
int a,b;
int main()
{
cin>>p;
while( scanf("%d%d",&a,&b) == 2 )
{
if ( b == 1 )
{
puts("0");
continue;
}
int ans = 0;
int temp = 1;
bool f = false;
rep(i,1,maxn)
{
ans++;
temp = ( temp * a ) % p;
if ( temp == b )
{
f = true;
printf("%d\n",ans);
break;
}
}
if ( !f ) puts("0");
}
return 0;
}
题意:给出a,b,p。求( a ^ x ) % p = b中的x。
思路:由于p不超过1W,那么%p后的答案也是小于p的,所以我们乘1W次如果能找到符合条件的x,那么就是存在的;否则无解。
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;
#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)
#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 0x7fffffff
#define mod 100000007
const int maxn = 10009;
int p;
int a,b;
int main()
{
cin>>p;
while( scanf("%d%d",&a,&b) == 2 )
{
if ( b == 1 )
{
puts("0");
continue;
}
int ans = 0;
int temp = 1;
bool f = false;
rep(i,1,maxn)
{
ans++;
temp = ( temp * a ) % p;
if ( temp == b )
{
f = true;
printf("%d\n",ans);
break;
}
}
if ( !f ) puts("0");
}
return 0;
}
相关文章推荐
- OC-KVC/KVO
- C语言的良好编程习惯
- 一篇文章让你成为 NIO 大师 - MyCAT通信模型
- NSDateFormatter的格式串详解—将GMT格式转为NSDate
- React Native之React速学教程(下)
- c++面试题
- java测试时常见的一些错误
- error C4703: 使用了可能未初始化的本地指针变量“db”
- activity间跳转简单方式
- 用c++程序向百度apistore中发送消息获得天气等信息
- 简单强大的Android内存泄漏检测工具 LeakCanary
- 软件设计师——计算机与软件工程知识&学习要点(上午题B)
- 轻松搞定技术面系列 1——基础篇
- 转: 电商交易中支付相关知识
- 文件系统之FastDFS
- HashMap、HashTable、LinkedHashMap和TreeMap用法和区别
- 前端网站优化
- 查找练习 hash——出现过的数字
- UVALive 7456 Least Crucial Node
- 使用python抓取js动态加载的网页