此题用到的公式:a^b%c=a^(b%phi(c)+phi(c))%c (b>=phi(c)).

1.当n!<phi(p)时,直接暴力掉;

2.当n!>=phi(p) && n!%phi(p)!=0,用上面公式求;

3.当n!>=phi(p) && n!%phi(p)==0,变为n^(phi(p))%p,找循环节,就可以了

 #include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<string>
#include<cstdlib>
#include<vector>
#define ll unsigned __int64
using namespace std;
ll an[];
ll euler(ll 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 mod)
{
ll ans=;
while(b)
{
if(b&) ans=(ans*a)%mod;
b>>=;
a=(a*a)%mod;
}
return ans%mod;
}
int main()
{
int t,k=;
ll m,ans,fac,c,i,j,phi,b,p;
cin>>t;
while(t--)
{
scanf("%I64u%I64u%I64u",&b,&p,&m);
printf("Case #%d: ",++k);
if(p==)
{
if(m==18446744073709551615U)
printf("18446744073709551616\n");
else printf("%I64u\n",m+);
continue;
}
ans=;fac=;
phi=euler(p);
//n!<phi(p)
for(i=;i<=m&&fac<=phi;i++)
{
if(pows(i,fac,p)==b)
ans++;
fac*=(i+);
}
fac%=phi;
//n!>=phi(p) && n!%phi(p)!=0
for(;i<=m&&fac;i++)
{
if(pows(i,fac+phi,p)==b)
ans++;
fac=(fac*(i+))%phi;
}
//n!>=phi(p) && n!%phi(p)==0
if(i<=m)
{
ll cnt=;
// memset(an,0,sizeof(an));
for(j=;j<p;j++)
{
an[j]=pows(i+j,phi,p);
if(an[j]==b)
cnt++;
}
c=(m-i+)/p;
ans+=c*cnt;
ll remind=(m-i+)%p;
for(j=;j<remind;j++)
if(an[j]==b)
ans++;
}
printf("%I64u\n",ans);
}
return ;
}

hdu 4335 What is N?的更多相关文章

  1. 数论 + 公式 - HDU 4335 What is N?

    What is N? Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=4335 Mean: 给你三个数b.P.M,让你求有多少个n满 ...

  2. HDU 4335 Contest 4

    利用降幂公式..呃,还是自己去搜题解吧.知道降幂公式后,就不难了. #include <iostream> #include <cstdio> #include <alg ...

  3. HDU 4335 What is N?(指数循环节)题解

    题意: 询问有多少数\(n\)满足\(n^{n!}\equiv b\mod p \land\ n\in[1,M]\),数据范围:\(M\leq2^{64}-1,p\leq1e5\) 思路: 这题显然要 ...

  4. HDU Today(最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others)     ...

  5. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  6. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

  7. hdu 4859 海岸线 Bestcoder Round 1

    http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...

  8. HDU 4569 Special equations(取模)

    Special equations Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  9. HDU 4006The kth great number(K大数 +小顶堆)

    The kth great number Time Limit:1000MS     Memory Limit:65768KB     64bit IO Format:%I64d & %I64 ...

随机推荐

  1. WP开发笔记——WP APP添加页面跳转动画

    微软的toolkit团队为我们为我们提供了这样一套组件,叫做TransitionServices服务. 简单说一下它具备的效果: turnstile(轴旋转效果): turnstile feather ...

  2. no permissions fastboot

    no permissions  fastboot 获取fastboot文件 1.编译后得android源码会在目录: andsource2/out/host/linux-x86/bin 产生fastb ...

  3. stanford moss

    A System for Detecting Software Plagiarism UPDATES May 18, 2014 Community contributions (incuding a ...

  4. 自己手写简约实用的Jquery tabs插件(基于bootstrap环境)

    一直想改版网站首页的图书展示部分,以前的展示是使用BootStrap的传统的collapse,网页篇幅占用大,也不够美观,操作也相对来说比较麻烦.于是有了自己利用Jquery来做一个图书展示的tabs ...

  5. php生成txt文件换行问题

    用双引号即"\r\n"换行,不能用单引号即'\r\n'.

  6. PHP操作MongoDB简明教程(转)

    转自:http://blog.sina.com.cn/s/blog_6324c2380100ux2m.html MongoDB是最近比较流行的NoSQL数据库,网络上关于PHP操作MongoDB的资料 ...

  7. sqlalchemy - day1

    一.Create engine Database url规则: dialect+driver://username:password@host:port/database echo: True表示cm ...

  8. 使用另一种方式实现js中Function的调用(call/apply/bind)

    在JavaScript中函数的调用可以有多种方式,但更经典的莫过于call和apply.call跟apply都绑定在函数上,他们两个的第一个参数意义相同,传入一个对象,他作为函数的执行环境(实质上是为 ...

  9. oracle11g RAC添加节点

    OS: [root@rac ~]# more /etc/oracle-releaseOracle Linux Server release 5.7 DB: SQL> SELECT * FROM ...

  10. oracle 存储过程编辑 卡死

    一.可用SYS登录, 二.查锁session_ID查找存储过程OPERATIONDATA_IMP被哪些session锁住而无法编译select * FROM dba_ddl_locks where n ...