bzoj3884 上帝与集合的正确用法
a^b mod P=a^(b mod phi(p)) mod p,利用欧拉公式递归做下去。
代码
#pragma comment(linker,"/STACK:1024000000,1024000000")
#include<cstdio>
#include<string>
#include<iostream>
#include<map>
#include<vector>
#include<set>
#include<algorithm>
#include<cstring>
#define fi first
#define sc second
#define pb push_back
using namespace std;
int p;
int f[];
int ksm(int x,int p)
{
if (x==) return ;
long long ans=ksm(x/,p);
ans=ans*ans%p;
if (x%) ans=ans*%p;
return ans;
}
int gao(int x)
{
int i,tmp=x,ans=x;
for (i=;i*i<=tmp;i++)
if (tmp%i==)
{
while (tmp%i==) tmp/=i;
ans=ans/i*(i-);
}
if (tmp>)
ans=ans/tmp*(tmp-);
return ans;
}
int calc(int x)
{
if (x==) return ;
int phi;
if (f[x]==) f[x]=gao(x);
phi=f[x];
return (ksm(calc(phi)+phi,x));
}
int main()
{
int test;
scanf("%d",&test);
while (test--)
{
scanf("%d",&p);
printf("%d\n",calc(p));
}
}
bzoj3884 上帝与集合的正确用法的更多相关文章
- bzoj3884上帝与集合的正确用法
Description 根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α”.“α”被定义为“ ...
- BZOJ3884: 上帝与集合的正确用法 拓展欧拉定理
Description 根据一些书上的记载,上帝的一次失败的创世经历是这样的: 第一天, 上帝创造了一个世界的基本元素,称做“元”. 第二天, 上帝创造了一个新的元素,称作“α”.“α”被定义为“ ...
- BZOJ3884: 上帝与集合的正确用法(欧拉函数 扩展欧拉定理)
Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 3860 Solved: 1751[Submit][Status][Discuss] Descripti ...
- bzoj3884: 上帝与集合的正确用法 欧拉降幂公式
欧拉降幂公式:http://blog.csdn.net/acdreamers/article/details/8236942 糖教题解处:http://blog.csdn.net/skywalkert ...
- bzoj3884: 上帝与集合的正确用法 扩展欧拉定理
题意:求\(2^{2^{2^{2^{...}}}}\%p\) 题解:可以发现用扩展欧拉定理不需要很多次就能使模数变成1,后面的就不用算了 \(a^b\%c=a^{b\%\phi c} gcd(b,c) ...
- bzoj千题计划264:bzoj3884: 上帝与集合的正确用法
http://www.lydsy.com/JudgeOnline/problem.php?id=3884 欧拉降幂公式 #include<cmath> #include<cstdio ...
- BZOJ3884 上帝与集合的正确用法(欧拉函数)
设f(n)为模n时的答案,由2k mod n=2k mod φ(n)+φ(n) mod n(并不会证),且k mod φ(n)=f(φ(n)),直接就可以得到一个递推式子.记搜一发即可. #inclu ...
- bzoj3884: 上帝与集合的正确用法(数论)
感觉是今天洛谷月赛T3的弱化版,会写洛谷T3之后这题一眼就会写了... 还是欧拉扩展定理 于是就在指数上递归%phi(p)+phi(p)直到1,则后面的指数就都没用了,这时候返回,边回溯边快速幂.因为 ...
- [bzoj3884]上帝与集合的正确用法——欧拉函数
题目大意 题解 出题人博客 代码 #include <bits/stdc++.h> using namespace std; const int M = 10001000; int phi ...
随机推荐
- Mongoose 的实例方法中访问静态方法
方法比较简单,也比较粗糙和丑陋,就是通过构造函数来访问静态方法,大致如下: 123456789 WorkSpaceSchema.methods.getPrice = function(startTim ...
- android——判断网络状态
平常我们玩qq的时候我们没联网 ,qq上面回显示"网络连接不给力,请检查网络连接"那么它是 通过什么方式判断网络是否连接的呢? 下面将写个案例展示如何检查网络连接状态的 由于我们这 ...
- php ajax请求和返回
define('NOW_TIME', $_SERVER['REQUEST_TIME']); define('REQUEST_METHOD',$_SERVER['REQUEST_METHOD']); d ...
- rabbitmq使用心得
因为公司项目需要使用消息中间件,实现相关业务的异步处理,所有选用了rabbitmq.通过看文档,爬过一个一个坑,终于还是实现了相关功能. 直接上配置文件: <?xml version=" ...
- 10个jQuery小技巧
收集的10个 jQuery 小技巧/代码片段,可以帮你快速开发. 1.返回顶部按钮 你可以利用 animate 和 scrollTop 来实现返回顶部的动画,而不需要使用其他插件. $('a.top' ...
- 在SQL语句中加入时间比较作为查询条件
select * from 表名 where 列名 = ? and DATEDIFF(hh,时间列,'2016-08-22 15:05:59.000')<9
- maxscale
一.maxscale简介1.MaxScale是maridb开发的一个mysql数据中间件,其配置简单,能够实现读写分离,并且可以根据主从状态实现写库的自动切换.2.官网:https://mariadb ...
- ES6 will change the way you write JS code.
https://hacks.mozilla.org/2015/04/es6-in-depth-an-introduction/ Counting to 6 The previous editions ...
- sql server 游标 写给自己
) --定义两个局部变量 @id @name 全局变量是两个@@name ) Declare Cur Cursor For --定义一个游标 select id,name from temp1 --查 ...
- HTML 播放器 写给自己
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...