日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

uva11029 - Leading and Trailing

發布時間:2023/11/30 编程问答 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 uva11029 - Leading and Trailing 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

11029 - Leading and Trailing

Time limit: 3.000 seconds?

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=115&page=show_problem&problem=1970

Apart from the novice programmers, all others know that you can’t exactly represent numbers raised to some high power. For example, the C function?pow(125456, 455)?can be represented in double data type format, but you won’t get all the digits of the result. However we can get at least some satisfaction if we could know few of the leading and trailing digits. This is the requirement of this problem.

?

Input

?

The first line of input will be an integer?T<1001, where?T?represents the number of test cases. Each of the next?T?lines contains two positive integers,?n?and?k.?n?will fit in 32 bit integer and?k?will be less than 10000001.

?

Output

?

For each line of input there will be one line of output. It will be of the format LLL…TTT, where LLL represents the first three digits of?n^k?and TTT represents the last three digits of?n^k. You are assured that?n^k?will contain at least 6 digits.

?

?

Sample Input

Output for Sample Input

2

123456 1

123456 2

123...456

152...936




大致的題意是:要求n的k次方,但是n和k非常大的時候沒有辦法去求(實際上可以模擬的,暫且不論),所以題目要求的數據只是n的k次方的前三位和后三位。思路:剛看到題便一眼想到用同余摸定理求出后三位,直接求的話太慢,干脆二分冪吧!但是出題人的用意顯然不只是在此,前三位的求法才是卡人的地方,想了半天,最終用double保存n的k次方,相當于縮小了一定倍數,比如,n的k次方等于344958……那么化成double就是0.344……然后取小數點后3位乘以1000即可!code:#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long ll;
const int mod=1000;
const int M=1000000000;
const int N=0x7fffffff;
ll mi (int n,int m)
{
if (m==1) return n%mod;
if (m%2==0)
return (mi(n,m/2)%1000)*(mi(n,m/2)%mod)%mod;
else
return n*mi(n,m/2)*mi(n,m/2)%mod;
}
double mi2(int n,int m)
{
if (m==0) return 1.0;
double s=mi2(n,m/2)*mi2(n,m/2);
if (m%2)
s*=n;
while (s>M) s/=M;
return s;
}
int main()
{
int t;
cin>>t;
while (t--)
{
int n,k;
cin>>n>>k;
int b=mi(n,k)%mod;
double c=mi2(n,k);
//cout<<c<<endl;
while (c>=1000) c/=10;
int d=c*1000;
while (d>=1000) d/=10;
printf("%d...%03d\n",d,b);
}
}

總結

以上是生活随笔為你收集整理的uva11029 - Leading and Trailing的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。