bzoj 3285 离散对数解指数方程
/**************************************************************
Problem: 3285
User: idy002
Language: C++
Result: Accepted
Time:756 ms
Memory:32072 kb
****************************************************************/ #include <cstdio>
#include <cmath>
#include <cstring>
#include <cctype>
#define N 1000010 typedef long long dnt; const int Hmod = ;
struct Hash {
int head[N], key[N], val[N], next[N], etot;
void init() {
etot = ;
memset( head, , sizeof(head) );
}
void insert( int k, int v ) {
int kk = k%Hmod;
etot++;
key[etot] = k;
val[etot] = v;
next[etot] = head[kk];
head[kk] = etot;
}
int query( int k ) {
int kk = k%Hmod;
for( int t=head[kk]; t; t=next[t] )
if( key[t]==k ) return val[t];
return -;
}
}hash; dnt mpow( dnt a, int b, int c ) {
dnt rt;
for( rt=; b; b>>=,a=(a*a)%c )
if( b& ) rt=(rt*a)%c;
return rt;
}
int findroot( int p ) {
int phi = p-;
int tmp = phi;
int stk[], top;
top = ;
for( int i=; i<=(<<); i++ ) {
if( tmp%i== ) {
stk[++top] = i;
do {
tmp/=i;
}while( tmp%i== );
}
}
if( tmp!= )
stk[++top] = tmp;
for( int r=; ; r++ ) {
bool ok = true;
for( int i=; i<=top; i++ ) {
if( mpow(r,phi/stk[i],p)== ) {
ok=false;
break;
}
}
if( ok ) return r;
}
}
dnt ind( dnt r, int a, int p ) { // ind_r(a) mod p-1
int m = ceil(sqrt(p-));
hash.init();
dnt cur = ;
for( int i=; i<m; i++ ) {
if( cur==a ) return i;
hash.insert( cur, i );
cur = (cur*r) % p;
}
dnt base;
base = cur = mpow(cur,p-,p);
for( int i=m; i<p; i+=m,cur=(cur*base)%p ) {
int j = hash.query( a*cur%p );
if( j!=- ) return i+j;
}
return -; // impossible
}
dnt gcd( dnt a, dnt b ) {
return b ? gcd(b,a%b) : a;
}
void exgcd( dnt a, dnt b, dnt &d, dnt &x, dnt &y ) {
if( b== ) {
d=a, x=, y=;
} else {
exgcd(b,a%b,d,y,x);
y-=a/b*x;
}
}
dnt meq( dnt a, dnt b, dnt c ) { // ax=b mod c
dnt d, dd, x, y;
a = (a%c+c)%c;
b = (b%c+c)%c;
d = gcd(a,c);
if( b%d!= ) return -;
exgcd(a/d,c/d,dd,x,y);
x = x*(b/d);
x = (x%(c/d)+(c/d))%(c/d);
if( x== ) x+=c/d;
return x;
} dnt a, b, c, g, p, r;
int aa[N], bb[N], cc[N], gg[N]; void read( int a[] ) {
int i;
char ch;
for( i=; isdigit(ch=getchar()); i++ )
a[i] = ch-'';
a[i] = -;
}
dnt modulo( int a[], dnt mod ) {
dnt rt = ;
for( int i=; a[i]!=-; i++ )
rt = (rt* + a[i]) % mod;
return rt;
}
int main() {
read(aa);
read(bb);
read(cc);
read(gg);
scanf( "%lld", &p );
a = modulo(aa,p-);
b = modulo(bb,p-),
c = modulo(cc,p);
g = modulo(gg,p); if( g%p== || c%p== ) {
if( g%p== && c%p== )
printf( "1\n" );
else
printf( "no solution\n" );
return ;
}
r = findroot(p);
// fprintf( stderr, "%d\n", (int)r );
dnt ans = meq( a*ind(r,g,p), ind(r,c,p)-b*ind(r,g,p), p- );
if( ans< )
printf( "no solution\n" );
else
printf( "%lld\n", ans );
}
bzoj 3285 离散对数解指数方程的更多相关文章
- bzoj 3751: [NOIP2014]解方程 同余系枚举
3.解方程(equation.cpp/c/pas)[问题描述]已知多项式方程:a ! + a ! x + a ! x ! + ⋯ + a ! x ! = 0求这个方程在[1, m]内的整数解(n 和 ...
- 牛顿迭代法解指数方程(aX + e^x解 = b )
高中好友突然问我一道这样的问题,似乎是因为他们专业要做一个计算器,其中的一道习题是要求计算器实现这样的功能. 整理一下要求:解aX + e^X = b 方程.解方程精度要求0.01,给定方程只有一解, ...
- bzoj 3751: [NOIP2014]解方程
Description 已知多项式方程: a0+a1x+a2x^2+...+an*x^n=0 求这个方程在[1,m]内的整数解(n和m均为正整数). 解题报告: 这题比较诡,看到高精度做不了,就要想到 ...
- BZOJ 3751: [NOIP2014]解方程 数学
3751: [NOIP2014]解方程 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3751 Description 已知多项式方程: ...
- bzoj 3751: [NOIP2014]解方程【数学】
--我真是太非了,自己搞了7个质数都WA,从别人那粘5个质数就A了-- 就是直接枚举解,用裴蜀定理计算是否符合要求,因为这里显然结果很大,所以我们对多个质数取模看最后是不是都为0 #include&l ...
- -【线性基】【BZOJ 2460】【BZOJ 2115】【HDU 3949】
[把三道我做过的线性基题目放在一起总结一下,代码都挺简单,主要就是贪心思想和异或的高斯消元] [然后把网上的讲解归纳一下] 1.线性基: 若干数的线性基是一组数a1,a2,a3...an,其中ax的最 ...
- bzoj 2654 && bzoj 3675 总结
手动博客搬家: 本文发表于20180929 15:18:55, 原地址https://blog.csdn.net/suncongbo/article/details/82897992 最近做到了两道( ...
- bzoj1644 / P1649 [USACO07OCT]障碍路线Obstacle Course
P1649 [USACO07OCT]障碍路线Obstacle Course bfs 直接上个bfs 注意luogu的题目和bzoj有不同(bzoj保证有解,还有输入格式不同). #include< ...
- BZOJ4869:[SHOI2017]相逢是问候——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=4869 题面复制于洛谷:https://www.luogu.org/problemnew/show/P ...
随机推荐
- select遍历list默认选中初始值
<select id="userstatus" name="userstatus"> <c:forEach items=&qu ...
- 工具类 | window批处理杀死指定端口进程
window批处理杀死指定端口进程,注意保存时使用ansi格式,运行输入端口即可 @echo off setlocal enabledelayedexpansion set /p port=请输入端口 ...
- Intellij-idea 如何编译maven工程
小编最近效应项目的要求,学习在idea上编写项目.作为一个新手遇到问题也算是正常的,重要的是把它解决,get新技能. 编写过maven工程的小伙伴们应该都知道怎么在eclipse中编译maven工程: ...
- SPOJ 8222 NSUBSTR - Substrings
http://www.spoj.com/problems/NSUBSTR/ 题意: F(x)定义为字符串S中所有长度为x的子串重复出现的最大次数 输出F[1]~F[len(S)] 用字符串S构建后缀自 ...
- SDWebImage源码阅读-第二篇
一 SDWebImageManager的downloadImageWithURL的方法 上一篇,我们刚开了个头,分析了一下开始加载图片之前如何取消其他正在下载的任务,接着,我们回到 - (void) ...
- mysql 1045 access denied for user 解决方法
提示:1045 access denied for user 'root'@'localhost' using password yes方法一: # /etc/init.d/mysql stop # ...
- c语言.函数指针数组
函数指针: 一个指向函数的指针.一般用函数名表示. 函数指针数组:元素为函数指针的数组.转移表.c语言中函数不可以定义为数组,只能通过定义函数指针来操作. #include<stdio.h> ...
- C. Ayoub and Lost Array(DP)
(又是被队友带着上分的一场--) 题目链接:http://codeforces.com/contest/1105/problem/C 题目大意:给你n,l,r.每一个数都是在l,r范围之内,然后问你这 ...
- Python人工智能之图片识别,Python3一行代码实现图片文字识别
1.Python人工智能之图片识别,Python3一行代码实现图片文字识别 2.tesseract-ocr安装包和中文语言包 注意:
- linux使用badblocks命令扫描硬盘排除故障(待验证)
检查硬盘是否产生坏道并输出# badblocks -s -v -o /root/badblocks.log /dev/sda //公司操作 -s Show the p ...