hdu 2837 Calculation
公式:a^b%p=a^(b%phi(p)+phi(p))%p b>=phi(p)
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#define ll __int64
using namespace std;
ll euler(int n)
{
ll ans=;
for(int i=;i*i<=n;i++)
{
if(n%i==)
{
ans*=i-;
n/=i;
while(n%i==)
{
ans*=i;
n/=i;
}
}
}
if(n>) ans*=n-;
return ans;
}
ll pows(ll a,ll b,ll m)
{
ll ans=;
while(b)
{
if(b&) ans=(ans*a)%m;
b>>=;
a=(a*a)%m;
}
return ans%m;
}
ll cmp(int a,int b,int m)
{
ll ans=;
for(int i=;i<=b;i++)
{
ans*=a;
if(ans>=m) return ans;
}
return ans;
}
ll fun(int n,int m)
{
ll phi=euler(m);
if(n<) return n;
ll a=fun(n/,phi);
ll b=cmp(n%,a,m);
if(b>=m)
{
ll ans=pows(n%,a+phi,m);
if(ans==)
ans+=m;
return ans;
}
return b;
}
int main()
{
int t,i,j,m,n;
cin>>t;
while(t--)
{
cin>>n>>m;
ll ans=fun(n,m)%m;
printf("%I64d\n",ans);
}
return ;
}
hdu 2837 Calculation的更多相关文章
- hdu 2837 Calculation 指数循环节套路题
Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- hdu 2837 坑题。
Calculation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- HDU 3501 Calculation 2(欧拉函数)
Calculation 2 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submi ...
- HDU 3501 Calculation 2------欧拉函数变形
Calculation 2 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 3501 Calculation 2 (欧拉函数)
题目链接 题意 : 求小于n的数中与n不互质的所有数字之和. 思路 : 欧拉函数求的是小于等于n的数中与n互质的数个数,这个题的话,先把所有的数字之和求出来,再减掉欧拉函数中所有质数之和(即为eula ...
- hdu 3501 Calculation 2 (欧拉函数)
题目 题意:求小于n并且 和n不互质的数的总和. 思路:求小于n并且与n互质的数的和为:n*phi[n]/2 . 若a和n互质,n-a必定也和n互质(a<n).也就是说num必定为偶数.其中互质 ...
- HDU 3501 Calculation 2
题目大意:求小于n的与n不互质的数的和. 题解:首先欧拉函数可以求出小于n的与n互质的数的个数,然后我们可以发现这样一个性质,当x与n互质时,n-x与n互质,那么所有小于n与n互质的数总是可以两两配对 ...
- HDU 3501 Calculation 2 ——Dirichlet积
[题目分析] 卷积太有趣了. 最终得出结论,互质数和为n*phi(n)/2即可. 计算(n*(n+1)/2-n-n*phi(n)/2)%md,用反正法即可证明. [代码] #include <c ...
- 题解报告:hdu 3501 Calculation 2 (欧拉函数的扩展)
Description Given a positive integer N, your task is to calculate the sum of the positive integers l ...
随机推荐
- 设计main函数退出后继续执行一段代码
原理: 使用 _onexit() 函数注册一个函数,这个函数会在main函数退出后执行 使用原则: 1.包含在cstdlib中,是c语言中的库函数: 2.需要注册的函数格式为:int类型返回值.无参数 ...
- javascript给不能修改的函数增加额外处理的方法
不知道是否可行,但姑且google之. 查看到相关的stack overflow(堆栈溢出)帖子:http://stackoverflow.com/questions/1659219/add-to-a ...
- sql简易的MRP资源分析
写了个简易的MRP根据传进来的数据,进行上下级的判断,父表,子表 构思: 3张变量表,第一张用来存传进来的成品,这边对表做了循环就是成品是一样一样进去的,取成品表的第一行,将数据做父表和子表关联,取出 ...
- Android 悬浮窗 WindowManager WindowManager.LayoutParamas
前方高清大图~~~~ 绘制的图片资源: // draw bitmap BitmapDrawable bmpDraw = (BitmapDrawable) getResources().getDrawa ...
- Google Ajax Library API使用方法(JQuery)
Google Ajax Library API使用方法 1.传统方式: <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7. ...
- Windows Server 2008 R2(x64) IIS7+PHP5(FastCGI)环境搭建
相关软件下载: 1.PHP下载地址: http://windows.php.net/downloads/releases/php-5.4.4-nts-Win32-VC9-x86.zip 如果是win2 ...
- VS2005 VS2008编译的程序在Win7下以管理员身份运行的设置
在VS2005或者VS2008 里面,直接项目右键---属性---连接器---清单文件---uac执行级别 选择requireAdministrator 重新编译 这样你的程序直接运行就拥有管理 ...
- Ant之build.xml
转载地址:http://www.cnblogs.com/clarkchen/archive/2011/03/10/1980194.html http://lisongqiu168.iteye.com/ ...
- 利用ajax在javascript中获取后台的值
<script type="text/javascript"> function login() { var sa = WebForm1.Hello().value; ...
- Python中处理时间 —— time模块
time模块 time 模块可用来处理时间,详细的说明参考 time模块说明. 逝去的秒数 逝去的秒数表示从某个时间(Python中是"Thu Jan 1 07:00:00 1970&quo ...