高次同余方程。   BL == N (mod P)求解最小的L。

/*
A^x=B(mod C)
设x=i*m-j(其中m=ceil(sqrt C))
并且i∈[1,m],j∈[0,m],以保证x能取到所有的0~C-1之间的数。
为什么只取到0~C-1就行了呢?
根据费马小定理 a^(p-1)=1(mod p)(p是素数),我们可以得到
a^(k mod p-1)=a^k(mod p)
即当k>p-1时,会出现类似于循环节的东西。
那么我们可以得到变形之后的式子
A^(i*m-j)=B(mod C)
化简之后为
A^(i*m)=B*A^j(mod C)
现在只需要枚举所有的j,然后把它们放到hash里,再枚举i,得到的第一个即为答案。
*/
#include<cstdio>
#include<iostream>
#include<cmath>
#include<map>
#define lon long long
using namespace std;
lon A,B,C;
map<lon,int> hash;
lon poww(lon a,lon b){
lon base=a,r=;
while(b){
if(b&) r*=base;r%=C;
base*=base;base%=C;
b>>=;
}
return r;
}
int main(){
while(scanf("%lld%lld%lld",&C,&A,&B)!=EOF){
if(A%C==){
printf("no solution\n");
continue;
}
hash.clear();
lon m=ceil(sqrt(C));
lon ans;
for(int i=;i<=m;i++){
if(!i){
ans=B%C;hash[ans]=i;
continue;
}
ans=(ans*A)%C;hash[ans]=i;
}
ans=;lon t=poww(A,m);bool fl=false;
for(int i=;i<=m;i++){
ans=(ans*t)%C;
if(hash[ans]){
ans=i*m-hash[ans];
ans=(ans%C+C)%C;
printf("%lld",ans);printf("\n");
fl=true;
break;
}
}
if(!fl) printf("no solution\n");
}
return ;
}

Discrete Logging(poj 2417)的更多相关文章

  1. POJ 2417 Discrete Logging (Baby-Step Giant-Step)

    Discrete Logging Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2819   Accepted: 1386 ...

  2. POJ - 2417 Discrete Logging(Baby-Step Giant-Step)

    d. 式子B^L=N(mod P),给出B.N.P,求最小的L. s.下面解法是设的im-j,而不是im+j. 设im+j的话,貌似要求逆元什么鬼 c. /* POJ 2417,3243 baby s ...

  3. [POJ2417]Discrete Logging(指数级同余方程)

    Discrete Logging Given a prime P, 2 <= P < 2 31, an integer B, 2 <= B < P, and an intege ...

  4. 01背包问题:Charm Bracelet (POJ 3624)(外加一个常数的优化)

    Charm Bracelet    POJ 3624 就是一道典型的01背包问题: #include<iostream> #include<stdio.h> #include& ...

  5. Scout YYF I(POJ 3744)

    Scout YYF I Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5565   Accepted: 1553 Descr ...

  6. (4)logging(日志模块)

    日志分成几个常用的级别 debug 10 代表程序调试过程中的信息 info 20 代表普通日志信息,用户的访问等等 warning 30 警告日志,有可能出错,但是目前还没出错的 error 40 ...

  7. Python学习笔记:logging(日志处理)

    在一个软件中,日志是可以说必不可少的一个组成部分,通常会在定位客户问题或者记录软件使用情况等场景中会用到.logging模板块是Python的一个内置标准库,用于实现对日志的控制输出,对于平常的日志输 ...

  8. 广大暑假训练1(poj 2488) A Knight's Journey 解题报告

    题目链接:http://vjudge.net/contest/view.action?cid=51369#problem/A   (A - Children of the Candy Corn) ht ...

  9. Games:取石子游戏(POJ 1067)

    取石子游戏 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37662   Accepted: 12594 Descripti ...

随机推荐

  1. 通过luac编译lua脚本

    在lua官网下载一个lua的release包,里面有已经编译好的lua启动文件(包括lua.exe),其中还有luac.exe, 这个程序是用来将lua文件编译成二进制码, 使用方法很简单,在控制台调 ...

  2. Linux-Java安装

    安装 yum -y install java-1.8.0-openjdk* Tomcat安装:到官网http://tomcat.apache.org/,然后找到Tomcat9下载地址 http://m ...

  3. Centos7系统下安装Docker

    1.确定你的Linux系统是Centos7 命令:cat /etc/redhat-release 2.yum安装gcc相关 1.配置好Centos7能上外网. 2.yum -y install gcc ...

  4. python并发编程之线程(创建线程,锁(死锁现象,递归锁),GIL锁)

    什么是线程 进程:资源分配单位 线程:cpu执行单位(实体),每一个py文件中就是一个进程,一个进程中至少有一个线程 线程的两种创建方式: 一 from threading import Thread ...

  5. 中国电信物联网平台入门学习笔记7:NB-IOT信号如何检测

    NB-IOT设备会因为信号的原因,数据发不出.但数据发不出的原因有很多,这么排除是NB-IOT信号的问题呢?那就需要NB-IOT信号检测装置. 网上的信号检测设备 作为一个常年蜗居在实验室的穷屌丝而言 ...

  6. ACM-ICPC 2016 Qingdao Preliminary Contest G. Sort

    Recently, Bob has just learnt a naive sorting algorithm: merge sort. Now, Bob receives a task from A ...

  7. source insight

    关于source inlight的版本 http://www.camnpr.com/archives/559.html   最新版本 http://www.sourceinsight.com/upda ...

  8. Chromium Embedded Framework

    关于CEF 近期由于工作需要开始研究了Google的Chromium Embedded Framework(CEF),这是一个基于Google Chromium开源代码的项目,使用CEF可以很方便的在 ...

  9. [转] NGINX宏观手记

    前言 任何一个工具都有它的灵魂所在,作为一个PHP程序员,我们可能仅仅使用了它的一小部分,这篇文章让你更加了解Nginx,本章大多都是总结.翻译.整理 ,希望你可以知道nginx不仅仅是PHP的附属品 ...

  10. chardet的下载及安装

    1.chardet下载地址 https://pypi.python.org/pypi/chardet/3.0.4#downloads 2.解压至安装路径 D:\Program Files (x86)\ ...