HDU 5393
[background]
保研的事终于告一段落了,之后去北京折腾了一段时间,本以为会在那里实习一个月,谁知道刚去ICT,心中就各种反感,可能是因为LP的态度吧,否则我可能会留在那里读研也说不定。花了两千多,最终灰溜溜的回来了,信心就大受打击。幸好的是,家人给予理解。八月的那一段时间,内心实在十分苦闷,也就很久没刷题了,这也是没办法的事。保研的事一直就缠绕着我,只想着复习复习,最后把线代就又重撸了一遍,把操作系统重新看了一遍,把离散又重新看了一遍,看完后内心其实依然忐忑,生怕面试老师会问各种奇怪的问题,于是又把那些常用的问题自己预备好答案。。。。最终,去的学校还算好吧,但过程中的郁闷、失落,大概没有人能体会到。
这道题是去面试前写的,放了很久没发上来,现在发一下吧。
[main]
移项合并后,你会发现其实就是求a^x=1 mod q,求最小的x。想到欧拉函数,只要欧拉函数的因数中有满足的最小,即可。
由于数其实较大,使用rho分解法,对欧拉中的数分解所有的质数。求那个最小的因数时,只需拿质数去不停地除phi得d,同时验证等式是否成立,直到a^d=1不成立,此时d*质数,又或者phi%d!=0。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll __int64
using namespace std; ll k,b,x,p; //****************************************************************
const int S=20;//随机算法判定次数,S越大,判错概率越小 //计算 (a*b)%c. a,b都是long long的数,直接相乘可能溢出的
// a,b,c <2^63
long long mult_mod(long long a,long long b,long long c)
{
a%=c;
b%=c;
long long ret=0;
while(b)
{
if(b&1){ret+=a;ret%=c;}
a<<=1;
if(a>=c)a%=c;
b>>=1;
}
return ret;
} //计算 x^n %c
long long pow_mod(long long x,long long n,long long mod)//x^n%c
{
if(n==1)return x%mod;
x%=mod;
long long tmp=x;
long long ret=1;
while(n)
{
if(n&1) ret=mult_mod(ret,tmp,mod);
tmp=mult_mod(tmp,tmp,mod);
n>>=1;
}
return ret;
} //以a为基,n-1=x*2^t a^(n-1)=1(mod n) 验证n是不是合数
//一定是合数返回true,不一定返回false
bool check(long long a,long long n,long long x,long long t)
{
long long ret=pow_mod(a,x,n);
long long last=ret;
for(int i=1;i<=t;i++)
{
ret=mult_mod(ret,ret,n);
if(ret==1&&last!=1&&last!=n-1) return true;//合数
last=ret;
}
if(ret!=1) return true;
return false;
} // Miller_Rabin()算法素数判定
//是素数返回true.(可能是伪素数,但概率极小)
//合数返回false; bool Miller_Rabin(long long n)
{
if(n<2)return false;
if(n==2)return true;
if((n&1)==0) return false;//偶数
long long x=n-1;
long long t=0;
while((x&1)==0){x>>=1;t++;}
for(int i=0;i<S;i++)
{
long long a=rand()%(n-1)+1;//rand()需要stdlib.h头文件
if(check(a,n,x,t))
return false;//合数
}
return true;
} //************************************************
//pollard_rho 算法进行质因数分解
//************************************************
long long factor[100];//质因数分解结果(刚返回时是无序的)
int tol;//质因数的个数。数组小标从0开始 long long gcd(long long a,long long b)
{
if(a==0)return 1;//???????
if(a<0) return gcd(-a,b);
while(b)
{
long long t=a%b;
a=b;
b=t;
}
return a;
} long long Pollard_rho(long long x,long long c)
{
long long i=1,k=2;
long long x0=rand()%x;
long long y=x0;
while(1)
{
i++;
x0=(mult_mod(x0,x0,x)+c)%x;
long long d=gcd(y-x0,x);
if(d!=1&&d!=x) return d;
if(y==x0) return x;
if(i==k){y=x0;k+=k;}
}
}
//对n进行素因子分解
void findfac(long long n)
{
if(Miller_Rabin(n))//素数
{
factor[tol++]=n;
return;
}
long long p=n;
while(p>=n)p=Pollard_rho(p,rand()%(n-1)+1);
findfac(p);
findfac(n/p);
} ll getphi(ll q){
for(int i=0;i<tol;i++) {
q=q-q/factor[i];
}
return q;
} void uniqued(){
sort(factor,factor+tol);
int tt=tol; tol=1;
for(int i=1;i<tt;i++){
if(factor[i]!=factor[tol-1])
factor[tol++]=factor[i];
}
} int main(){
int T,cc;
scanf("%d",&T);
while(T--){
tol=0;
scanf("%I64d%I64d%I64d%I64d",&k,&b,&x,&p);
if(!k){puts(b==x?"1":"-1");continue;}
if(k==1)
{
if(!b)puts("1");
else
{
printf("%d\n",p/gcd(b,p));
}
continue;
}
ll f=p*(k-1),s=(k-1)*x+b;
ll q=f/gcd(f,s);
if(q==1){puts("1");continue;}
if(gcd(k,q)!=1){puts("-1");continue;}
findfac(q);
uniqued();
ll phi=getphi(q);
tol=0;
findfac(phi);
uniqued();
for(int i=0;i<tol;i++){
if(phi%factor[i]==0){
phi/=factor[i];
while(true){
if(pow_mod(k,phi,q)!=1ll||phi%factor[i]!=0) break;
phi/=factor[i];
}
if(pow_mod(k,phi,q)!=1ll) phi*=factor[i];
}
}
printf("%I64d\n",phi);
}
return 0;
}
HDU 5393的更多相关文章
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- hdu 4481 Time travel(高斯求期望)(转)
(转)http://blog.csdn.net/u013081425/article/details/39240021 http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 3791二叉搜索树解题(解题报告)
1.题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=3791 2.参考解题 http://blog.csdn.net/u013447865/articl ...
- hdu 4329
problem:http://acm.hdu.edu.cn/showproblem.php?pid=4329 题意:模拟 a. p(r)= R'/i rel(r)=(1||0) R ...
随机推荐
- Fib(兔子问题)python实现多种方法
# 斐波那契数列是学计算机入门最经典的一道题目 # 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci) # ...
- ACM_整数反转
整数反转 Time Limit: 2000/1000ms (Java/Others) Problem Description: 给定一个32位int型的整数,把这个整数反着输出,如123,输出321. ...
- 【转】Linux下history命令用法
转自:http://blog.sina.com.cn/s/blog_5caa94a00100gyls.html 如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你 ...
- RESTful 设计理论
RESTful 设计: 1.协议通信协议:https 2.域名部署在API专用域名下,除非API很简单(https://www.example.com/api)https://api.example. ...
- jdk11安装没有jre文件夹
原因:jdk11安装之后是没有jre的 如果需要jre,需要到jdk目录下面去 打开命令窗口,然后执行如下命令: bin\jlink.exe --module-path jmods --a ...
- mysql外键创建失败原因
引用:http://blog.csdn.net/wangpeng047/article/details/19624351 首先,如果和外键相关的几张表中已经插入了数据,可能导致外键插入的失败 在MyS ...
- CSS——层叠性
层叠性:浏览器渲染是从上而下的,当多个样式作用于同一个(同一类)标签时,样式发生了冲突,总是执行后边的代码(后边代码层叠前边的代码).和标签调用选择器的顺序没有关系. <!DOCTYPE htm ...
- 使用 Spring Social 连接社交网络
Spring Social 框架是spring 提供社交平台的分享组件 https://www.ibm.com/developerworks/cn/java/j-lo-spring-social/
- 10、scala面向对象编程之Trait
1. 将trait作为接口使用 2.trait中定义具体方法 3.trait定义具体字段 4.trait中定义抽象字段 5.为实例对象混入trait 6.trait调用链 7.在trait中覆盖抽象 ...
- C# 调用指定打印机 (并不是默认)
this.printDocument1.PrinterSettings.PrinterName = "Microsoft XPS Document Writer"; this.pr ...