lightoj Leading and Trailing
2016-08-23 19:59
375 查看
Leading and Trailing
You are given two integers: n and k, your task is to find the most significant three digits, and least significant three digits of nk.
Each case starts with a line containing two integers: n (2 ≤ n < 231) and k (1 ≤ k ≤ 107).
#include<stdio.h>
#include<string.h>
#include<math.h>
typedef long long LL;
LL powermod(LL a,LL b,LL c)
{
LL ans=1;
a=a%c;
while(b>0)
{
if(b%2)
ans=ans*a%c;
b/=2;
a=a*a%c;
}
return ans;
}
int main()
{
int t,l;
LL n,k;
l=1;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld",&n,&k);
LL a=powermod(n,k,1000);
double s=1.0*k*log10(n*1.0);
double b=pow(10,s-int(s));
LL su=LL(100*b);
printf("Case %d: %lld %03lld\n",l++,su,a);
}
return 0;
}
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
Input
Input starts with an integer T (≤ 1000), denoting the number of test cases.Each case starts with a line containing two integers: n (2 ≤ n < 231) and k (1 ≤ k ≤ 107).
Output
For each case, print the case number and the three leading digits (most significant) and three trailing digits (least significant). You can assume that the input is given such that nk contains at least six digits.Sample Input | Output for Sample Input |
5 123456 1 123456 2 2 31 2 32 29 8751919 | Case 1: 123 456 Case 2: 152 936 Case 3: 214 648 Case 4: 429 296 Case 5: 665 669 |
#include<string.h>
#include<math.h>
typedef long long LL;
LL powermod(LL a,LL b,LL c)
{
LL ans=1;
a=a%c;
while(b>0)
{
if(b%2)
ans=ans*a%c;
b/=2;
a=a*a%c;
}
return ans;
}
int main()
{
int t,l;
LL n,k;
l=1;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld",&n,&k);
LL a=powermod(n,k,1000);
double s=1.0*k*log10(n*1.0);
double b=pow(10,s-int(s));
LL su=LL(100*b);
printf("Case %d: %lld %03lld\n",l++,su,a);
}
return 0;
}
相关文章推荐
- 解决crond引发大量sendmail、postdrop进程问题
- 【LightOJ】1282 - Leading and Trailing(快速幂 & 数论)
- LightOJ 1138 - Trailing Zeroes (III)【二分】
- Yarn中使用FairScheduler进行任务调度
- thread_线程间协作:wait、notify、notifyAll和Condition
- NIO和AIO
- 1282 - Leading and Trailing
- ORA-00054: 资源正忙, 但指定以 NOWAIT 方式获取资源, 或者超时失效
- 172. Factorial Trailing Zeroes -- 求n的阶乘末尾有几个0
- 图形绘制 Canvas Paint Path 详解
- ABAP的PAI和PBO中的字段传递顺序
- 219. Contains Duplicate II
- Cassandra研究报告-http://blog.csdn.net/zyz511919766/article/details/38683219/
- Vertica的这些事<九>—— 关于vertica的Connection Failover
- 数据流DataInputStream和内存流ByteArrayInputStream
- 从api.ai工作原理来看构建简单场景chatbot的一般方法
- 如何透过F5探测后端应用健康状态?
- 217. Contains Duplicate
- sendMail
- strong,weak,retain,assign,copy,nomatic的区别