codeforces 17D. Notepad 欧拉函数降幂

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 < 101061 ≤ n < 101061 ≤ 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,数据范围 2 ≤ b < 101061 ≤ n < 101061 ≤ c ≤ 109


给出两个公式:

a^b%c=a^(b%phi(c)). 欧拉函数降幂,要求a与c互质
a^b %c= a^(b%phi(c)+phi(c)) %c (b>=phi(c),不大于时直接二分幂)   扩展的欧拉函数用于降幂,无互质要求


CODE:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e6+10;

LL c;
char b[N];  ///b
char s[N];  ///n

LL euler_phi(LL n){         ///计算单个phi函数
    LL m = (int)sqrt(n+0.5);
    LL ans = n;
    for(LL i = 2;i <= m;i++) if(n%i == 0){
        ans = ans/i*(i-1);
        while(n%i == 0) n /= i;
    }
    if(n > 1) ans = ans/n*(n-1);
    return ans;
}

LL Pow(LL a,LL b,LL mod){   ///二分幂
    LL ret = 1;
    LL A = a;
    while(b){
        if(b&1) ret = ret*A%mod;
        A = A*A%mod;
        b >>= 1;
    }
    return ret;
}

int main(void)
{
    scanf("%s%s%I64d",b,s,&c);
    int lenb = strlen(b);
    int lens = strlen(s);
    LL tb=0,tb1=0;    ///b和b-1
    for(int i = 0;i < lenb;i++){     ///计算b
        tb = (tb*10+b[i]-'0')%c;
    }
    tb1 = (tb-1+c)%c;  ///计算b-1
    LL phic = euler_phi(c);
    for(int i = lens-1;i >= 0;i--){  ///n-1
        if(s[i] == 0) s[i] = '9';
        else{
            s[i]--;
            break;
        }
    }
    bool flag = false; ///标记n是否大于等于phic
    LL tn = 0;         ///n
    for(int i = 0;i < lens;i++){     ///计算n
        if(tn >= phic) flag = true;
        if(flag) tn = (tn*10+s[i]-'0')%phic;
        else     tn = tn*10+s[i]-'0';
    }
    if(flag) tn += phic;             ///n大于等于phic的时候才加
    LL ans = Pow(tb,tn,c);
    ans = ans*tb1%c;
    if(ans == 0) printf("%I64d\n",c);
    else         printf("%I64d\n",ans);
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您提供的链接是Codeforces的一个问题,问题编号为104377。Codeforces是一个知名的在线编程竞赛平台,经常举办各种编程比赛和训练。Gym是Codeforces的一个扩展包,用于组织私人比赛和训练。您提供的链接指向了一个问题的页面,但具体的问题内容和描述无法通过链接获取。如果您有具体的问题或需要了解关于Codeforces Gym的更多信息,请提供更详细的信息。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [http://codeforces.com/gym/100623/attachments E题](https://blog.csdn.net/weixin_30820077/article/details/99723867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [http://codeforces.com/gym/100623/attachments H题](https://blog.csdn.net/weixin_38166726/article/details/99723856)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [CodeforcesPP:Codeforces扩展包](https://download.csdn.net/download/weixin_42101164/18409501)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值