上帝与集合的正确用法

Time Limit: 5 Sec  Memory Limit: 128 MB
[Submit][Status][Discuss]

Description

  

Input

  第一行一个T,接下来T行,每行一个正整数p,代表你需要取模的值。

Output

  T行,每行一个正整数,为答案对p取模后的值。

Sample Input

  3
  2
  3
  6

Sample Output

  0
  1
  4

HINT

  对于100%的数据,T<=1000,p<=10^7

Solution

  我们运用欧拉定理:

  然后还有一个定理:一个数在执行log次操作后,值不会改变。

  然后就可以直接求了。

Code

 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long s64; const int ONE = ;
const int INF = ; int T,x;
int phi[ONE],pn; int get()
{
int res=,Q=;char c;
while( (c=getchar())< || c> )
if(c=='-')Q=-;
res=c-;
while( (c=getchar())>= && c<= )
res=res*+c-;
return res*Q;
} int Quickpow(int a,int b,int MOD)
{
int res = ;
while(b)
{
if(b & ) res = (s64)res * a % MOD;
a = (s64)a * a % MOD;
b >>= ;
}
return res;
} int Getphi(int n)
{
int res = n;
for(int i=; i*i<=n; i++)
if(n % i == )
{
res = res/i*(i-);
while(n % i == ) n /= i;
}
if(n != ) res = res/n*(n-);
return res;
} int Deal(int p)
{
pn = ; phi[] = p;
while(p != ) phi[++pn] = p = Getphi(p);
phi[++pn] = ; int a = ;
for(int i=pn; i>=; i--)
{
if(a >= phi[i]) a = a%phi[i] + phi[i];
a = (s64)Quickpow(, a, phi[i-]);
if(!a) a = phi[i-];
} return a % phi[];
} int main()
{
T = get();
while(T--)
{
x = get();
printf("%d\n", Deal(x));
}
}

【BZOJ3884】上帝与集合的正确用法 [欧拉定理]的更多相关文章

  1. BZOJ3884: 上帝与集合的正确用法 拓展欧拉定理

    Description   根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α”.“α”被定义为“ ...

  2. BZOJ3884: 上帝与集合的正确用法(欧拉函数 扩展欧拉定理)

    Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 3860  Solved: 1751[Submit][Status][Discuss] Descripti ...

  3. bzoj3884: 上帝与集合的正确用法 扩展欧拉定理

    题意:求\(2^{2^{2^{2^{...}}}}\%p\) 题解:可以发现用扩展欧拉定理不需要很多次就能使模数变成1,后面的就不用算了 \(a^b\%c=a^{b\%\phi c} gcd(b,c) ...

  4. BZOJ3884 上帝与集合的正确用法 【欧拉定理】

    题目 对于100%的数据,T<=1000,p<=10^7 题解 来捉这道神题 欧拉定理的一般形式: \[a^{m} \equiv a^{m \mod \varphi(p) + [m \ge ...

  5. bzoj3884上帝与集合的正确用法

    Description   根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α”.“α”被定义为“ ...

  6. [BZOJ3884] 上帝与集合的正确用法 (欧拉函数)

    题目链接:  https://www.lydsy.com/JudgeOnline/problem.php?id=3884 题目大意: 给出 M, 求 $2^{2^{2^{2^{...}}}}$ % M ...

  7. bzoj3884 上帝与集合的正确用法

    a^b mod P=a^(b mod phi(p)) mod p,利用欧拉公式递归做下去. 代码 #pragma comment(linker,"/STACK:1024000000,1024 ...

  8. bzoj3884: 上帝与集合的正确用法 欧拉降幂公式

    欧拉降幂公式:http://blog.csdn.net/acdreamers/article/details/8236942 糖教题解处:http://blog.csdn.net/skywalkert ...

  9. bzoj千题计划264:bzoj3884: 上帝与集合的正确用法

    http://www.lydsy.com/JudgeOnline/problem.php?id=3884 欧拉降幂公式 #include<cmath> #include<cstdio ...

随机推荐

  1. 《剑指offer》---寻找反转数组最小值

    本文算法使用python3实现 1.题目描述:   把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4, ...

  2. 某一线互联网公司前端面试题总结css部分

    1,css3选择器 :not(selector) 选择页面内所有type!=text的类型: input:not([type=text]){ color: red; font-weight: bold ...

  3. 判断滚动条滚动到document底部

    滚动条没有实际的高度.只是为了呈现效果才在外型上面有长度. 在js当中也没有提供滚动条的高度API. 参考了网上有关资料:判断滚动条到底部的基本逻辑是滚动条滚动的高度加上视口的高度,正好是docume ...

  4. 关闭win7/Server 2008非正常关机启动自动修复功能

    命令提示符下输入 bcdedit /set {default} bootstatuspolicy ignoreallfailures bcdedit /set {current} recoveryen ...

  5. [C/C++] const用法详解

    const在C语言中算是一个比较新的描述符,我们称之为常量修饰符,意即其所修饰的对象为常量(immutable). 我们来分情况看语法上它该如何被使用. 1.函数体内修饰局部变量.例:void fun ...

  6. [BinaryTree] 二叉树类的实现

    二叉树结点的抽象数据类型: template<class T> class BinaryTreeNode { friend class BinaryTree<T>; priva ...

  7. ismember matlab

    ismember 判断A中的元素在B中有没有出现 LIA = ismember(A,B) for arrays A and B returns an array of the same size as ...

  8. 【刷题】SPOJ 8222 NSUBSTR - Substrings

    You are given a string S which consists of 250000 lowercase latin letters at most. We define F(x) as ...

  9. BZOJ1068:[SCOI2007]压缩——题解

    http://www.lydsy.com/JudgeOnline/problem.php?id=1068 Description 给一个由小写字母组成的字符串,我们可以用一种简单的方法来压缩其中的重复 ...

  10. LOJ6342::跳一跳——题解

    https://loj.ac/problem/6342 f[i]表示从i开始跳的期望时间,f[n]=0. 所以f[i]=(f[i]+f[i+1]+……+f[n])/(n-i+1)+1. 移项整理可求f ...