UVaLive 7457 Discrete Logarithm Problem (暴力)

题意:求一个x使得 a^x%p = b p为素数

析:从1开始扫一下就好,扫到p-1就可以了,关键是这个题为什么要用文件尾结束,明明说是0,但是不写就WA。。。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e3 + 100;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
int solve(int a, int b, int m){
    int x = a;
    for(int i = 1; i < m; ++i){
        if(x == b)  return i;
        x = (x * a) % m;
    }
    return 0;
}

int main(){
    int r, m, a;
    scanf("%d", &m);
    while(scanf("%d", &a) == 1){
        if(!a)  break;
        scanf("%d", &r);
        int ans = solve(a, r%m, m);
        printf("%d\n", ans);
    }
    return 0;
}
时间: 2024-12-19 21:03:41

UVaLive 7457 Discrete Logarithm Problem (暴力)的相关文章

UVALIVE 2955 Vivian&#39;s Problem

参考: http://blog.csdn.net/acm_cxlove/article/details/7860735 感觉这里需要记录一下 #include <map> #include <set> #include <list> #include <cmath> #include <ctime> #include <deque> #include <stack> #include <queue> #incl

UVALive - 3521 Joseph&#39;s Problem (整除分块)

给定$n,k$$(1\leqslant n,k\leqslant 10^9)$,计算$\sum\limits _{i=1}^nk\: mod\:i$ 通过观察易发现$k\%i=k-\left \lfloor \frac{k}{i} \right \rfloor*i$,因此我们考虑把$\left \lfloor \frac{k}{i} \right \rfloor$的值相同的$i$分成一组直接求和,复杂度为$O(\sqrt{n})$. 整除分块原理(选自某dalao博客) 1 #include<b

Matching Problem 暴力

Matching Problem 暴力 题意: 给一个序列a,长度为n,序列b,长度为4,求a的子序列中,有多少个长度与b相等且当\(b_i = b_j\) 时 \(a_i\) = \(a_j\) 题解: 因为n<300,所以用了一个三重循环,暴力求解,注意的是,子序列不用去重. #include <cstdio> int cnt[310], c[310][310]; int main() { int n, ans = 0, a[310], b[5]; scanf("%d&qu

HDU 2601 An easy problem(暴力枚举/质因子分解)

An easy problem Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7963    Accepted Submission(s): 1920 Problem Description When Teddy was a child , he was always thinking about some simple math p

hdu4282A very hard mathematic problem 暴力枚举

//给出k //找x,y,z使得x^z+y^z+x*y*z = k //x,y,z都为正整数x<y,z>1问有多少种方法 //当z = 2时,可以看到左边是一个完全平方 //而当z>=3时,可以暴力枚举x,y //由于k<2^31所以x<2^(31/3)枚举复杂度可以过 #include<cstdio> #include<cstring> #include<iostream> #include<cmath> using name

Killer Problem(暴力)

题意: 给定一个序列,每次询问l到r之间两个数差的绝对值的最小值. 分析: 开始以为是线段树离线处理,实际暴力就好! #include <map> #include <set> #include <list> #include <cmath> #include <queue> #include <stack> #include <cstdio> #include <vector> #include <st

HDU 1032 [The 3n + 1 problem] 暴力模拟

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1032 题目大意:给出i,j,要求输出i j之间"3N+1"的循环次数的最大值. 关键思想:暴力,模拟.可以优化,因为某些大数在进行操作时,会变为已经求过的小数,之后的循环次数已求过. 代码如下: //between i,j 模拟,暴力 #include <iostream> #include <algorithm> using namespace std; int

hdu3448Bag Problem 暴力枚举

//给k个数,问最多取n个,所取的数的和不大于m的最大的和 //暴力枚举所有情况 #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std ; const int maxn = 110 ; typedef int ll; ll ans = 0 ; ll a[maxn] ; ll n , m ; int k ; void dfs(i

UVA11898 - Killer Problem(暴力)

题目链接 题目大意:给你n个数字(有顺序),然后给你范围l和r,让你在这个范围内找出最小的绝对差值. 解题思路:因为这个数字的范围是从1到10000,这样就说明长度大于10000的时候,肯定会有相同的数字出现,所以结果一定是0:同样也说明最多只需要判断10000个数字,这是最极端的情况,所以暴力可以过. 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; c