LightOJ 1282 - Leading and Trailing (求n^k的前三位和后三位)
2016-04-04 18:45
351 查看
1282 - Leading and Trailing
You are given two integers: n and k, your taskis to find the most significant three digits, and least significant threedigits of
nk.
Each case starts with a line containing two integers: n (2≤ n < 231) and
k (1 ≤ k ≤ 107).
nk contains atleast six digits.
思路:后三位很好求,快速幂取模就好了,同样前三位我们也可以模拟快速幂来写,用double存储答案,模拟一下快速幂,中间记得转换就好了,注意输出为3位,如果后三位是022,那么必须输出022,不能输出22
ac代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 101000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
double change(double x)
{
while(x>=1000.0)
{
x/=10.0;
}
return x;
}
int main()
{
int t,i;
int cas=0;
scanf("%d",&t);
while(t--)
{
ll n,k;
scanf("%I64d%I64d",&n,&k);
int ans2=(int)powmod(n,k,1000);
double ans1=1.0,a=n*1.0;
while(k)
{
if(k%2)
{
ans1=ans1*a;
ans1=change(ans1);
}
a=a*a;
a=change(a);
k/=2;
}
printf("Case %d: %d %03d\n",++cas,(int)ans1,ans2);
}
return 0;
}
PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
nk.
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 leadingdigits (most significant) and three trailing digits (least significant). Youcan assume that the input is given such thatnk contains atleast 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 |
思路:后三位很好求,快速幂取模就好了,同样前三位我们也可以模拟快速幂来写,用double存储答案,模拟一下快速幂,中间记得转换就好了,注意输出为3位,如果后三位是022,那么必须输出022,不能输出22
ac代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<set>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#define MAXN 101000
#define LL long long
#define ll __int64
#define INF 0xfffffff
#define mem(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-8
using namespace std;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
double dpow(double a,ll b){double ans=1.0;while(b){if(b%2)ans=ans*a;a=a*a;b/=2;}return ans;}
//head
double change(double x)
{
while(x>=1000.0)
{
x/=10.0;
}
return x;
}
int main()
{
int t,i;
int cas=0;
scanf("%d",&t);
while(t--)
{
ll n,k;
scanf("%I64d%I64d",&n,&k);
int ans2=(int)powmod(n,k,1000);
double ans1=1.0,a=n*1.0;
while(k)
{
if(k%2)
{
ans1=ans1*a;
ans1=change(ans1);
}
a=a*a;
a=change(a);
k/=2;
}
printf("Case %d: %d %03d\n",++cas,(int)ans1,ans2);
}
return 0;
}
相关文章推荐
- 山东省第一届ACM大学生程序设计竞赛(原题) Ivan comes again!
- CodeForces 651B Beautiful Paintings
- CodeForces 651B Beautiful Paintings
- 【读书笔记】Android访问远程数据的步骤(Messenger、AIDL、ContentProvider
- 【ZOJ 1937】 【POJ 2248】 Addition Chains
- Poj 2010 Moo University - Financial Aid【优先队列+神技巧】
- RAII&智能指针
- Light OJ 1223 Testing Mailboxes (DP)
- 10014---Trail ~ CronJobs
- 31.offsetof宏与container_of宏
- HDU 5235 Friends (2015 Multi-University Training Contest 2 搜索+剪枝)
- POJ 2010 Moo University - Financial Aid
- 【BZOJ-2434】阿狸的打字机 AC自动机 + Fail树 + DFS序 + 树状数组
- linker command failed with exit code 1)错误总结
- 70. Climbing Stairs
- 本地socket unix domain socket
- 杭电1022Train Problem I
- A. Dreamoon and Stairs(Codeforces Round #272)
- QPainter 使用注意事项
- 杭电1021Fibonacci Again