Codeforces Beta Round #17:Notepad 【欧拉广义降幂】

题目:

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 < 10^{10^6}, 1 ≤ n < 10^{10^6}, 1 ≤ c ≤ 10^9). 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.

题意:

给你一个b进制,问b进制位有n位的不同数有多少个,把这些数写在纸上,最后一页写了多少个?

题解:

b进制每位只能是 0 ~ b-1,最高为不能为0,所以最高位只有b-1中选择,其他位都有b种选择,所以得出公式:

(b-1)b^{n-1}(mod~c),余数为0,则答案为c

AC代码:

#include<iostream>
#include<string.h>
#include<cstdio>
using namespace std;
typedef long long ll;
const int MAXN = 1e6+66;
char b[MAXN],n[MAXN];
ll Eular(ll c)              //欧拉函数值phi()
{
    ll phi = c;
    for(int i=2;i*i<=c;++i)
    {
        if(c%i==0)
        {
            phi = phi - phi/i;
            while(c%i==0)
                c = c/i;
        }
    }
    if(c>1) phi = phi - phi/c;
    return phi;
}
ll quick_pow(ll a,ll x,ll mod) //快速幂
{
    ll ans = 1;
    while(x)
    {
        if(x&1) ans = ans*a % mod;
        a = a*a % mod;
        x = x>>1;
    }
    return ans % mod;
}
ll cal(char *s,ll mod)    //取模得到b值
{
    ll res = 0;
    for(int i=0;i<strlen(s);++i)
        res = (s[i]-'0'+res*10) % mod;
    return res;
}
int main()
{
    int c;
    scanf("%s %s %d",b,n,&c);
    ll bb = cal(b,c);
    ll phi = Eular(c);
    bool vis = false;    //标记n-1是否大于phi(c)
    ll nn = 0;
    for(int i=0;i<strlen(n);++i) //计算n值
    {
        nn = n[i]-'0'+ nn*10;
        if(nn>=phi+1)
        {
            vis = true;
            nn = nn % phi;
        }
    }
    if(vis) nn += phi; //欧拉广义降幂
    ll ans = (quick_pow(bb,nn-1,c)*(bb-1)+c)%c;
    if(ans) cout<<ans;
    else cout<<c;
    return 0;

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值