CF 17D - Notepad(数论取模+欧拉函数)

博客探讨了非十进制数系统中的数论概念,特别是针对CF 17D问题,该问题涉及在特定基数b下长度为n的数字序列,且不包含前导零。每个页面可以容纳c个数字。通过使用数论取模和欧拉函数,可以计算出最后一个页面将写入多少个数字。输入参数包括基数b、数字长度n和每页容量c。输出是最后一页上数字的数量。示例解释了如何在二进制系统中计算具有4个长度为3的数字,并展示了如何在不同情况下分布这些数字到页面上。
摘要由CSDN通过智能技术生成

D - Notepad
Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

Description

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

<p< p="">

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

<p< p="">

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

Sample Input

Input
2 3 3
Output
1
Input
2 3 4
Output
4

Hint

<p< p="">

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.


思路: ab%c=a*10%c+b%c;


#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;
const int mm=3e6+7;
char b[mm],s[mm];
long long c;
long long mod(long long a,long long b)///a^b%c
{ long long z=1;
  while(b)
  {
    if(b&1)
      z=(z*a)%c;
    a=(a*a)%c;
    b/=2;
  }
  return z;
}
long long phi(long long n)///欧拉函数与n互质的个数
{
    long long rea=n;
    for(int i=2;i*i<=n;i++)
    if(n%i==0)
    {
        rea=rea-rea/i;
        do
        n/=i;
        while(n%i==0);
    }
    if(n>1)
    rea=rea-rea/n;
    return rea;
}
int main()
{  long long bb,nn,lb,ls;
  while(cin>>b>>s>>c)
  {
    bb=0;nn=0;
    lb=strlen(b);ls=strlen(s);
    int zz=phi(c);
    for(int i=0;i<lb;i++)
    {
      bb=(bb*10+b[i]-'0')%c;
    }
    bool yes=0;
    for(int i=0;i<ls;i++)
    {  nn=nn*10+s[i]-'0';
      if(nn>zz)
        {yes=1;nn=nn%zz;
        }
    }
    if(yes)
      nn+=zz;
    --nn;
    long long z=0;
    if(bb==0)bb=c;
    z=mod(bb,nn);
    z=(((bb+c-1)%c)*z)%c;
    if(z!=0)
    cout<<z<<"\n";
    else cout<<c<<"\n";
  }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值