题目链接:点击打开链接
解题思路:
用到数论知识,不然的话可能就要JAVA开大数·····鉴于我JAVA这么弱,还是用数学知识吧!
(i * i * i * ····*i) % m ==( ( (i % m) * i % m) ····)%m。总之就是最后不要忘记循环结束后还要对m取一次余。
完整代码:
#include <algorithm> #include <iostream> #include <cstring> #include <climits> #include <cstdio> #include <string> #include <cmath> #include <map> #include <queue> using namespace std; typedef long long LL; const int MOD = int(1e9)+7; const int INF = 0x3f3f3f3f; const double EPS = 1e-9; const double PI = acos(-1.0); //M_PI; const int maxn = 10001; int ans[maxn]; int main() { #ifdef DoubleQ freopen("in.txt","r",stdin); #endif int n , m , y; while(cin >> n >> m>> y) { int cnt = 0; for(int i = 0 ; i < m ; i ++) { int s = 1; for(int j = 0 ; j < n ; j ++) s = (s * i) % m; if(s % m == y) ans[cnt++] = i; } if(cnt == 0) cout << "-1" << endl; else { for(int i = 0 ; i < cnt ; i ++) printf("%d%s" , ans[i] , i == cnt - 1 ? "\n" : " "); } } }
更多精彩请访问:点击打开链接
时间: 2024-10-21 00:12:09