Codeforces Beta Round #17

题目链接:点击我打开题目链接

大概题意:

给你 \(b\),\(n\),\(c\).

让你求:\((b)^{n-1}*(b-1)\%c\).

\(2<=b<=10^{10^6},1<=n<=10^{10^6},1<=c<=10^9\)

简明题解:

因为 \(b\) , \(n\)都太大了。关键是求 \((b)^{n-1}\%c\)

所以,我们可以利用欧拉函数 \(phi()\) 的性质。

对于\(a^{b} \% c\) 的形式,我们可以有:

当 \(a\),\(c\) 互质时有 \(a^{phi(c)} = 1( \mod c)\),

那么经过推导就有(有空写一下 \(Pre-knowledge\)):

\(a^b\%c=a^{(b\%phi(c))}\). (数论欧拉定理)

但是这个题上并没有说明 \(a\)与 \(c\) 互质。所以不能用这个方法。

所以正解是,我们可以学习一下广义欧拉定理(无互质要求),用这个来降幂: (广义欧拉定理):

\(a^b\%c≡a^{(b\%phi(c))\%c}\) \((b<phi(c))\)

\(a^b \%c= a^{(b\%phi(c)+phi(c))\%c}\) (\(b>=phi(c)\))

然后这题预处理一下 \(phi\)就可以解决了。

复杂度:大概是 \(sqrt(c) * log(c))+log(phi(c))\)

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1000100;
char b[N],n[N];
int phi(int x)
{
int res=x;
for(int i=2;i*i<=x;i++)if(x%i==0)
{
res=res/i*(i-1);
while(x%i==0)x/=i;
}
if(x>1)res=res/x*(x-1);
return res;
}
int q_pow(int a,int k,int mod)
{
int res=1;
while(k)
{
if(k&1)res=1LL*res*a%mod;
a=1LL*a*a%mod;
k>>=1;
}
return res%mod;
}
int cal(char *str,int mod)
{
int res=0;
for(int i=0;str[i];i++)
{
res=(10LL*res + str[i]-'0') % mod;
}
return res;
}
int main()
{
int c;
scanf("%s%s%d",b,n,&c);
if(c==1)
{
cout<<1<<endl;
exit(0);
}
int B=cal(b,c);
int res=(B + c - 1) % c;
int Phi=phi(c);
int t=0;
for(int i=0;n[i];i++)
{
t = min(1000000000LL,10LL * t + n[i]-'0');
} if(t - 1 < Phi)
{
res = 1LL * res * q_pow(B,t-1,c)%c;
}
else
{
res = 1LL * res * q_pow(B,cal(n,Phi) + Phi - 1,c)%c;
}
printf("%d\n",(res + c - 1)%c + 1);
return 0;
}

Codeforces Beta Round #17 D. Notepad (数论 + 广义欧拉定理降幂)的更多相关文章

  1. Codeforces Beta Round #17 D.Notepad 指数循环节

    D. Notepad time limit per test 2 seconds memory limit per test 64 megabytes input standard input out ...

  2. Codeforces Beta Round #17 C. Balance DP

    C. Balance 题目链接 http://codeforces.com/contest/17/problem/C 题面 Nick likes strings very much, he likes ...

  3. Codeforces Beta Round #17 A - Noldbach problem 暴力

    A - Noldbach problem 题面链接 http://codeforces.com/contest/17/problem/A 题面 Nick is interested in prime ...

  4. Codeforces Beta Round #17 A.素数相关

    A. Noldbach problem Nick is interested in prime numbers. Once he read about Goldbach problem. It sta ...

  5. Codeforces Beta Round #17 C. Balance (字符串计数 dp)

    C. Balance time limit per test 3 seconds memory limit per test 128 megabytes input standard input ou ...

  6. Codeforces Beta Round #13 C. Sequence (DP)

    题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...

  7. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  8. Codeforces Beta Round #62 题解【ABCD】

    Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...

  9. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

随机推荐

  1. QNX与Linux两家未来有望独霸车载电子操作系统

    车载电子操作系统是汽车智能化的核心,能够有效分配车机的硬件资源,对车内各种任务功能进行协同管理,并控制各项任务优先级别.常见的车载电子操作系统有:QNX.Linux(Android,AaliOS).W ...

  2. python数据处理技巧二

    python数据处理技巧二(掌控时间) 首先简单说下关于时间的介绍其中重点是时间戳的处理,时间戳是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00 ...

  3. 洛谷 P3040 [USACO12JAN]贝尔分享Bale Share

    P3040 [USACO12JAN]贝尔分享Bale Share 题目描述 Farmer John has just received a new shipment of N (1 <= N & ...

  4. 排序(1)---------选择排序(C语言实现)

    选择排序的基本思想: 选择排序(Selection sort)是一种简单直观的排序算法. 它的工作原理例如以下.首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素 ...

  5. vim基础学习之自动补全功能

    本章我们学习自动补全功能1.自动补全优先从当前的编辑区获得补全列表例如:我们写下如下内容 aaaaa aabbb aaab 当我们再次输入aa,然后我们按下Tab的时候,会弹出一个包含 aaaaa a ...

  6. FileStream vs/differences StreamWriter?

    https://stackoverflow.com/questions/4963667/filestream-vs-differences-streamwriter What is different ...

  7. POJ 2430 状压DP

    题意: 思路: 先预处理出所有格子的statement statement=1–>只有上边的格子被覆盖 statement=2–>只有下边的格子被覆盖 statement=3–>上下 ...

  8. 下载安装与配置Excel 2013数据挖掘加载项(SQL Server 2012 SP1 + SQLServer2012_DMAddin.msi)

    一.系统要求 在安装这个数据挖掘加载项前,你的机器必须要可以支持如下的相关系统设备: (1)操作系统:windows 7 和  windows 8,暂时不支持 windows 10. (2)excel ...

  9. django第三方库

    1. django_celery_beat 作用:网页端配置定时任务 注意:1,需要迁移表格 2.需要注册app python3 manage.py makemigrations python3 ma ...

  10. 通过Gradle来下载依赖的jar包

    前段时间在看Gradle文章方向其比ANT.Maven真的要简单很多,特别是在下载jar依赖方面下面就是用gradle写的一个jar包依赖下载的小例子: (1)建立build.gradle文件 (2) ...