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. Java中的类型转换(Integer、Long、String)

    这段时间将项目中一个模块参照C++源代码,实现一个JAVA版.主要功能是将一些字段信息转换为String类型,传输后可以进行解析. Integer.Long转为String,Java本身提供了这种转换 ...

  2. 在web中使用HTTPS

    背景 目前网上流行的是HTTP协议,HTTPS协议还在逐步推广的过程中. HTTP协议以明文发送内容,容易被攻击者窃听.HTTPS在HTTP的基础上加入了SSL协议,SSL依靠证书来验证服务器的身份[ ...

  3. Java课堂作业01

    题目:编写一个程序,此程序从命令行接收多个数字,求和之后输出结果. 设计思想:用for循环将string型转换为int型,再用sum求和,使其一直相加,到达最大长度,sum即为所求sum. 程序流程图 ...

  4. Java历程-初学篇 Day05选择结构(2)

    一,switch 由于本作者学的是jdk6.0版本,我知道7.0可以使用字符串,但是我就不改了 语法: switch(char类型/int类型){ case 值: //输出 break; ... de ...

  5. 我的第一个python web开发框架(6)——第一个Hello World

    小白中午听完老菜讲的那些话后一直在思考,可想来想去还是一头雾水,晕晕呼呼的一知半解,到最后还是想不明白,心想:老大讲的太高深了,只能听懂一半半,看来只能先记下来,将明白的先做,不明白的等以后遇到再学. ...

  6. 第六章 JDBC

    第一章 JDBC 一.JDBC的简介 1.什么是JDBC JDBC是java数据库连接(java database connectivity)技术的简称,它充当了java应用程序与各个不同数据库之间进 ...

  7. iOS中单例需要注意的

    单例模式怎么定义的,可能在不同的语言,不同的书中不完全一样,但是概况开来都应该是:一个类有且仅有一个实例,并且自行实例化向整个系统提供. 因此,首先你可能需要确定你是真的需要一个单例类,还是说仅仅是需 ...

  8. Extjs6(五)——写一个包含toolbar、form、grid的子页面

    本文基于ext-6.0.0 这个页面布局是很多管理系统的常用布局,具体如下图: 一.页面主体personalInfo.js 整个页面采用border布局,分成三部分,这个personalInfo.js ...

  9. Web安全学习笔记(一)

    Web安全学习笔记(一): URL协议 HTTP协议 1. URL 2. HTTP协议 什么是HTTP HTTP的工作原理 HTTP报文 什么是Cookies HTTP请求方式 Referer请求的功 ...

  10. 见到过的MOS管的一些参数

    选择MOS管的时候需要注意的几个参数1,VDSS(击穿电压):此电压要选择合适,一般是加入的电压值的峰值的两倍.2,VGS(th)(开启电压):3,RDS(on)(漏源电阻):这个值要尽可能的小,因为 ...