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))\)

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int N=1000100;
  5. char b[N],n[N];
  6. int phi(int x)
  7. {
  8. int res=x;
  9. for(int i=2;i*i<=x;i++)if(x%i==0)
  10. {
  11. res=res/i*(i-1);
  12. while(x%i==0)x/=i;
  13. }
  14. if(x>1)res=res/x*(x-1);
  15. return res;
  16. }
  17. int q_pow(int a,int k,int mod)
  18. {
  19. int res=1;
  20. while(k)
  21. {
  22. if(k&1)res=1LL*res*a%mod;
  23. a=1LL*a*a%mod;
  24. k>>=1;
  25. }
  26. return res%mod;
  27. }
  28. int cal(char *str,int mod)
  29. {
  30. int res=0;
  31. for(int i=0;str[i];i++)
  32. {
  33. res=(10LL*res + str[i]-'0') % mod;
  34. }
  35. return res;
  36. }
  37. int main()
  38. {
  39. int c;
  40. scanf("%s%s%d",b,n,&c);
  41. if(c==1)
  42. {
  43. cout<<1<<endl;
  44. exit(0);
  45. }
  46. int B=cal(b,c);
  47. int res=(B + c - 1) % c;
  48. int Phi=phi(c);
  49. int t=0;
  50. for(int i=0;n[i];i++)
  51. {
  52. t = min(1000000000LL,10LL * t + n[i]-'0');
  53. }
  54. if(t - 1 < Phi)
  55. {
  56. res = 1LL * res * q_pow(B,t-1,c)%c;
  57. }
  58. else
  59. {
  60. res = 1LL * res * q_pow(B,cal(n,Phi) + Phi - 1,c)%c;
  61. }
  62. printf("%d\n",(res + c - 1)%c + 1);
  63. return 0;
  64. }

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. OpenJDK源码研究笔记(十二):JDBC中的元数据,数据库元数据(DatabaseMetaData),参数元数据(ParameterMetaData),结果集元数据(ResultSetMetaDa

    元数据最本质.最抽象的定义为:data about data (关于数据的数据).它是一种广泛存在的现象,在许多领域有其具体的定义和应用. JDBC中的元数据,有数据库元数据(DatabaseMeta ...

  2. CSUOJ 1603 Scheduling the final examination

    1603: Scheduling the final examination Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 49  Solved: 1 ...

  3. Ubuntu中的解压缩文件的方式

    记录Ubuntu下各种压缩和解压方式: .tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) -- ...

  4. UI_搭建MVC

    新建RootViewController 继承于 UIViewController 新建RootView 继承于 UIView AppDelegate.m 中引入 #import "Root ...

  5. 全面了解Linux下Proc文件系统

    全面了解Linux下Proc文件系统   Proc是一个虚拟文件系统,在Linux系统中它被挂载于/proc目录之上.Proc有多个功能 ,这其中包括用户可以通过它访问内核信息或用于排错,这其中一个非 ...

  6. ssm框架的总结

    ssm对应的是spring+springmvc+mybatis, 一.spring,略. 二.spring mvc是spring提供的mvc模块, 从图中可以看出,springmvc的模块划分非常多, ...

  7. 51Nod 迷宫问题(最短路+权值)(模板)

    你来到一个迷宫前.该迷宫由若干个房间组成,每个房间都有一个得分,第一次进入这个房间,你就可以得到这个分数.还有若干双向道路连结这些房间,你沿着这些道路从一个房间走到另外一个房间需要一些时间.游戏规定了 ...

  8. golang 简单web服务

    1.golang print输入 package main import "fmt" func main() { fmt.Printf("Hello World!\n&q ...

  9. 谈谈Spine动画在产品中的应用

    笔者介绍:姜雪伟,IT公司技术合伙人.IT高级讲师,CSDN社区专家,特邀编辑,畅销书作者,国家专利发明人;已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D ...

  10. x264代码剖析(八):encode()函数之x264_encoder_close()函数

    x264代码剖析(八):encode()函数之x264_encoder_close()函数 encode()函数是x264的主干函数.主要包含x264_encoder_open()函数.x264_en ...