POJ2720 Last Digits
一道题又写了近两个点……
这道题直接暴力快速幂肯定会爆(别想高精),所以还是要用一点数学知识的~
有一个东西叫欧拉降幂公式,就是:
\(x ^ y \equiv x ^ {y \ \ mod \ \ \varphi(p) + \varphi(p)} (mod \ \ p)\)
然后对于那些爆了的答案,就可以用这个公式递归求解。
然而这样还是会\(TLE\),因此采用打表法:对于已经算过的答案\(f_{b, i}\),将这个数记录下来,从而减少运算复杂度。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("")
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 105;
inline ll read()
{
ll ans = 0;
char ch = getchar(), last = ' ';
while(!isdigit(ch)) last = ch, ch = getchar();
while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
if(last == '-') ans = -ans;
return ans;
}
inline void write(ll x)
{
if(x < 0) x = -x, putchar('-');
if(x >= 10) write(x / 10);
putchar(x % 10 + '0');
}
int b, n, m; //f[m] = b ^ f[m - 1]
ll f[maxn][maxn], a[10];
ll check(ll b, ll y)
{
if(y == -1) return -1;
ll ret = 1;
for(int i = 1; i <= y; ++i)
{
ret *= b;
if(ret > (ll)1e14) return -1;
}
return ret;
}
void init()
{
a[0] = 1;
for(int i = 1; i <= 7; ++i) a[i] = a[i - 1] * 10;
Mem(f, -1);
for(int i = 0; i < maxn; ++i) f[0][i] = 0, f[1][i] = 1;
for(int i = 2; i < maxn; ++i)
{
f[i][0] = 1;
for(int j = 1; j < maxn; ++j)
{
f[i][j] = check(i, f[i][j - 1]);
if(f[i][j] == -1) break;
}
}
}
ll quickpow(ll a, ll b, ll mod)
{
a %= mod;
ll ret = 1;
for(; b; b >>= 1, a = a * a % mod)
if(b & 1) ret = ret * a % mod;
return ret;
}
ll phi(int n)
{
ll ret = n;
for(int i = 2; i * i <= n; ++i)
{
if(n % i == 0)
{
ret = ret / i * (i - 1);
while(n % i == 0) n /= i;
}
}
if(n > 1) ret = ret / n * (n - 1);
return ret;
}
ll calc(int b, int m, int mod)
{
if(mod == 1) return 0;
if(!m) return 1;
if(f[b][m] != -1) return f[b][m] % mod;
int g = phi(mod);
return quickpow(b, calc(b, m - 1, g) + g, mod);
}
void print(ll x, int n)
{
if(!n) return;
print(x / 10, n - 1);
putchar(x % 10 + '0');
}
int main()
{
init();
while(scanf("%d", &b) && b)
{
m = read(); n = read();
if(f[b][m] == -1) f[b][m] = calc(b, m, a[7]);
print(f[b][m], n), enter;
}
return 0;
}
POJ2720 Last Digits的更多相关文章
- [LeetCode] Reconstruct Original Digits from English 从英文中重建数字
Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...
- [LeetCode] Remove K Digits 去掉K位数字
Given a non-negative integer num represented as a string, remove k digits from the number so that th ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Add Digits 加数字
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- LeetCode 258. Add Digits
Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...
- ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】
FZU 2105 Digits Count Time Limit:10000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- Revolving Digits[EXKMP]
Revolving Digits Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- 【LeetCode】Add Digits
Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...
- Add Digits, Maximum Depth of BinaryTree, Search for a Range, Single Number,Find the Difference
最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the ...
随机推荐
- ASP.Net 之委托事件
1.首先给一张图让大家了解什么是委托?它的优缺点是什么? 2.通过代码的运用更深入地了解委托事件(窗体应用程序) 1)下面我们先定义一个无参数的委托. //1.0 定义一个自定义的委托,此委托的签名是 ...
- 使用gunzip、tar、rar、(zip压缩和unzip解压缩)
---------------------20171119------------------------------ 解压gz后缀 使用gunzip filename.gz ------------ ...
- c# MVC模式学习笔记_数据验证
改变显示字段名称 设计字段规范 1.引用 using System.ComponentModel; using System.ComponentModel.DataAnnotations; 2.Dis ...
- sql语句中as的意思是什么?
as 一般用在两个地方,一个是query的时候,用来重新指定返回的column(列) 名字如:一个table 有个column叫 id, 我们的query是select id from table1. ...
- MySQL mysqldump数据导出基本操作
mysqldump mysqldump命令是mysql数据库中备份工具,用于将MySQL服务器中的数据库以标准的sql语言的方式导出,并保存到文件中. 选项 --all-databases, -A:导 ...
- 改bug后 fix bug 时,一个不错的修复描述模板
*问题原因:* 问题出现的原因.*解决方法:* 问题的解决方案.*影响分支:* 影响哪些分支. *相关修改:* 具体的修改文件列表.*自测结果:* 自行测试了哪些用例,将大概步骤描述出来. *影响功能 ...
- 强化学习系列之:Deep Q Network (DQN)
文章目录 [隐藏] 1. 强化学习和深度学习结合 2. Deep Q Network (DQN) 算法 3. 后续发展 3.1 Double DQN 3.2 Prioritized Replay 3. ...
- weex 数据绑定,动态控制组件的显示内容及样式
无论的原生开发还是weex开发,经常会需要我们对一些组件/控件动态赋值,在原生中,我们大家都知道,对控件setText就可以了,那么在weex中呢,我们需要怎么做呢,其实很简单,几行代码就可以搞定!首 ...
- 客户端和服务端如何使用Token和Session
一.我们先解释一下他的含义: 1.Token的引入:Token是在客户端频繁向服务端请求数据,服务端频繁的去数据库查询用户名和密码并进行对比,判断用户名和密码正确与否,并作出相应提示,在这样的背 ...
- Java学习---Map的学习
1. Map 1.1. map中的方法 1.2. Map.Entry 对于集合来讲,就是把kye-value的数据保存在了Map.Entry的实例之后,再在Map集合中插入了一个Map.Entry的实 ...