hdu 4335 What is N?
此题用到的公式: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?的更多相关文章
- 数论 + 公式 - 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满 ...
- HDU 4335 Contest 4
利用降幂公式..呃,还是自己去搜题解吧.知道降幂公式后,就不难了. #include <iostream> #include <cstdio> #include <alg ...
- HDU 4335 What is N?(指数循环节)题解
题意: 询问有多少数\(n\)满足\(n^{n!}\equiv b\mod p \land\ n\in[1,M]\),数据范围:\(M\leq2^{64}-1,p\leq1e5\) 思路: 这题显然要 ...
- HDU Today(最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=2112 HDU Today Time Limit: 15000/5000 MS (Java/Others) ...
- HDOJ 2111. Saving HDU 贪心 结构体排序
Saving HDU Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【HDU 3037】Saving Beans Lucas定理模板
http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...
- hdu 4859 海岸线 Bestcoder Round 1
http://acm.hdu.edu.cn/showproblem.php?pid=4859 题目大意: 在一个矩形周围都是海,这个矩形中有陆地,深海和浅海.浅海是可以填成陆地的. 求最多有多少条方格 ...
- HDU 4569 Special equations(取模)
Special equations Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u S ...
- HDU 4006The kth great number(K大数 +小顶堆)
The kth great number Time Limit:1000MS Memory Limit:65768KB 64bit IO Format:%I64d & %I64 ...
随机推荐
- SNRO:Number Range
业务对象是在一定的编号范围内分配编号的,编号既可以是内部分配也可以是外部分配.对于外部分配,用户输入编号,系统检查这个编号是否被占用.对于内部分配,系统会自动的把编号分配给业务对象.所以内部分配和外部 ...
- Nginx +keepalived
Nginx +keepalived 配置高可用的Nginx 准备环境: 节点node17,node18 lftp 172.16.0.1:/pub/Sources/6.x86_64/nginx ...
- 启语外语培训网SEO历程
网站开发完成已经好长时间了,但收录一直上不去,排名也上不去.我这篇文章主要是分析一下网站的SEO历程 网址:http://waiyupx.cheer-edu.net/School/School-5.h ...
- C# 解析XML格式的字符串
public CreateOrderReturnResult GetCreateOrderReturnApi() { var result = new CreateOrderReturnResult( ...
- C++ string的常用功能
头文件为#include<string> string str,str1; char s[]; str.length和str.size()是一样的功能都是返回当前字符串的大小: str.e ...
- 基于PinnedSectionListView实现联系人通讯录并且点击打电话
PinnedSectionListView具体下载地址.使用方法和注意事项:http://www.cnblogs.com/zzw1994/p/4997601.html 怎么根据联系人姓名首字符顺序读取 ...
- PySide 简易教程<三>-------动手写起来
到目前为止,已经接触的Pyside的界面元素有如下几个:QWidget.QPushButton.QLabel.本次再介绍两个tooltip和messagebox.tooltip是一个鼠标悬浮提示信息, ...
- 如何参与linux 内核开发
如果想评论或更新本文的内容,请直接联系原文档的维护者.如果你使用英文 交流有困难的话,也可以向中文版维护者求助.如果本翻译更新不及时或者翻 译存在问题,请联系中文版维护者. 英文版维护者: Gre ...
- java排序集锦
java实现排序的一些方法,来自:http://www.javaeye.com/topic/548520 package sort; import java.util.Random; /** * 排序 ...
- 【iOS】屏幕旋转,屏幕自适应方向变化
1. iOS有四个方向的旋转,为了保证自己的代码能够支持旋转,我们必须首先处理一个函数: - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInter ...