HDU 2815
特判B不能大于等于C
高次同余方程
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; const int Maxn=65535; struct hash{
int a,b,next;
}Hash[Maxn*2];
int flag[Maxn+66];
int top,idx; void insert(int a,int b){
int k=b&Maxn;
if(flag[k]!=idx){
flag[k]=idx;
Hash[k].next=-1;
Hash[k].a=a;
Hash[k].b=b;
return ;
}
while(Hash[k].next!=-1){
if(Hash[k].b==b) return ;
k=Hash[k].next;
}
Hash[k].next=++top;
Hash[top].next=-1;
Hash[top].a=a;
Hash[top].b=b;
} int find(int b){
int k=b&Maxn;
if(flag[k]!=idx) return -1;
while(k!=-1){
if(Hash[k].b==b)
return Hash[k].a;
k=Hash[k].next;
}
return -1;
} int gcd(int a,int b){
return b==0? a:gcd(b,a%b);
} int ext_gcd(int a,int b,int &x,int &y){
int t,ret;
if(!b){
x=1; y=0;
return a;
}
ret=ext_gcd(b,a%b,x,y);
t=x; x=y; y=t-a/b*y;
return ret;
} int Inval(int a,int b,int n){
int x,y,e;
ext_gcd(a,n,x,y);
e=(long long )x * b%n;
return e<0?e+n:e;
} int pow_mod(long long a,int b,int c){
long long ret=1%c; a%=c;
while(b){
if(b&1)
ret=ret*a%c;
a=a*a%c;
b=b>>1;
}
return ret;
} int BabyStep(int A,int B,int C){
top=Maxn; ++idx;
long long buf=1%C,D=buf,K;
int i,d=0,tmp;
for(i=0;i<=100;buf=buf*A%C,i++){
if(buf==B)
return i;
}
while((tmp=gcd(A,C))!=1){
if(B%tmp) return -1;
++d;
C/=tmp;
B/=tmp;
D=D*A/tmp%C;
}
int M=(int)ceil(sqrt((double)C));
for(buf=1%C,i=0; i<= M; buf=buf*A%C,i++){
insert(i,buf);
}
for(i=0,K=pow_mod((long long )A,M,C);i<=M;D=D*K%C,i++){
tmp=Inval((int)D,B,C); int w;
if(tmp>=0&&(w=find(tmp))!=-1)
return i*M+w+d;
}
return -1;
} int main(){
int A,B,C;
while(scanf("%d%d%d",&A,&C,&B)!=EOF){
if(B>=C) {
printf("Orz,I can’t find D!\n");
continue;
}
int ans=BabyStep(A,B,C);
if(ans==-1) printf("Orz,I can’t find D!\n");
else printf("%d\n",ans);
}
return 0;
}
HDU 2815的更多相关文章
- HDU 2815 Mod Tree 离散对数 扩张Baby Step Giant Step算法
联系:http://acm.hdu.edu.cn/showproblem.php?pid=2815 意甲冠军: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...
- hdu 2815 Mod Tree (exBSGS)
http://acm.hdu.edu.cn/showproblem.php?pid=2815 //解 K^D ≡ N mod P #include<map> #include<cma ...
- hdu 2815 Mod Tree 高次方程,n不为素数
Accepted 406MS 8576K 2379 B C++/** 这里加了一点限制,,大体还是一样的,, **/ #include <iostream> #include <cs ...
- HDU 2815 Mod Tree
不会,先搁着…… http://blog.csdn.net/acm_cxlove/article/details/7832197
- POJ 3243 // HDU 2815(改下输出,加个判断)
A^x = B (mod C) 的模板题,不够要用扩展BSGS (虽然AC,但完全理解不了模板0.0,以后学好数学在来慢慢理解555555) #include <iostream> #in ...
- HDU 2815 扩展baby step giant step 算法
题目大意就是求 a^x = b(mod c) 中的x 用一般的baby step giant step 算法会超时 这里参考的是http://hi.baidu.com/aekdycoin/item/2 ...
- hdu 2815 : Mod Tree 【扩展BSGS】
题目链接 直接用模板好了.实在不行,反正有队友啊~~~~ #include<bits/stdc++.h> using namespace std; typedef long long LL ...
- HDU 2815 Mod Tree (扩展 Baby Step Giant Step )
Mod Tree Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- HDU题解索引
HDU 1000 A + B Problem I/O HDU 1001 Sum Problem 数学 HDU 1002 A + B Problem II 高精度加法 HDU 1003 Maxsu ...
随机推荐
- oc13--pragma mark
// // main.m // #pragma mark基本使用 // // Created by xiaomage on 15/6/18. // Copyright (c) 2015年 xiaoma ...
- 0x57 倍增优化DP
真的是下定了巨大的决心来搞这一讲,果不其然耗了一晚上 开车旅行(真的是NOIP的题吗怎么这么恐怖) 首先,先用set把小A和小B从城市i出发,到达的下一个城市预处理出来. f[i][j][k]表示走了 ...
- Sublime Text 2 界面主题 推荐 Flatland
先搜索下THEME-FLATLAND 安装完后在preferences中选择settings-usr { "color_scheme": "Packages/Theme ...
- 关于H5优化的一些问题
required修改默认提示 : <form action="abc.php"> <input type="text" required o ...
- ASP.NET Core Web API下事件驱动型架构的实现
mvp 原创博文:http://www.cnblogs.com/daxnet/p/8082694.html
- Mysql数据的增删改
插入数据 INSERT 更新数据 UPDATE 删除数据 DELETE 再来回顾一下之前我们练过的一些操作,相信大家都对插入数据.更新数据.删除数据有了全面的认识.那么在mysql中其实最重要的不 ...
- 取消页面按钮的enter按下事件
<script src="../../@Javascript/jquery-1.8.1.js"></script> <script lan ...
- wolf
package com.wh.Demo50; /** * @author 王恒 * @datetime 2017年4月7日 下午4:40:54 * @description * 分析:子类重写父类的方 ...
- All Metro Apps on Windows 8.1 Do Not Work
所有的Metro Apps不能够正常打开,表现为打开后自动最小化到任务栏,并且不能恢复正常状态.在Event Viewer\Application中相应的错误信息为: Activation of ap ...
- 关于FastIo
由于你的驱动将要绑定到文件系统驱动的上边,文件系统除了处理正常的IRP 之外,还要处理所谓的FastIo.FastIo是Cache Manager 调用所引发的一种没有irp 的请求.换句话说,除了正 ...