Last Digits
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2233   Accepted: 474

Description

Exponentiation of one integer by another often produces very large results. In this problem, we will compute a function based on repeated exponentiation, but output only the last n digits of the result. Doing this efficiently requires careful thought about how to avoid computing the full answer.

Given integers b, n, and i, we define the function f(x) recursively by f(x) = bf(x-1) if x > 0, and f(0)=1. Your job is to efficiently compute the last n decimal digits of f(i). 

Input

The input consists of a number of test cases. Each test case starts with the integer b (1 <= b <= 100) called the base. On the next line is the integer i (1 <= i <= 100) called the iteration count. And finally, the last line contains the number n (1 <= n <= 7), which is the number of decimal digits to output. The input is terminated when b = 0.

Output

For each test case, print on one line the last n digits of f(i) for the base b specified. If the result has fewer than n digits, pad the result with zeroes on the left so that there are exactly n digits.

Sample Input

2
4
7
10
10
6
3
10
7
0

Sample Output

0065536
000000
4195387

Source

/*
* @Author: Lyucheng
* @Date: 2017-08-07 15:47:29
* @Last Modified by: Lyucheng
* @Last Modified time: 2017-08-07 20:10:43
*/
/*
题意:定义一个函数f(i)=b^(f(i-1)),给你b,i,n让你求f(i)的后n位,不足的用前导零补充 思路:后n位就是f(n)%(10^n) 问题:超时...打表
LL b,n,i;
LL mod;
char str[10];
LL pos;
char format[] = "%00d\n"; inline LL power(LL a,LL b,LL mod){//a的b次方
if(b==0) return 1;
LL cnt=power(a,b/2,mod);
cnt=cnt*cnt%mod;
if(b%2==1) cnt=cnt*a%mod;
return cnt;
} // return f(x)%mod
inline LL fun(LL b,LL x,LL mod){
if(x==0) return 1LL;//如果是0次,那么就是1
else{
LL res=power(b,fun(b,x-1,mod),mod);
if(res==0) res=mod;
return res;
}
}
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h> using namespace std; int a[][]={
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{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,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,},
{,,,,,,,,,,,,}, };
int b,i,n; int main(){
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(scanf("%d",&b)!=EOF&&b){
scanf("%d%d",&i,&n);
string s="";
int pos=a[b-][(i>?:i)];
for(int i=;i<n;i++){
s+=pos%+'';
pos/=;
}
while(n--){
cout<<s[n];
}cout<<endl;
}
return ;
}

poj 2720 Last Digits的更多相关文章

  1. poj 3373 Changing Digits (DFS + 记忆化剪枝+鸽巢原理思想)

    http://poj.org/problem?id=3373 Changing Digits Time Limit: 3000MS   Memory Limit: 65536K Total Submi ...

  2. POJ 3373 Changing Digits(DP)

    题目链接 记录路径的DP,看的别人的思路.自己写的也不好,时间居然2000+,中间的取余可以打个表,优化一下. 写的各种错,导致wa很多次,写了一下午,自己构造数据,终于发现了最后一个bug. dp[ ...

  3. POJ 3373 Changing Digits 好蛋疼的DP

    一開始写的高位往低位递推,发现这样有些时候保证不了第四条要求.于是又開始写高位往低位的记忆化搜索,又发现传參什么的蛋疼的要死.然后又发现高位開始的记忆化搜索就是从低位往高位的递推呀,遂过之. dp[i ...

  4. POJ 3373 Changing Digits 记忆化搜索

    这道题我是看了别人的题解才做出来的.题意和题解分析见原文http://blog.csdn.net/lyy289065406/article/details/6698787 这里写一下自己对题目的理解. ...

  5. POJ 3373 Changing Digits

    题目大意: 给出一个数n,求m,使得m的长度和n相等.能被k整除.有多个数符合条件输出与n在每位数字上改变次数最小的.改变次数同样的输出大小最小的.  共同拥有两种解法:DP解法,记忆化搜索的算法. ...

  6. POJ 3548 Restoring the digits

    暴力搜索.注意题目说每个字母对应的数字不同,这句话表明最多只有10个字母,所以暴力DFS绝对不会TLE. #include<cstdio> #include<cstring> ...

  7. ACM : POJ 2676 SudoKu DFS - 数独

    SudoKu Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu POJ 2676 Descr ...

  8. [ACM训练] 算法初级 之 搜索算法 之 广度优先算法BFS (POJ 3278+1426+3126+3087+3414)

    BFS算法与树的层次遍历很像,具有明显的层次性,一般都是使用队列来实现的!!! 常用步骤: 1.设置访问标记int visited[N],要覆盖所有的可能访问数据个数,这里设置成int而不是bool, ...

  9. POJ 2151 Check the difficulty of problems

    以前做过的题目了....补集+DP        Check the difficulty of problems Time Limit: 2000MS   Memory Limit: 65536K ...

随机推荐

  1. Day-1:初识开发板与基础知识

    买的这款51,ARM,AVR三合一的单片机,也不知道后面具体使用会不会有问题,先玩玩看吧. ------------------------------------------------------ ...

  2. [UIKit学习]06.懒加载,模型,自定义代码段,重写构造方法

    懒加载 在get中加载,且只加载一次 - (NSArray *)shops { if (_shops == nil) { NSString *file = [[NSBundle mainBundle] ...

  3. 系统学习java高并发系列二

    转载请注明原创出处,谢谢! 什么是线程? 线程是进程的一个实体,是CPU调度和分派的基本单位,它是比进程更小的能独立运行的基本单位.线程自己基本上不拥有系统资源,只拥有一点在运行中必不可少的资源(如程 ...

  4. PYTHON 函数局部变量和全局变量

    有这样一段PYTHON代码,从事C语言开发的人都知道,如果定义了全局变量,而函数内没有定义同名的函数变量的话,那么在函数内对该变量的赋值就是对全局变量空间数值的修改, 然后在PYTHON中却不尽相同, ...

  5. 语音传输之RTP/RTCP/UDP及软件实现关键点

    语音通信是实时通信,一定要保证实时性,不然用户体验会很糟糕.IETF设计了RTP来承载语音等实时性要求很高的数据,同时设计了RTCP来保证服务质量(RTP不保证服务质量).在传输层,一般选用UDP而不 ...

  6. SQLServer总结

    基础 nvarchar 和 varchar等的区别 1.nvarchar多了一个N,n表示使用的unicode编码,不用N开头的是用的utf-8编码. 2.所以中文在varchar中占两个字符长度,在 ...

  7. class DELPHICLASS TObject

    class DELPHICLASS TObject    1.自己猜想:delphi,是windows平台的快速应用程序开发工具Rapid Application Development 简称RAD. ...

  8. 关于 HashTable

    hashTable 的一些认识: 底层使用散列表,存贮键值对,键值非null 使用synchronize 保证线程安全 (线程安全) ■全局变量 //The hash table data. //底层 ...

  9. Easy sssp

    Easy sssp 时间限制: 1 Sec  内存限制: 128 MB提交: 103  解决: 20[提交][状态][讨论版] 题目描述 输入数据给出一个有N(2  < =  N  < = ...

  10. 前端基础之初识 HTML

    HTML HTML(Hypertext Markup Language)即超文本标记语言,是WWW的描述语言.设计HTML语言的目的是为了能把存放在一台电脑中的文本或图形与另一台电脑中的文本或图形方便 ...