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. Httpd 文件服务器的搭建

    服务器信息 系统: CentOS 安装操作 安装 httpd 直接通过 yum 安装: yum install httpd 安装完成之后,可以检查版本: http 查看版本 httpd -versio ...

  2. 今日SGU 5.23

    SGU 223 题意:给你n*n的矩形,放k个国王,每个国王不能放在别的国王的8连边上,问你有多少种方法 收获:状态DP,因为每行的放置只会影响下一行,然我们就枚举每行的状态和对应的下一行的状态,当两 ...

  3. 使用DbUtils实现增删改查——ResultSetHandler 接口的实现类

    在上一篇文章中<使用DbUtils实现增删改查>,发现运行runner.query()这行代码时.须要自己去处理查询到的结果集,比較麻烦.这行代码的原型是: public Object q ...

  4. Bmob移动后端云服务平台--Android从零開始--(一)何为Bmob

    Bmob移动后端云服务平台--Android从零開始--(一)何为Bmob 在正式的项目开发中,单client不能满足我们的需求,须要实现client与服务端的连接. 而在编写Android服务端代码 ...

  5. 3.常用Bracket插件

    转自:https://blog.csdn.net/iso_wsy/article/details/52608205 1.Emmet 如果你从事Web前端开发的话,对该插件一定不会陌生.它可以加快你的 ...

  6. 如何使iframe外部的超级链接的页面在iframe中打开

    如何使iframe外部的超级链接的页面在iframe中打开,有以下两种方法: 一.html方法: <iframe name="a1"></iframe> & ...

  7. K短路 spfa + A*

    #include <stdio.h> #include <string.h> #include <queue> #include <algorithm> ...

  8. shell 日期转换

    1.字符串转换为时间戳可以这样做: date -d "2010-10-18 00:00:00" +%s 输出形如: 1287331200 其中,-d参数表示显示指定的字符串所表示的 ...

  9. PHP 获取完整URL地址

    /** * 获取当前完整URL * @return string */ function get_url() { $sys_protocal = isset($_SERVER['SERVER_PORT ...

  10. nslookup---域名查询

    nslookup命令是常用域名查询工具,就是查DNS信息用的命令. nslookup4有两种工作模式,即“交互模式”和“非交互模式”. 在“交互模式”下,用户可以向域名服务器查询各类主机.域名的信息, ...