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
The only input line contains three space-separated integers b, n 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.
Sample Input
2 3 3
1
2 3 4
4
Hint
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";
}
}
博客探讨了非十进制数系统中的数论概念,特别是针对CF 17D问题,该问题涉及在特定基数b下长度为n的数字序列,且不包含前导零。每个页面可以容纳c个数字。通过使用数论取模和欧拉函数,可以计算出最后一个页面将写入多少个数字。输入参数包括基数b、数字长度n和每页容量c。输出是最后一页上数字的数量。示例解释了如何在二进制系统中计算具有4个长度为3的数字,并展示了如何在不同情况下分布这些数字到页面上。
1246

被折叠的 条评论
为什么被折叠?



