2480: Spoj3105 Mod

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 557  Solved: 210
[Submit][Status][Discuss]

Description

已知数a,p,b,求满足a^x≡b(mod p)的最小自然数x。

Input

    每个测试文件中最多包含100组测试数据。
    每组数据中,每行包含3个正整数a,p,b。
    当a=p=b=0时,表示测试数据读入完全。

Output

    对于每组数据,输出一行。
    如果无解,输出“No Solution”(不含引号),否则输出最小自然数解。

Sample Input

5 58 33
2 4 3
0 0 0

Sample Output

9
No Solution
 
 
 
【吐槽】
一道很简单的模板题。
当p=1时记得要特判。。。(博主因为这个wa了无数次)
 /*************
bzoj 2480
by chty
2016.11.8
*************/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<algorithm>
using namespace std;
#define mod 99991
typedef long long ll;
struct node{ll v,num,f;}hash[mod+];
ll A,B,C;
inline ll read()
{
ll x=,f=; char ch=getchar();
while(!isdigit(ch)) {if(ch=='-') f=-; ch=getchar();}
while(isdigit(ch)) {x=x*+ch-''; ch=getchar();}
return x*f;
}
ll gcd(ll a,ll b) {return !b?a:gcd(b,a%b);}
void insert(ll v,ll x)
{
ll t=v%mod;
while(hash[t].f&&hash[t].v!=v) {t++; if(t>mod) t-=mod;}
if(!hash[t].f) {hash[t].f=;hash[t].num=x;hash[t].v=v;}
}
ll find(ll v)
{
ll t=v%mod;
while(hash[t].f&&hash[t].v!=v) {t++; if(t>mod) t-=mod;}
if(!hash[t].f) return -;
else return hash[t].num;
}
void exgcd(ll a,ll b,ll &x,ll &y)
{
if(!b) {x=; y=; return;}
exgcd(b,a%b,x,y);
ll t=x;x=y;y=t-a/b*y;
}
ll Shank()
{
ll ret=;
for(ll i=;i<=;i++) {if(ret==B) return i;ret=ret*A%C;}
ll temp,ans(),cnt();
while((temp=gcd(A,C))!=)
{
if(B%temp) return -;
C/=temp; B/=temp;
ans=ans*(A/temp)%C;
cnt++;
}
ll m=(ll)ceil(sqrt(C*1.0)),t();
for(ll i=;i<m;i++) {insert(t,i);t=t*A%C;}
for(ll i=;i<m;i++)
{
ll x,y;
exgcd(ans,C,x,y);
ll val=x*B%C;
val=(val%C+C)%C;
ll j=find(val);
if(j!=-) return m*i+j+cnt;
ans=ans*t%C;
}
return -;
}
void pre() {for(int i=;i<=mod;i++)hash[i].f=,hash[i].num=hash[i].v=-;}
int main()
{
freopen("cin.in","r",stdin);
freopen("cout.out","w",stdout);
while(~scanf("%d%d%d",&A,&C,&B))
{
if(!(A+B+C)) break;
if(C==) {puts(""); continue;}
pre(); A%=C; B%=C;
ll ans=Shank();
if(ans==-) puts("No Solution");
else printf("%lld\n",ans);
}
return ;
}
 

【bzoj2480】Spoj3105 Mod的更多相关文章

  1. 【模板】exBSGS/Spoj3105 Mod

    [模板]exBSGS/Spoj3105 Mod 题目描述 已知数\(a,p,b\),求满足\(a^x\equiv b \pmod p\)的最小自然数\(x\). 输入输出格式 输入格式: 每个测试文件 ...

  2. 【BZOJ1467/2480】Pku3243 clever Y/Spoj3105 Mod EXBSGS

    [BZOJ1467/2480]Pku3243 clever Y/Spoj3105 Mod Description 已知数a,p,b,求满足a^x≡b(mod p)的最小自然数x. Input      ...

  3. 【HDU2815】【拓展BSGS】Mod Tree

    Problem Description   The picture indicates a tree, every node has 2 children.  The depth of the nod ...

  4. 【CF889E】Mod Mod Mod DP

    [CF889E]Mod Mod Mod 题意:给你一个序列$a_1,a_2...a_n$,定义$f(x,n)=x\mod a_n$,$f(x,i)=x\mod a_i+f(x \mod a_i,i+1 ...

  5. 【BZOJ】【2480】【SPOJ 3105】Mod

    扩展BSGS Orz zyf……然而他的题解对AC大神的题解作了引用……而坑爹的百度云……呵呵了... 扩展BSGS模板题 /************************************* ...

  6. 【51NOD-0】1046 A^B Mod C

    [算法]快速幂运算 [题解]快速幂的原理是把幂用二进制表示,从最低位a,次低位a2,次次低位(a2)2. #include<cstdio> long long quick_pow(long ...

  7. 【51NOD-0】1008 N的阶乘 mod P

    [算法]简单数学 [题解]多项式展开:(a*b)%p=(a%p*b%p)%p #include<cstdio> #include<algorithm> #define rep( ...

  8. 通用js函数集锦<来源于网络/自己> 【一】

    通用js函数集锦<来源于网络/自己>[一] 1.返回一个全地址2.cookie3.验证用户浏览器是否是微信浏览器4.验证用户浏览器是否是微博内置浏览器5.query string6.验证用 ...

  9. 首师大附中互测题:LJX的校园:入学典礼【C003】

    [C003]LJX的校园:入学典礼[难度C]—————————————————————————————————————————————————————————————————————————————— ...

随机推荐

  1. WPF自定义控件之图形解锁控件 ScreenUnLock

    ScreenUnLock 与智能手机上的图案解锁功能一样.通过绘制图形达到解锁或记忆图形的目的. 本人突发奇想,把手机上的图形解锁功能移植到WPF中.也应用到了公司的项目中. 在创建ScreenUnL ...

  2. hibernate 多对多一个对象出现多条记录问题

    hibernate 多对多时,当须要依据它关联的对象查找的时候,会出现一个对象有多条记录的问题 用 left join fetch 抓取查询的时候还是会出现这问题,是由于主表在关联表中有多条记录 用 ...

  3. distinct和group by的性能比较

    distinct和group by的性能比较 当去重复的字段 的个数比较多的时候,group by 比distinct要快很多 当去重复的字符 的个数比较少的时候,distinct 比group by ...

  4. 纯 as3 项目中引用 fl 包下的类

    如果安装了 Flash IDE, 将下面的文件添加到项目的 libs 中即可:D:\Program Files\Adobe\Adobe Flash CS6\Common\Configuration\A ...

  5. Linux history时间用户ip设置

    Linux history时间用户ip设置        在使用linux服务器的时候发生一些不知道谁操作的问题,google一下说history命令可以查看到历史记录,用过之后发现还是不够详细,再g ...

  6. sphinx-1.3.0扩展在pPHP 7.0.7版本编译不通过

    在这个网友也是在php7上面编译插件不通过 https://bugs.php.net/bug.php?id=71586 下php7对应的扩展文件即可 http://git.php.net/?p=pec ...

  7. 微信卡券开发,代金券修改卡券信息返回40145错误码: invalid update! Can not both set PayCell and CenterCellInfo(include: center_title, center_sub_title, center_url). hint: [DZ9rna0637ent1]

      修改代金券,接口返回的数组是这样的内容 Array ( [errcode] => 40145 [errmsg] => invalid update! Can not both set  ...

  8. (转)Java调用SQL Server的存储过程详解

    本文转载自:http://dev.yesky.com/128/8088128.shtml 1使用不带参数的存储过程 使用 JDBC 驱动程序调用不带参数的存储过程时,必须使用 call SQL 转义序 ...

  9. 转-SpringMVC——之 国际化

    原文地址:http://www.cnblogs.com/liukemng/p/3750117.html 在系列(7)中我们讲了数据的格式化显示,Spring在做格式化展示的时候已经做了国际化处理,那么 ...

  10. ALSA声卡笔记2---ASoC驱动框架

    1.简单了解一下ASOC 在嵌入式系统里面的声卡驱动为ASOC(ALSA System on Chip) ,它是在ALSA 驱动程序上封装的一层   分为3大部分,Machine,Platform和C ...