SGU 261
来源:互联网 发布:淘宝卖家设置发货地址 编辑:程序博客网 时间:2024/04/26 01:27
给定p,k,a,其中p,k是素数,求x^k=a (mod p)。
求出所有满足条件的x。
#include<map>#include<cstdio>#include<cmath>#include<vector>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;vector<ll>f,as;ll pow(ll a,ll b,ll mod){ll as=1;while(b){if(b&1)as=(as*a)%mod;a=(a*a)%mod;b>>=1;}return as;}bool g_test(ll g,ll p){for(ll i=0;i<f.size();i++) if(pow(g,(p-1)/f[i],p)==1) return 0;return 1;}ll yuangen(ll p){f.clear();ll tmp=p-1;for(ll i=2;i<=tmp/i;i++) if(tmp%i==0){ f.push_back(i);while(tmp%i==0) tmp/=i;}if(tmp!=1)f.push_back(tmp);ll g=0;while(++g) if(g_test(g,p)) return g;}ll discrete_log(ll x,ll n,ll m){//x^y=n (mod m) 求 ymap<ll,int>rec;ll s=(ll)(sqrt(m)+0.5);ll cur=1;for(int i=0;i<s;i++){rec[cur]=i;cur=cur*x%m;}ll mul=cur;cur=1;for(int i=0;i<s;i++){ll more=n*pow(cur,m-2,m)%m;if(rec.count(more))return i*s+rec[more];cur=cur*mul%m;}return -1;}ll ex_gcd(ll a,ll b,ll& x,ll& y){if(b==0){x=1;y=0;return a;}else{ll r=ex_gcd(b,a%b,y,x);y-=x*(a/b);return r;}}void line_mod_equation(ll a,ll b,ll n){//ax=b (mod n) 求xll x,y,d;as.clear();d=ex_gcd(a,n,x,y);if(b%d==0){x%=n;x+=n;x%=n;as.push_back(x*(b/d)%(n/d));for(ll i=1;i<d;i++) as.push_back((as[0]+i*n/d)%n);}}int main(){ll a,k,p,g,q;while(~scanf("%lld%lld%lld",&p,&k,&a)){if(a==0){puts("1\n0");continue;}//a==0特判g=yuangen(p);q=discrete_log(g,a,p);line_mod_equation(k,q,p-1);for(int i=0;i<as.size();i++) as[i]=pow(g,as[i],p);sort(as.begin(),as.end());printf("%d\n",as.size());for(int i=0;i<as.size();i++) printf("%lld%c",as[i],i==as.size()-1?'\n':' ');}return 0;}
0 0
- SGU 261
- SGU 261
- sgu-261 Discrete Roots
- SGU
- SGU
- SGU
- SGU
- SGU
- SGU
- (SGU
- SGU
- SGU
- SGU
- SGU
- SGU
- SGU
- SGU
- SGU 261 Discrete Roots N次剩余
- Windows Python 27 64 位配置LibSvm接口
- [BAT]批处理修改IP地址
- simpleXML
- JavaScript加载XML文件的两种方式
- 向 Web 开发人员推荐35款 JavaScript 图形图表库
- SGU 261
- error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead.
- 达内学习日志Day54:MyBatis框架
- PyCharm --------JetBrains
- 通过Bind绑定Service通信
- apache多站点配置中ServerName ServerAlias什么意思?
- [游戏技术]求生之路服务器参数配置
- leetcode-Remove Duplicates from Sorted List II
- CSS学习笔记之<meta标签>