嘟嘟嘟

一道题又写了近两个点……

这道题直接暴力快速幂肯定会爆(别想高精),所以还是要用一点数学知识的~

有一个东西叫欧拉降幂公式,就是:

     \(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的更多相关文章

  1. [LeetCode] Reconstruct Original Digits from English 从英文中重建数字

    Given a non-empty string containing an out-of-order English representation of digits 0-9, output the ...

  2. [LeetCode] Remove K Digits 去掉K位数字

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  3. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  4. [LeetCode] Add Digits 加数字

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

  5. LeetCode 258. Add Digits

    Problem: Given a non-negative integer num, repeatedly add all its digits until the result has only o ...

  6. ACM: FZU 2105 Digits Count - 位运算的线段树【黑科技福利】

     FZU 2105  Digits Count Time Limit:10000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  7. Revolving Digits[EXKMP]

    Revolving Digits Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  8. 【LeetCode】Add Digits

    Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  9. 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 ...

随机推荐

  1. Java - 枚举与注解

    Enumeration 于Java 1.5增加的enum type... enum type是由一组固定的常量组成的类型,比如四个季节.扑克花色. 在出现enum type之前,通常用一组int常量表 ...

  2. git pull和git pull --rebase的使用

    使用下面的关系区别这两个操作: git pull = git fetch + git merge git pull --rebase = git fetch + git rebase 现在来看看git ...

  3. javaweb浏览器url上项目名称的更改

    如何改变上面url项目名称地址,如把04去掉改成teachershare 如下图:在项目设置中设置web context-root属性,将04去掉. 就可以用这个登录了. 要注意的是如果之前已经部署在 ...

  4. eclipse 更改背景颜色字体

    原文 切一个自己的图: 废话不说,直接入题. 方式一:替换Eclipse的配置文件 其实Eclipse的各种配置都是在文件设置里的,因此只要用一个配置好的模版来替换默认的配置文件,即可将所有配置克隆到 ...

  5. Struts 类型转换之局部和全局配置

    我们碰到过很多情况,就是时间日期常常会出现错误,这是我们最头疼的事,在struts2中有一些内置转换器,也有一些需要我们自己配置. 我们为什么需要类型转换呢? 在基于HTTP协议的Web应用中 客户端 ...

  6. springcloud 实战 feign使用中遇到的相关问题

    springcloud 实战 feign使用中遇到的相关问题 1.使用feign客户端调用其他微服务时,session没有传递成功,sessionId不一样. /** * @author xbchen ...

  7. Linux 安装命令

  8. MySQL数据库(8)----表的创建、删除、索引和更改

    MySQL允许使用 CREATE TABLE 语句和 DROP TABLE 语句来创建.删除表,使用 ALTER TABLE 语句更改其结构.CREATE INDEX 语句和 DROP INDEX 语 ...

  9. BIEE总结

    一,数据仓库,BI涉及到的相关概念  1.DW:    即数据仓库(Data Warehouse),是一个面向主题的(Subject Oriented).集成的(Integrated).相对稳定的(N ...

  10. d3js data joins深入理解

    Data joins 给定一个数据数组和一个 D3 selection  我们就可以attach或者说是'join'数组中的每个数据到selection中的每个元素上. 这将使得我们的数据和可视化元素 ...