CodeForces 17D Notepad(同余定理)
2 seconds
64 megabytes
standard input
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.
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.
In the only line output the amount of numbers written on the same page as the last number.
2 3 3
1
2 3 4
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(同余定理)的更多相关文章
- codeforces 17D Notepad
codeforces 17D Notepad 题意 题解 TBD 更新模板(phi.欧拉降幂) 代码 #include<bits/stdc++.h> using namespace std ...
- 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 ...
- 51nod 1433 0和5【数论/九余定理】
1433 0和5 题目来源: CodeForces 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 难度:2级算法题 收藏 关注 小K手中有n张牌,每张牌上有一个一位数的数,这个 ...
- Light oj 1214-Large Division (同余定理)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1214 题意很好懂,同余定理的运用,要是A数被B数整除,那么A%B等于0.而A很大,那我 ...
- 如何运用同余定理求余数【hdoj 1212 Big Number【大数求余数】】
Big Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- hdu 4704 同余定理+普通快速幂
此题往后推几步就可找到规律,从1开始,答案分别是1,2,4,8,16.... 这样就可以知道,题目的目的是求2^n%Mod的结果.....此时想,应该会想到快速幂...然后接着会发现,由于n的值过大, ...
- OJ随笔——【1088-N!】——同余定理
题目如下: Description 请求N!(N<=10000),输出结果对10007取余输入每行一个整数n,遇到-1结束.输出每行一个整数,为对应n的运算结果. Sample Input ...
- [ACM] POJ 2635 The Embarrassed Cryptographer (同余定理,素数打表)
The Embarrassed Cryptographer Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11978 A ...
- 算法训练 K好数 数位DP+同余定理
思路:d(i,j)表示以i开头,长度为j的K好数的个数,转移方程就是 for(int u = 0; u < k; ++u) { int x = abs(i - u); if(x == 1) co ...
随机推荐
- C++ Primer学习笔记(二)
题外话:一工作起来就没有大段的时间学习了,如何充分利用碎片时间是个好问题. 接 C++ Primer学习笔记(一) 27.与 vector 类型相比,数组的显著缺陷在于:数组的长度是固定的,无法 ...
- 在 Mac 上搭建 Nginx PHP Mysql 开发环境
事实上这个过程跟Linux下安装都几乎相同,仅仅是部分命令有区别,大同小异. 网上看到非常多教程都是用 brew 之类的包管理器安装,可是 Mac 自带了 php , 难道还要再装一个第三方的?强迫症 ...
- 【Java面试题】16 静态代码块,main方法,构造代码块,构造方法
public class HelloB extends HelloA{ public HelloB() { System.out.println("HelloB"); } { Sy ...
- 获取用户真实的IP
在实际项目很使用的函数,果断收集了 function get_client_ip() { if (getenv("HTTP_CLIENT_IP") && str ...
- 不定义JQuery插件 不要说会JQuery
二.普及JQuery知识 知识1:用JQuery写插件时,最核心的方法有如下两个: $.extend(object) 可以理解为JQuery 添加一个静态方法. $.fn.extend(object) ...
- C# Smtp方式发送邮件
//简单邮件传输协议类 System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(); ...
- 一、NHibernate配置所支持的属性
属性名 用途 dialect 设置NHibernate的Dialect类名 - 允许NHibernate针对特定的关系数据库生成优化的SQL 可用值: full.classname.of.Dialec ...
- SQLServer------存储过程的使用
转载: http://www.cnblogs.com/hoojo/archive/2011/07/19/2110862.html 例子: 1.学生表 CREATE TABLE [dbo].[Stude ...
- 墨卡托投影, GPS 坐标转像素, GPS 坐标转距离
Before: 1. 研究的需要, 在 google map 上爬取了一些的静态卫星地图图片,每张图片的像素为 256*256 2. 通过 photshop 将这些地图碎片手动拼成了地图, 地图只是覆 ...
- 使用Jquery做分页效果
之前写过一个PHP 的分页效果,但是今天小伙伴和我说了一个不适用后台单纯用前段的JS来写分页,整理了一下,代码如下: html: <div id="containet"> ...