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

题目的意思就是求 ((b-1)* b ^(n-1))%c
如果用java高精度加快速幂来求,肯定会爆炸,因为有一百万位。
这道题目完全可以不用快速幂,利用同余定理就可以了,当然用了快速幂会更快一些
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <queue> using namespace std;
#define MAX 1000000
char b[MAX+5];
char n[MAX+5];
long long int c;
int main()
{
while(scanf("%s%s%lld",b,n,&c)!=EOF)
{ long long int num=0;//b
long long int num2=0;//b-1
int len=strlen(b);
//b
for(int i=0;i<len;i++)
num=(b[i]-'0'+num*10)%c;
//b-1
for(int i=len-1;i>=0;i--)
if(b[i]!='0'){b[i]--;break;}
else b[i]='9';
for(int i=0;i<len;i++)
num2=(b[i]-'0'+num2*10)%c; int len2=strlen(n);
long long int ans=0;
long long int num3=num;
//n-1
for(int i=len2-1;i>=0;i--)
if(n[i]!='0'){n[i]--;break;}
else n[i]='9'; for(int j=0;j<len2;j++)
{
if(n[j]!='0')
{
if(j!=0)
{
long long int num4=num;
for(int k=1;k<=9;k++)
num=((num%c)*num4)%c;
}
int x=n[j]-'0';
if(j==0)
x--;
for(int p=1;p<=x;p++)
num=((num%c)*num3)%c;
}
else
{
if(j!=0)
{
long long int num4=num;
for(int k=1;k<=9;k++)
num=((num%c)*num4)%c;
}
else
num=1;
}
}
num=((num%c)*(num2%c))%c;
if(num==0)
num=c;
printf("%lld\n",num);
}
return 0;
}



CodeForces 17D Notepad(同余定理)的更多相关文章

  1. codeforces 17D Notepad

    codeforces 17D Notepad 题意 题解 TBD 更新模板(phi.欧拉降幂) 代码 #include<bits/stdc++.h> using namespace std ...

  2. Codeforces 17D Notepad 简单的数论

    从题意,anw =  (b-1)*b^(n-1)%c,强调,为了b^(n-1). 弱渣只能推了宣传. phi(c)为小于c且与c互质的个数. 当x >= phi(c)时:A^x = A(x%ph ...

  3. 51nod 1433 0和5【数论/九余定理】

    1433 0和5 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题  收藏  关注 小K手中有n张牌,每张牌上有一个一位数的数,这个 ...

  4. Light oj 1214-Large Division (同余定理)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1214 题意很好懂,同余定理的运用,要是A数被B数整除,那么A%B等于0.而A很大,那我 ...

  5. 如何运用同余定理求余数【hdoj 1212 Big Number【大数求余数】】

    Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  6. hdu 4704 同余定理+普通快速幂

    此题往后推几步就可找到规律,从1开始,答案分别是1,2,4,8,16.... 这样就可以知道,题目的目的是求2^n%Mod的结果.....此时想,应该会想到快速幂...然后接着会发现,由于n的值过大, ...

  7. OJ随笔——【1088-N!】——同余定理

    题目如下: Description 请求N!(N<=10000),输出结果对10007取余输入每行一个整数n,遇到-1结束.输出每行一个整数,为对应n的运算结果.   Sample Input ...

  8. [ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)

    The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11978   A ...

  9. 算法训练 K好数 数位DP+同余定理

    思路:d(i,j)表示以i开头,长度为j的K好数的个数,转移方程就是 for(int u = 0; u < k; ++u) { int x = abs(i - u); if(x == 1) co ...

随机推荐

  1. Java多例模式

    多例模式又划分为有上限多例模式和无上限多例模式两种,没上限的多例模式和直接 new 一个对象没什么差别,此处不做记录. 有上限多例模式:实际上是单例模式的推广,如果它的上限是1,那么就成了单例模式了. ...

  2. e672. 缩放,剪取,移动,翻转缓冲图像

    AffineTransform tx = new AffineTransform(); tx.scale(scalex, scaley); tx.shear(shiftx, shifty); tx.t ...

  3. EntityFramework定向加载实体

    Reference()和Collection() 方法 IList<Student> studList = context.Students.ToList<Student>() ...

  4. 做asp.net的在别人眼中都是渣渣吗?

    做asp.net的在别人眼中都是渣渣吗?

  5. Win10開始菜单打不开

    一.前言 自从用Win10之后(附上<我的Win10之旅>).用清理软件.总是深度清理,导致rt问题. 每次百度都是没用的解决方法: 今天,再一次清理(Wise Care 365 注冊表深 ...

  6. 利用jstack 找到异常代码

    1.top找出耗时pid进程或ps -ef |grep xxx 找出pid 2.ps p 3036 -L -o pcpu,pid,tid,time,tname,cmd  3036为pid 3.prin ...

  7. DOTween中的Time.Scale

    因为在做游戏暂停的时候通常会使用Time.Scale = 0 ,可是暂停的时候UI如果需要继续有动画怎么办呢?在DoTween中只需要设置         tweener.SetUpdate(true ...

  8. 左连接去重(objec)

    需求场景: 1.前端使用的object-table(angularJs) 2.自定义模糊查询 可以模糊查询主表,主表没有数据的时候,可通过字表的(name或者hostname)字段来查询(主-子:一对 ...

  9. linux系统中,tee命令的使用

    需求描述: 今天在看nginx内容的过程,遇到了tee这个命令,所以查询了下,在这里记录下使用方法. 操作过程: 1.执行以下的命令 [root@testvm ~]# uname -n | tee h ...

  10. oracle之trunc(sysdate)

    --截取后得到的仍为date数据类型 select trunc(sysdate) from dual;--2017-03-13 00:00:00select trunc(sysdate+1) from ...