sgu111高精度开方

来源:互联网 发布:软件技术服务合同范本 编辑:程序博客网 时间:2024/04/29 23:35

题目

111. Very simple problem

time limit per test: 0.25 sec. 
memory limit per test: 4096 KB

You are given natural number X. Find such maximum integer number that it square is not greater than X.

Input

Input file contains number X (1≤X≤101000).

Output

Write answer in output file.

Sample Input

16

Sample Output

4

题解:

高精度开方加压位(压七位)+二分法  PS:直接高精度二分会TLE

压位打法好!!!!!


附上代码

#include <cstdio>#include <cstring>#define L 10000000using namespace std;struct node{int len;long long num[1500];}st = {0};char s[1500] = {'\0'};void init(){scanf("%s", s);int l = strlen(s), tens[8] = {0, 1, 10, 100, 1000, 10000, 100000, 1000000};st.len = (l - 1) / 7 + 1;for (int i = l - 1, j = 1, k = 1; i >= 0; i--, j++){if (j == 8) j = 1, k++;st.num[k] += tens[j] * (s[i] - '0');}}void outp(struct node a){printf("%lld", a.num[a.len]);for (int i = a.len - 1; i >= 1; i--){if (a.num[i] < 1000000) printf("0");if (a.num[i] < 100000) printf("0");if (a.num[i] < 10000) printf("0");if (a.num[i] < 1000) printf("0");if (a.num[i] < 100) printf("0");if (a.num[i] < 10) printf("0");printf("%lld", a.num[i]);}printf("\n");}bool smaller(struct node a, struct node b){if (a.len > b.len) return false;if (a.len < b.len) return true;for (int i = a.len; i >= 1; i--){if (a.num[i] > b.num[i]) return false;if (a.num[i] < b.num[i]) return true;}return false;}bool same(struct node a, struct node b){if (a.len != b.len) return false;for (int i = 1; i <= a.len; i++)if (a.num[i] != b.num[i])return false;return true;}struct node add(struct node a, struct node b){struct node sum = {0};sum.len = a.len;if (b.len > sum.len) sum.len = b.len;for (int i = 1; i <= sum.len; i++) sum.num[i] = a.num[i] + b.num[i];for (int i = 1; i <= sum.len; i++){sum.num[i + 1] += sum.num[i] / L;sum.num[i] %= L;}if (sum.num[sum.len + 1] > 0) sum.len++;while (sum.num[sum.len] == 0) sum.len--;if (sum.len < 1) sum.len = 1;return sum;}struct node ave(struct node a, struct node b){struct node sum = {0};sum = add(a, b);for (int i = sum.len; i >= 1; i--){if (i != 1 && sum.num[i] % 2 != 0){sum.num[i - 1] += L;sum.num[i] --;}sum.num[i] /= 2;}while (sum.num[sum.len] == 0) sum.len--;if (sum.len < 1) sum.len = 1;return sum;}struct node cheng(struct node a, struct node b){struct node sum = {0};sum.len = a.len + b.len - 1;for (int i = 1; i <= a.len; i++)for (int j = 1; j <= b.len; j++)sum.num[i + j - 1] += a.num[i] * b.num[j];for (int i = 1; i <= sum.len; i++){sum.num[i + 1] += sum.num[i] / L;sum.num[i] %= L;}if (sum.num[sum.len + 1] > 0) sum.len++;while (sum.num[sum.len] == 0) sum.len--;if (sum.len < 1) sum.len = 1;return sum;}void work(){struct node l = {0}, r = st, middle, last;l.len = 1;while (smaller(l, r)){middle = ave(l, r);if (same(l, middle)){middle.num[1]++;for (int i = 1; middle.num[i] >= L; i++){middle.num[i + 1]++;middle.num[i] -= L;}}if (same(last, middle)) break;struct node ch = cheng(middle, middle);if (same(ch, st)){l = middle;break;}if (smaller(ch, st)) l = middle;else r = middle;last = middle;}outp(l);}int main(){init();if (st.len == 1 && st.num[1] == 0) printf("0\n");else work();return 0;}
代码丑陋不要见怪

0 0
原创粉丝点击