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 ...
随机推荐
- 【转】MFC CListCtrl 使用技巧
以下未经说明,listctrl默认view 风格为report 相关类及处理函数 MFC:CListCtrl类 SDK:以 “ListView_”开头的一些宏.如 ListView_InsertCol ...
- asp.net 后端验证
using EntryRegistration.Filters; using EntryRegistration.Models.Entity; using System; using System.C ...
- html5简单拖拽实现自动左右贴边+幸运大转盘
此篇文章主要实现两个功能: 1.点击屏幕下方签到悬浮按钮: 2.弹出幸运大转盘,转盘抽奖签到 效果如图: 在网上找了很多移动端拖拽的js实现方式,大部分都是这一种,html5的touch事件,但是 ...
- 制作一款3D炸弹超人游戏
说起炸弹超人,相信很多朋友都玩过类似的游戏,其中最为人熟知的莫过于<泡泡堂>.该类型游戏需要玩家在地图中一边跑动一边放置炸弹,同时还要躲避敌方炸弹保护自己.最初的炸弹超人游戏都是2D的,今 ...
- Unity获取指定资源目录下的所有文件
使用前需要引入System.IO;这个命名空间 public void GetFiles() { //路径 //string path = string.Format("{0}", ...
- 简单易懂的snmpd.conf配置文件说明
转自http://blog.chinaunix.net/u2/61187/showart_689604.html 用 snmp+mrtg 可以很好的实现对局域网内服务器状态的监控. 现在就以 ...
- nohub和重定向文件
1.如果使用远程连接的Linux的方式并想后台运行执行如下命令: 格式:nohup <程序名> & 比如:nohup /usr/local/collection/bin/start ...
- complex()
complex() 用于将一个对象转换为复数 In [1]: complex(123) # 将整数转换为复数 Out[1]: (123+0j) In [2]: complex(') # 将纯数字的字符 ...
- ios Instruments 内存泄露
本文转载至 http://my.oschina.net/sunqichao/blog?disp=2&p=3 虽然iOS 5.0版本之后加入了ARC机制,由于相互引用关系比较复杂时,内存泄露还是 ...
- Mybatis中的foreach
<delete id="deleteByParam"> DELETE FROM YZ_SECURITIES_CURRENCY WHERE ID IN <forea ...