D. Notepad
time limit per test

2 seconds

memory limit per test

64 megabytes

input

standard input

output

standard output

Nick is attracted by everything unconventional. He doesn't like decimal number system any more, and he decided to study other number systems. A number system with base b caught his attention. Before he starts studying it, he wants to write in his notepad all the numbers of length n without leading zeros in this number system. Each page in Nick's notepad has enough space for c numbers exactly. Nick writes every suitable number only once, starting with the first clean page and leaving no clean spaces. Nick never writes number 0 as he has unpleasant memories about zero divide.

Would you help Nick find out how many numbers will be written on the last page.

Input

The only input line contains three space-separated integers bn and c (2 ≤ b < 10106, 1 ≤ n < 10106, 1 ≤ c ≤ 109). You may consider that Nick has infinite patience, endless amount of paper and representations of digits as characters. The numbers doesn't contain leading zeros.

Output

In the only line output the amount of numbers written on the same page as the last number.

Examples
input
2 3 3
output
1
input
2 3 4
output
4
Note

In both samples there are exactly 4 numbers of length 3 in binary number system. In the first sample Nick writes 3 numbers on the first page and 1 on the second page. In the second sample all the 4 numbers can be written on the first page.

题意:求[ (b-1) *(b)^(n-1)]%c;

思路:指数循环节;

#include<bits/stdc++.h>
using namespace std;
#define ll __int64
#define esp 0.00000000001
const int N=1e5+,M=1e6+,inf=1e9+;
const int mod=;
#define MAXN 10000010
ll quickpow(ll x,ll y,ll z)
{
ll ans=;
while(y)
{
if(y&)
ans*=x,ans%=z;
x*=x;
x%=z;
y>>=;
}
return ans;
}
ll phi(ll n)
{
ll i,rea=n;
for(i=;i*i<=n;i++)
{
if(n%i==)
{
rea=rea-rea/i;
while(n%i==) n/=i;
}
}
if(n>)
rea=rea-rea/n;
return rea;
}
char a[M];
char b[M];
int main()
{
ll x,y,z,i,t;
while(~scanf("%s%s%I64d",b,a,&z))
{
t=strlen(b);
ll x=;
for(i=;i<t;i++)
x=(x*+b[i]-'')%z;
if(x==)
x=z;
t=strlen(a);
ll p=phi(z);
ll ans=;
int flag=;
for(i=;i<t;i++)
{
ans=(ans*+a[i]-'');
if(ans>=p)
flag=;
ans%=p;
}
if(flag)
ans+=p;
ans--;
ll gg=(quickpow(x,ans,z)*(x-))%z;
if(gg)
printf("%I64d\n",gg);
else
printf("%I64d\n",z);
}
return ;
}

Codeforces Beta Round #17 D.Notepad 指数循环节的更多相关文章

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

    Codeforces Beta Round #17 题目链接:点击我打开题目链接 大概题意: 给你 \(b\),\(n\),\(c\). 让你求:\((b)^{n-1}*(b-1)\%c\). \(2 ...

  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. linux时间格式化

    echo `date +'[%Y-%m-%d %H:%M:%S]'`

  2. VMware虚拟机Bridged(桥接模式)

    转载于:https://www.linuxidc.com/Linux/2016-09/135521.htm   vmware为我们提供了三种网络工作模式,它们分别是:Bridged(桥接模式).NAT ...

  3. JavaServlet实现下载功能

        我们在项目中经常会用到下载功能,所以今天我们先说下下载功能实现的思路,然后通过一个案例代码来具体体现. 1.下载的思路: ①首先要获取我们要操作的文件对象的路径 ②然后使用获取的文件对象路径构 ...

  4. c# ,socket编程的一些常用方法

    1 命名空间 需要添加的命名空间 using System.Net; using System.Net.Socket; 2 构造新的socket对象 socket原型: public socket ( ...

  5. python创建进程的两种方式

    1.方式1 import time import multiprocessing def task(arg): time.sleep(2) print(arg) def run(): # 进程1 p1 ...

  6. 我的Android进阶之旅】GitHub 上排名前 100 的 Android 开源库进行简单的介绍

    GitHub Android Libraries Top 100 简介 本文转载于:https://github.com/Freelander/Android_Data/blob/master/And ...

  7. is和==的区别以及编码、解码

    一.is和==的区别 1,id( ) id( )是python的一个内置函数,通过id( )我们可以查看到一个变量表的值在内存中的地址: s1 = 2 print(id(s1)) # 15143680 ...

  8. 008-shiro与spring web项目整合【二】认证、授权、session管理

    一.认证 1.添加凭证匹配器 添加凭证匹配器实现md5加密校验. 修改applicationContext-shiro.xml: <!-- realm --> <bean id=&q ...

  9. 003-mysql查询表的数据大小

    在需要备份数据库里面的数据时,我们需要知道数据库占用了多少磁盘大小,可以通过一些sql语句查询到整个数据库的容量,也可以单独查看表所占容量. 1.查看数据库表结构大小,要查询表所占的容量,就是把表的数 ...

  10. Apache和Nigix

    Apache   ,   Nginx    是开源的HTTP服务器软件 HTTP服务器本质上也是一种应用程序--它通常运行在服务器之上,绑定服务器的IP地址并监听某一个tcp端口来接收并处理HTTP请 ...