输入a和p。如果p不是素数,则若满足ap = a (mod p)输出yes,不满足或者p为素数输出no。最简单的快速幂,啥也不说了。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
ll p,a; int whether(int p)
{
int f=;
for (int i=;i*i<=p;i++)
if (p%i==)
{
f=;
break;
}
return f;
} int submain()
{
ll res=,n=p,x=a;
while (n>)
{
if (n&) res=res * x % p;
/*如果n最后一位是一,那么乘上x*/
x=x*x % p;
n>>=;
/*右移以为,即除以二*/
}
return (res==a);
} int main()
{
while (scanf("%lld%lld",&p,&a))
{
if (p==a && a==) break;
if (!whether(p))
{
if (submain()) cout<<"yes"<<endl;
else cout<<"no"<<endl;
}
else
cout<<"no"<<endl;
}
return ;
}

【快速幂】POJ3641 - Pseudoprime numbers的更多相关文章

  1. GCD&&素筛&&快速幂 --A - Pseudoprime numbers

    Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a (mod p). Th ...

  2. POJ3641 Pseudoprime numbers(快速幂+素数判断)

    POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Car ...

  3. POJ3641 Pseudoprime numbers (幂取模板子)

    给你两个数字p,a.如果p是素数,并且ap mod p = a,输出“yes”,否则输出“no”. 很简单的板子题.核心算法是幂取模(算法详见<算法竞赛入门经典>315页). 幂取模板子: ...

  4. 【POJ - 3641】Pseudoprime numbers (快速幂)

    Pseudoprime numbers Descriptions 费马定理指出,对于任意的素数 p 和任意的整数 a > 1,满足 ap = a (mod p) .也就是说,a的 p 次幂除以  ...

  5. POJ 3641 Pseudoprime numbers (数论+快速幂)

    题目链接:POJ 3641 Description Fermat's theorem states that for any prime number p and for any integer a ...

  6. HDU 3641 Pseudoprime numbers(快速幂)

    Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11336   Accepted: 4 ...

  7. poj 3641 Pseudoprime numbers 快速幂+素数判定 模板题

    Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7954 Accepted: 3305 D ...

  8. pojPseudoprime numbers (快速幂)

    Description Fermat's theorem states that for any prime number p and for any integer a > 1, ap = a ...

  9. HDU 2817 A sequence of numbers 整数快速幂

    A sequence of numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

随机推荐

  1. fileinput 小计(显示历史上传图片)

    今天又需要,要求在选中某条记录后显示历史上传图片 上传控件是fileinput.js 想法:界面有上传图片的控件,重新加载控件,并加入历史上传图片地址 实现代码: var filepathArray ...

  2. php中使用static方法

    <?php class Char{ public static $number = 0; public static $name; function __construct($what){ se ...

  3. (十五)linux下gdb调试

    一.gdb常用命令: 命令 描述 backtrace(或bt) 查看各级函数调用及参数 finish 连续运行到当前函数返回为止,然后停下来等待命令 frame(或f) 帧编号 选择栈帧 info(或 ...

  4. 一文看懂IC芯片生产流程:从设计到制造与封装

    http://blog.csdn.net/yazhouren/article/details/50810114 芯片制造的过程就如同用乐高盖房子一样,先有晶圆作为地基,再层层往上叠的芯片制造流程后,就 ...

  5. libsensor

    https://github.com/easyiot-china/libsensor https://github.com/adafruit/DHT-sensor-library https://gi ...

  6. 【LOJ2254】SNOI2017一个简单的询问

    莫队,每次询问的是两个区间,就把区间拆开,分开来算就好了. 借鉴了rank1大佬的玄学排询问的姿势. #include<bits/stdc++.h> #define N 50010 typ ...

  7. Office Excel保留两位小数的方法,网上到处乱摘的

    今天看到一位朋友的问题就在网上查了下,顺便记下来自己用  =ROUND(A1,2)-(MOD(A1*10^3,20)=5)*10^(-2)

  8. seq和{ }生成序列

    基本用法 [root@C ~]# seq 5 1 2 3 4 5 [root@C ~]# echo {1..5} 1 2 3 4 5 #步进输出 [root@C ~]# seq 1 2 5 1 3 5 ...

  9. tornado write render redirect IP

    write 用法( self.flush() ) render (跳转指定网页)用法 redirect(跳转指定路由)用法 self.request.remote_ip 显示用户 IP 地址 less ...

  10. Leetcode 之Wildcard Matching(32)

    跟上题类似,主要考虑‘*’的匹配问题.如果遇到‘*’,则跳过继续匹配,如果不匹配,则s++,重新扫描. bool isMatch2(const char *s, const char *p) { if ...