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 ...
随机推荐
- GraphQL介绍&使用nestjs构建GraphQL查询服务
GraphQL介绍&使用nestjs构建GraphQL查询服务(文章底部附demo地址) GraphQL一种用为你 API 而生的查询语言.出自于Facebook,GraphQL非常易懂,直接 ...
- nuxt.js踩坑之 - SSR 与 CSR 显示不一致问题
[Vue warn]: The client-side rendered virtual DOM tree is not matching server-rendered content. This ...
- js 数组常用的一些方法
数组可以说是js经常会遇到的数据结构,以下我们对数组进行详细的学习! 一.数组的创建 var mycars = new Array(): || new Array(3); || new Array( ...
- groovy函数、字符串、循环
三重单引号字符串 '''a triple single quoted string''' 三重单引号字符串是普通的java.lang.String 三重单引号字符串是多行的.您可以跨越行边界跨越字符串 ...
- 文件上传(Servlet/Struts2/SpringMVC)
文件下载(Servlet/Struts2)的链接:http://www.cnblogs.com/ghq120/p/8328093.html 文件上传 Servlet实现 要实现文件上传的功能,必须在f ...
- qt5.6.3下使用firebird
有人把firebird比作数据库界的瑞士军刀,想学习一下其在QT5.6中的使用,于是便开始了一场自己挖坑,自己埋的旅程. 环境说明:win7 64位+QT5.6 mingw4.9 32位(好像官网上也 ...
- 001profile条件化创建bean
01.类级别条件创建 @Configuration @Profile("dev") public class Aclass{}---->影响整个类,包括类的注解.开发环境,类 ...
- vue2.0 学习 ,开始学习
先看官网的介绍上面的教程 https://cn.vuejs.org/v2/guide/ 尝试 Vue.js 最简单的方法是使用 JSFiddle Hello World 例子.你可以在浏览器新标签 ...
- 编辑文本(EditText)
今天要给大家介绍的是简单的编辑文本框: 先看一下它的基本属性: 1.Activity public class EditTextActivity extends Activity { private ...
- Directly output the object name
package basic.java; public class Case { public static void main(String[] args) { Student s = new Stu ...