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. zip---解压缩文件

    zip命令可以用来解压缩文件,或者对文件进行打包操作.zip是个使用广泛的压缩程序,文件经它压缩后会另外产生具有“.zip”扩展名的压缩文件. 语法 zip(选项)(参数) 选项 -A:调整可执行的自 ...

  2. mysql not in用法

    select * from zan where uid not in(select uid from zan where zhongjiang !=0) group by uid order by r ...

  3. HRBUST 1818 石子合并问题--直线版

    石子合并问题--直线版 Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HRBUST. Original ...

  4. 64。node.js 中间件express-session使用详解

    转自:http://jinjiakarl.com/2018/06/09/node-js-%E4%B8%AD%E9%97%B4%E4%BB%B6express-session%E4%BD%BF%E7%9 ...

  5. c# 装箱与拆箱的概念

    1装箱 就是将值类型的数据赋值给引用类型的实例中 比如 int类型的123赋值给Object o int i=123; Object o=(Object) i; 2拆箱 就是从引用类型的数据中提取数据 ...

  6. api h5 sdk 接入的说明

    api 接入 :纯后台接入没有页面 h5接入: 有页面的接入  需要和客户交互 客户输入内容 sdk 接入:需要对方的sdk文件 植入文件

  7. Dubbo学习总结(3)——Dubbo-Admin管理平台和Zookeeper注册中心的搭建

    ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用提供一致性服务的软件,提供的功 ...

  8. 关于Blocking IO,non-Blokcing IO,async IO的区别和理解

    来源:http://shmilyaw-hotmail-com.iteye.com/blog/1896683 概括来说,一个IO操作可以分为两个部分:发出请求.结果完成.如果从发出请求到结果返回,一直B ...

  9. [Anuglar & NgRx] StoreRouterConnectingModule

    Always treat Router as the source of truth When we use Ngrx, we can see that we will use a "Sto ...

  10. php课程 12-39 继承中parent的作用是什么

    php课程 12-39 继承中parent的作用是什么 一.总结 一句话总结:PHP5中使用parent::来引用父类的方法.parent:: 可用于调用父类中定义的成员方法. parent::的追溯 ...