#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long int64;
int Case;
#define maxn 200005
#define maxm 600005
int now[maxn],prep[maxm];
int64 val[maxm],pi,pk,t;
int64 ksm(int64 x,int64 y,int64 p){
if (y==) return %p;
if (y==) return x%p;
int64 d=ksm(x,y/,p);
if (y%==) return d*d%p*x%p;
else return d*d%p;
}
int64 exgcd(int64 a,int64 b,int64 &x,int64 &y){
if (b==){
x=,y=;
return a;
}
int64 GCD=exgcd(b,a%b,x,y),temp;
temp=x,x=y,y=temp-a/b*y;
return GCD;
}
void insert(int x,int64 y){
int pos=y%maxn;
prep[x]=now[pos],now[pos]=x,val[x]=y;
}
int search(int64 x){
int pos=x%maxn,ans=maxm*;
for (int i=now[pos];i!=-;i=prep[i]){
if (val[i]==x) ans=min(ans,i);
}
if (ans<maxm*) return ans;
else return -;
}
int64 extanded_baby_step_gaint_step(int64 A,int64 B,int64 C){
int64 tmp,temp=,x,y,D,R,cnt=; int pos;
for (int i=;i<;i++){
if (temp==B) return i;
temp=temp*A%C;
}
D=;
for (temp=exgcd(A,C,x,y);temp!=;cnt++,temp=exgcd(A,C,x,y)){
if (B%temp) return -;
C/=temp,B/=temp;
D=D*(A/temp)%C;
}
memset(now,-,sizeof(now));
R=; tmp=ceil(sqrt(C*1.0));
for (int i=;i<tmp;i++){
insert(i,R);
R=R*A%C;
}
for (int i=;i<tmp;i++){
temp=exgcd(D,C,x,y);
x=(x%C*(B/temp)%C+C)%C;
pos=search(x);
if (pos!=-) return i*tmp+pos+cnt;
D=D*R%C;
}
return -;
}
int64 inv(int64 X){
int64 x,y;
int64 temp=exgcd(X,pk,x,y);
return (x%pk+pk)%pk;
}
int64 work(int64 n){
if (n==) return 1LL%pk;
int64 ans=;
for (int i=;i<=pk;i++) if (i%pi) ans=ans*i%pk;
ans=ksm(ans,n/pk,pk);
int64 k=n%pk;
for (int i=;i<=k;i++) if (i%pi) ans=ans*i%pk;
return ans*work(n/pi)%pk;
}
int64 calc(int64 n,int64 m){
int64 a,b,c; int64 k=;
a=work(n),b=work(n-m),c=work(m);
for (int i=n;i;i/=pi) k+=(i/pi);
for (int i=n-m;i;i/=pi) k-=(i/pi);
for (int i=m;i;i/=pi) k-=(i/pi);
return 1LL*ksm(pi,k,pk)%pk*a%pk*inv(b)%pk*inv(c)%pk;
}
void work1(){
int64 n,m,p;
scanf("%lld%lld%lld",&n,&m,&p);
printf("%lld\n",ksm(n,m,p)%p);
}
void work2(){
int64 A,B,C,ans;
scanf("%lld%lld%lld",&A,&B,&C);
B%=C;
ans=extanded_baby_step_gaint_step(A,B,C);
if (ans==-) printf("Math Error\n");
else printf("%lld\n",ans);
}
void work3(){
int64 n,m,p,temp,x,y,tmp,ans=;
scanf("%lld%lld%lld",&m,&n,&p);
t=p;
for (int i=;i<=sqrt(p);i++){
if (t%i==){
pi=i,pk=;
while (t%i==){
pk=pk*i;
t/=i;
}
temp=exgcd(p/pk,pk,x,y);
tmp=calc(n,m)/temp;
x=x*tmp%p*(p/pk)%p;
ans=(ans+x)%p;
}
}
if (t>){
pi=t,pk=t;
temp=exgcd(p/pk,pk,x,y);
tmp=calc(n,m)/temp;
x=x*tmp%p*(p/pk)%p;
ans=(ans+x)%p;
}
printf("%lld\n",(ans%p+p)%p);
}
int main(){
scanf("%d",&Case);
for (int type;Case;Case--){
scanf("%d",&type);
if (type==) work1();
else if (type==) work2();
else work3();
}
return ;
}

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3283

题目大意:

吐槽:HN2016省队集训原题,辣鸡出题人......

做法:对于操作1,没什么好说的,直接上快速幂即可。

对于操作2,如果p是质数,就是BSGS算法,否则,就用扩展BSGS算法;而实际上扩展BSGS可以解决p是质数,所以就不用分类讨论了。

对于(extended)baby_step_gaint_step算法,未完待续......

对于操作3,组合数取模,扩展lucas定理。

对于组合数取模,未完待续......

bzoj3283: 运算器的更多相关文章

  1. 第一篇T语言实例开发(版本5.3),带错误检测的加减乘除运算器

    带错误检测的加减乘除运算器 表达式 TC综合开发工具里的表达式大体分为:计算表达式.条件表达式 计算表达式: 它一般是用在赋值过程中,或者是和条件表达式混合使用这样的表达式里只有数字运算符(如:+.- ...

  2. 高级四则运算器—结对项目反思(193 & 105)

    高级四则运算器—结对项目反思(193 & 105) 本周我和一位韩国同学(71061105)一起结对编程完成了我们的结对项目——高级的小学四则运算题目生成器. PSP表格   PSP2.1 P ...

  3. 高级四则运算器—结对项目总结(193 &105)

    高级四则运算器—结对项目总结 为了将感想与项目经验体会分割一下,特在此新开一篇博文. 界面设计 啥都不说,先上图震慑一下... 上面的三个界面是我们本次结对项目的主界面,恩,我也觉得挺漂亮的!你问我界 ...

  4. bzoj 3283: 运算器 扩展Baby Step Giant Step && 快速阶乘

    3283: 运算器 Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 184  Solved: 59[Submit][Status][Discuss] D ...

  5. Akka(1):Actor - 靠消息驱动的运算器

    Akka是由各种角色和功能的Actor组成的,工作的主要原理是把一项大的计算任务分割成小环节,再按各环节的要求构建相应功能的Actor,然后把各环节的运算托付给相应的Actor去独立完成.Akka是个 ...

  6. 第四次作业 四则运算器在C++层面上的进一步思考

    OOA(Object Oriented Analysis,面向对象分析) 从客观存在的事务和事务之间的关系,归纳出有关对象(包括对象的属性和行为)以及对象之间的联系,并将具有相同属性和行为的对象用一个 ...

  7. MFC实现一元稀疏多项式运算器

    MFC实现一元稀疏多项式运算器 基本要求 输入并建立两个多项式 多项式a与b相加,建立和多项式c 多项式a与b相减,建立差多项式d 输出多项式a, b, c, d.输出格式:比如多项式a为:A(x)= ...

  8. 结对编程——四则运算器(UI第十组)

    博客目录: 一.问题描述                   二.设计思路                   三.UI开发过程                       四.对接过程       ...

  9. C#四则运算器(多态方法实现)

    在上一节C#课上,我们学习了用类的继承的方式来做一个四则运算器,然而老师的代码在课上演示的效果并不理想,而且没有使用多态的思想实现,今天我们就来用多态的方式实现四则运算器. 1. 题目及要求 2. A ...

随机推荐

  1. 【C#】【Thread】Semaphore/SemaphoreSlim信号量

    System.Threading.Semaphore 类表示一个命名(系统范围)信号量或本地信号量. 它是一个对 Win32 信号量对象的精简包装. Win32 信号量是计数信号量,可用于控制对资源池 ...

  2. 学习jQuery的on事件

    开发asp.net mvc程序,多少是离不开jQuery客户程序.今天Insus.NET学习jQuery的一个on事件驱动. 先在网页视图放一个图片铵钮,用户可以使用mouse对这图片时行over,o ...

  3. CSS3 3D骰子

    z zz zz zzzz zzzzz zzzzzz

  4. Lowest Common Ancestor of a Binary Tree

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  5. codevs2693 上学路线(施工)

    难度等级:黄金 2693 上学路线(施工) 题目描述 Description 问题描述 你所在的城市街道好像一个棋盘,有a条南北方向的街道和b条东西方向的街道. 南北方向a条街道从西到东依次编号为1到 ...

  6. 20151023 - discuz 6 中 insenz 营销推广失效的问题

    将很久之前的论坛重新放在网络上,发现首页打开非常慢,用 Web Inspector 检查,发现 insenz.com 已失效导致. 解决办法: 1.进入数据库:执行 SELECT * FROM cdb ...

  7. android之拍照与摄像

    拍照和摄像的意图很简答,这里直接贴代码 布局文件 <?xml version="1.0" encoding="utf-8"?> <Linear ...

  8. CSS3——让最后一行显示省略号

    代码如下: <!DOCTYPE html> <html> <head> <title></title> <meta charset=& ...

  9. DatePicker及其监听

    xml文件: <DatePicker android:id="@+id/datep" android:layout_width="wrap_content" ...

  10. [weird problem] the xm file transfered by wcf,some sections in it were always repeated

    some sections in xml are always repeated,I received these file by wcf. I thought it's caused by buff ...