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;
}

转载于:https://www.cnblogs.com/LzyRapx/p/7738447.html

Codeforces Beta Round #17 D. Notepad (数论 + 广义欧拉定理降幂)相关推荐

  1. Codeforces Beta Round #17 C. Balance DP

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

  2. Codeforces Beta Round #22 (Div. 2 Only) E. Scheme(DFS+强连通)

    题目大意 给了 n(2<=n<=105) 个点,从每个点 u 出发连向了一个点 v(共 n 条边) 现在要求添加最少的边使得整个图是一个强连通图 做法分析 这道题千万不要一般化:先求强连通 ...

  3. Codeforces Beta Round #4 (Div. 2 Only)

    Codeforces Beta Round #4 (Div. 2 Only) A 水题 1 #include<bits/stdc++.h> 2 using namespace std; 3 ...

  4. Codeforces Beta Round #7

    Codeforces Beta Round #7 http://codeforces.com/contest/7 A 水题 1 #include<bits/stdc++.h> 2 usin ...

  5. Codeforces Beta Round #5 B. Center Alignment 模拟题

    B. Center Alignment 题目连接: http://www.codeforces.com/contest/5/problem/B Description Almost every tex ...

  6. Codeforces Beta Round #75 (Div. 1 Only) B. Queue 线段树。单点更新

    http://codeforces.com/problemset/problem/91/B 题意: 给你n个数,求得i 到n中小于a[i]的最右边的a[j],然后求a[i]到a[j]之间包含了多少个数 ...

  7. Codeforces Beta Round #51 D. Beautiful numbers 数位dp + 状态优化

    传送门 文章目录 题意: 思路: 题意: 思路: 数位dpdpdp挺经典的一个题辣,有一个很明显的状态就是f[pos][num][lcm]f[pos][num][lcm]f[pos][num][lcm ...

  8. codeforces beta round 1

    codeforces beta round 1 A Theatre Square in the capital city of Berland has a rectangular shape with ...

  9. Codeforces Beta Round #1 A,B,C

    A. Theatre Square time limit per test:1 second memory limit per test:256 megabytes input:standard in ...

最新文章

  1. iOS 依次执行 异步网络请求的一种实现
  2. 转发:Hekaton:SQL Server集成的内存事务处理
  3. Android实现炫酷的星空变幻效果
  4. ASP.NET Core 2.1 : 十五.图解路由(2.1 or earler)
  5. 【C#】C#对电子邮件的收发操作
  6. 双屏鼠标经常跑到副屏_1+1gt;2,让ipad变成你的电脑副屏
  7. 彻底弄懂C语言数组名
  8. The 2018 ACM-ICPC Asia Qingdao Regional Contest, Online
  9. python删除文件夹下文件夹_python删除指定文件夹下文件和文件夹的方法详解
  10. C# 根据url获取文件流流
  11. 凝思系统改时间_大众改原厂盲点监测系统,中山大众原厂改装,途观L改盲点监测...
  12. mysql限制用户只能访问指定数据库
  13. 调用远程摄像头进行人脸识别_工地如何实现安全帽检测/人脸识别?
  14. BERT模型—2.BERT模型预训练与微调
  15. 基于LASSO的波士顿房价分析
  16. android siri声波动画,Waver声波效果开源项目:和 Siri 一起学数学
  17. OpenWrt挂载U盘(NTFS)并在网上邻居共享访问
  18. 华为hcia-datacom 学习日记
  19. 水果店营销技巧与方法,水果店营销模式和技巧
  20. 为何某些文章的阅读量这么高?

热门文章

  1. 程序员应知——学习、思考与分享
  2. UltraTextEditor
  3. Linux进程间通信中的文件和文件锁
  4. Oracle的PL/SQL编程前奏之基础技能实战一(匿名子程序)
  5. X window的思想和终端的重大意义
  6. 使用 Swagger 文档化和定义 RESTful API
  7. 刚才我提出要把数据库处理部分放到代码里,但是有人提出,存储过程有缓存,速度快。我该怎么说啊?...
  8. PowerShell2.0之桌面计算机维护(八)关闭或重启远程计算机
  9. GVRP和VTP比较
  10. 网络存储导论第七章:重要系统灾备方法