Uva-11029-leading and Trailing (Fast Power + formula variant)

Source: Internet
Author: User
Tags cmath






Topic Transfer: UVA-11029






Idea: The last three bits can be directly and quickly power modulo, then the first three bits can have two approaches, one is the use of double, one is the use of the formula method, the specific look at the code bar






Note that the rear three bit is less than three bits to fill 0, that is, with%03d






AC Code ①:




#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <cctype>
#define LL long long
#define INF 1000000000
using namespace std;

#define MOD 1000

int T;
int n, k;

int kmod (int x, int n) {// fast power
int ret = 1;
while (n) {
if (n & 1) ret = (ret * x)% MOD;
x = (x * x)% MOD;
n >> = 1;
}
return ret;
}

double kkmod (double x, int n) {// Use double to find the first three digits
double ret = 1;
while (n) {
if (n & 1) ret = ret * x;
while (ret> = INF) ret / = INF;
x = x * x;
while (x> = INF) x / = INF;
n >> = 1;
}
return ret;
}

int main () {
scanf ("% d", & T);
while (T-) {
scanf ("% d% d", & n, & k);
Ransom
int ttt = n% 1000;
ttt = kmod (ttt, k);
Ransom
double lll = kkmod ((double) n, k);
lll * = 1000; // may lll be less than 1000, maybe less than three
while (lll> = 1000) {
lll / = 10;
}
/ * char str [1234];
sprintf (str, "% lf", 1000 * lll);
str [3] = '\ 0'; * / // You can also output the first three digits like this
// printf ("% lf \ n", lll);
Ransom
printf ("% d ...% 03d \ n", (int) lll, ttt); // Remember to use% 03d for the last three digits, and output it strictly according to the format
}
return 0;
}


AC Code ②:





#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <deque>
#include <cctype>
#define LL long long
#define INF 0x7fffffff
using namespace std;

int T;
int n, k;

int kmod (int x, int n) {
int ret = 1;
while (n) {
if (n & 1) ret = (ret * x)% 1000;
x = x * x% 1000;
n >> = 1;
}
return ret;
}

int main () {
scanf ("% d", & T);
while (T-) {
scanf ("% d% d", & n, & k);
int lll, ttt;
lll = kmod (n% 1000, k);
ttt = (int) pow (10, 2 + fmod (k * log10 (n), 1));
printf ("% d ...% 03d \ n", ttt, lll);
}
return 0;
}


Uva-11029-leading and Trailing (Fast Power + formula variant)


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.