104 条题解
-
2卢睿韬 LV 7 @ 2018-05-23 17:50:59
#include<bits/stdc++.h>
using namespace std;int ans[100001];
char s[7];
int main()
{
int a=1;
int n;
while(scanf("%s %d", s, &n)==2&&a++)
{ if(a!=2)puts("");
memset(ans,0,sizeof(ans));
ans[1]=1;
int dot;
int b=0;
int zhen;
for(int i=5;i>=0;i--)
if(s[i]!='0')
{
zhen=i;
break;
}for(int i=0;i<=zhen;i++)
{
if(s[i]!='.')
b=b*10+s[i]-'0';
else
dot=(zhen-i)*n;
}
int max=1;
for(int i=1;i<=n;i++)
{int in=0,j;
for(j=1;j<=max||in;j++)
{
int now=b*ans[j]+in;
ans[j]=now;
in=ans[j]/10;
ans[j]%=10;
}
if(max<j-1)
max=j-1;
}
for(int i=max;i>dot;i--)
printf("%d",ans[i]);
if(dot!=0)
printf(".");
for(int i=dot;i>0;i--)
printf("%d",ans[i]);
}
return 0;
} -
22017-05-07 12:45:33@
/*
不多说 这道题写了整整两节晚自习
最后发现代码太丑了
不敢贴出来啊
这就是个裸的高精度比谁会写代码啊
心好痛 于是丢了自己的代码(100++)
丢个别人的正常点的代码吧
谁让我作死去弄啥重载运算符
醉了醉了
其实思想很简单
就是像我们做小数乘法一样
我们把他当作整数乘法来做 只需要记录下小数点的位置
然后加上小数点即可 不输出前导0和后缀0,整数就只输出整数部分
注意一些0的处理问题就好了
蛮坑的
数据范围还有点带感
唉还有36天NOIP了 感觉要被虐了
*/#include <iostream> #include <stdio.h> #include <string.h> using namespace std; int ans[100001]; char s[7]; int main() { int a=1; int n; while(scanf("%s %d", s, &n)==2&&a++) { if(a!=2)puts(""); memset(ans,0,sizeof(ans)); ans[1]=1; int dot; int b=0; int zhen; for(int i=5;i>=0;i--) if(s[i]!='0') { zhen=i; break; } for(int i=0;i<=zhen;i++) { if(s[i]!='.') b=b*10+s[i]-'0'; else dot=(zhen-i)*n; } int max=1; for(int i=1;i<=n;i++) {int in=0,j; for(j=1;j<=max||in;j++) { int now=b*ans[j]+in; ans[j]=now; in=ans[j]/10; ans[j]%=10; } if(max<j-1) max=j-1; } for(int i=max;i>dot;i--) printf("%d",ans[i]); if(dot!=0) printf("."); for(int i=dot;i>0;i--) printf("%d",ans[i]); } return 0; }
-
02024-08-16 12:32:34@
#include <iostream> #include <stdio.h> #include <string.h> using namespace std; int ans[100001]; char s[7]; int main() { int a=1; int n; while(scanf("%s %d", s, &n)==2&&a++) { if(a!=2)puts(""); memset(ans,0,sizeof(ans)); ans[1]=1; int dot; int b=0; int zhen; for(int i=5;i>=0;i--) if(s[i]!='0') { zhen=i; break; } for(int i=0;i<=zhen;i++) { if(s[i]!='.') b=b*10+s[i]-'0'; else dot=(zhen-i)*n; } int max=1; for(int i=1;i<=n;i++) {int in=0,j; for(j=1;j<=max||in;j++) { int now=b*ans[j]+in; ans[j]=now; in=ans[j]/10; ans[j]%=10; } if(max<j-1) max=j-1; } for(int i=max;i>dot;i--) printf("%d",ans[i]); if(dot!=0) printf("."); for(int i=dot;i>0;i--) printf("%d",ans[i]); } return 0; }
-
02022-04-09 15:37:51@
#include<stdio.h> #include<algorithm> using namespace std; #define ll long long ll x,y,n,m,L; void kzgcd(ll a,ll b,ll &d,ll &x,ll &y) { if(b==0) { d=a;x=1;y=0; return; } kzgcd(b,a%b,d,y,x); y-=a/b*x; } int main() { scanf("%lld%lld%lld%lld%lld",&x,&y,&m,&n,&L); ll a=((n-m)%L+L)%L; ll b=L; ll c=((x-y)%L+L)%L; ll d,e,p,q; kzgcd(a,b,d,p,q); if(c%d) { printf("Impossible"); return 0; } e=b/d; printf("%lld",((p*(c/d))%e+e)%e); }
-
02022-03-04 13:58:57@
python 投机取巧不用写高精度
import decimal
import sys
decimal.getcontext().prec = 1000
while True:
ss=sys.stdin.readline()
if ss=='':
break
num,p=ss.split()
x=decimal.Decimal(num)
ans=str("{:.900f}".format(pow(x,int(p))))
if '.' in ans:
ans=ans.strip( '0' )
if ans[-1]=='.':
ans=ans.strip('.')
print(ans) -
02021-05-06 17:08:09@
#include<bits/stdc++.h>
using namespace std;int ans[100001];
char s[7];
int main()
{
int a=1;
int n;
while(scanf("%s %d", s, &n)==2&&a++)
{ if(a!=2)puts("");
memset(ans,0,sizeof(ans));
ans[1]=1;
int dot;
int b=0;
int zhen;
for(int i=5;i>=0;i--)
if(s[i]!='0')
{
zhen=i;
break;
}for(int i=0;i<=zhen;i++)
{
if(s[i]!='.')
b=b*10+s[i]-'0';
else
dot=(zhen-i)*n;
}
int max=1;
for(int i=1;i<=n;i++)
{int in=0,j;
for(j=1;j<=max||in;j++)
{
int now=b*ans[j]+in;
ans[j]=now;
in=ans[j]/10;
ans[j]%=10;
}
if(max<j-1)
max=j-1;
}
for(int i=max;i>dot;i--)
printf("%d",ans[i]);
if(dot!=0)
printf(".");
for(int i=dot;i>0;i--)
printf("%d",ans[i]);
}
return 0;
} -
02020-08-29 19:51:00@
₈
-
02017-06-21 21:20:22@
dasdasdas
-
02017-06-21 21:20:17@
- sadasd
-
02016-02-03 11:25:25@
/*
时间:2016.2.2
题号:VJ P1010
方法:重载运算符,注意小数点的操作
*/
#include<iostream>
#include<cstring>
using namespace std;
const int N = 250 * 6;
char ans[11][N];
class A
{ public:
A operator*(A &b); //重载运算符
int get(); //将字符串转化为整数数组。
void print(int n); //将结果保存到字符串ans
private:
int num[N] = {0}, len, point;
};
int A::get()
{ char c[7]; int i; bool flag = false;
if(!(cin >> c)) return 0;
len = strlen(c) - 1;
for(i = 0;i < len;i++) {
if(c[i] != '.' && !flag) num[len-i-1] = c[i] - '0';
if(c[i] == '.'){ flag = true;point = len - i; }
if(flag) num[len-i-1] = c[i+1] - '0'; }
return 1;
}
A A::operator*(A &b)
{ int i, j, k = 0;
A res;
for(i = 0;i < len;i++) {
for(j = 0;j < b.len;j++) {
res.num[i+j] = res.num[i+j] + num[i] * b.num[j] + k;
k = res.num[i+j] / 10;res.num[i+j] %= 10; }
if(k != 0) res.num[i+j] = k;k = 0;
}
res.len = len + b.len;
while(res.num[res.len] == 0 && res.len > point+b.point) res.len--;
res.len++;res.point = point + b.point;
return res;
}
void A::print(int n)
{ int i = len - 1, j = 0, k = 0; bool flag = false;
while(num[j] == 0 && j < point) j++;
if(num[len-1] == 0 && j == i) { ans[n][k] = 0 + '0';return ; }
if(i == point && num[i] == 0){ ans[n][k] = '.';k++;i--;flag = true; }
for(;i >= j;i--)
if(i == point - 1 && !flag)
{ ans[n][k] = '.';k++;ans[n][k] = num[i] + '0';k++; }
else { ans[n][k] = num[i] + '0';k++; }
}
int main()
{ int n, j = 0; A a;
while(a.get() && cin >> n)
{ A b = a;
for(int i = 0;i < n-1;i++) b = a * b;
b.print(j);j++; }
for(int i = 0;i < j;i++) cout << ans[i] << endl;
return 0;
} -
02015-08-28 13:23:00@
-
02015-08-01 20:24:16@
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=2000;
int numn;
class bint
{
int n[maxn];
int ed;
int dot;
public:
bint();
bint(char a);
void p1();
bint operator(const bint &a);
void print();
};
void bint::p1()
{
n[0]=1;
ed=1;
dot=0;
}
bint::bint()
{
memset(n,0,sizeof(n));
ed=0;
dot=0;
}
bint::bint(char *a)
{
int l=strlen(a);
int adot=0;
ed=0;
for(int i=l-1;i>=0;i--)if(a[i]=='.')adot=l-1-i;
// printf("adot=%d\n",adot);
if(adot==0)
{
for(int i=l-1;i>=0;i--)n[l-1-i]=a[i]-'0';
ed=l;
}
else
{
bool f=false;
int c=0;
for(int i=l-1;i>=0;i--){
if(a[i]>'0')f=true;
if(i>l-1-adot&&a[i]=='0'&&!f){c++;continue;}
if(a[i]!='.'){n[ed++]=a[i]-'0';}
}
dot=adot-c;
}
}
bint bint::operator *(const bint &a)
{
bint ans;
for(int i=0;i<ed;i++)
for(int j=0;j<a.ed;j++)
ans.n[i+j]+=n[i]*a.n[j];
ans.ed=ed+a.ed-1;
for(int i=0;i<=ed+a.ed-2;i++)
{
ans.n[i+1]+=(ans.n[i]/10);
ans.n[i]%=10;
}
if(ans.n[ans.ed])
ans.ed++;
ans.dot=dot+a.dot;
return ans;
}
void bint::print()
{
if(dot==0)
{
for(int i=ed-1;i>=0;i--)printf("%d",n[i]);printf("\n");
}
else
{
bool f=false;
for(int i=ed-1;i>=0;i--)
{
if(n[i]>0)f=true;
if(!(n[i]==0&&!f))
printf("%d",n[i]);
if(i==dot){f=true;printf(".");}
}printf("\n");
}
}
int main()
{
// freopen("1.in","r",stdin);
// freopen("1.out","w",stdout);
char a[10];int n;
bool vis[10];
while(scanf("%s%d",a,&n)!=EOF)
{
bint m[9];
bint ans;
ans.p1();
//printf("%s\n",a);
int k=n;int con=0;
while(k!=0)
{
if(k%2)vis[con]=true;
con++;k/=2;
}
m[0]=a;
// m[0].print();
for(int j=1;j<=8;j++){
m[j]=m[j-1]*m[j-1];
// m[j].print();
}
for(int j=0;j<=con;j++)
if(vis[j])ans=ans*m[j];
ans.print();
memset(a,0,sizeof(a));
memset(vis,false,sizeof(vis));
}
return 0;
}
快速幂+高精度+小数点。。
还有freopen一定要删掉。。 -
02015-07-24 12:55:24@
记录信息
评测状态 Accepted
题目 P1010 清帝之惑之乾隆
递交时间 2015-08-22 08:39:45
代码语言 C++
评测机 VijosEx
消耗时间 184 ms
消耗内存 912 KiB
评测时间 2015-08-22 08:39:47
评测结果
编译成功foo.cpp: In function 'int main()':
foo.cpp:17:7: warning: 'zhen' may be used uninitialized in this function [-Wmaybe-uninitialized]
int zhen;
^
foo.cpp:49:3: warning: 'dot' may be used uninitialized in this function [-Wmaybe-uninitialized]
for(int i=dot;i>0;i--)
^
测试数据 #0: Accepted, time = 15 ms, mem = 908 KiB, score = 10
测试数据 #1: Accepted, time = 51 ms, mem = 908 KiB, score = 10
测试数据 #2: Accepted, time = 31 ms, mem = 908 KiB, score = 10
测试数据 #3: Accepted, time = 20 ms, mem = 908 KiB, score = 10
测试数据 #4: Accepted, time = 15 ms, mem = 908 KiB, score = 10
测试数据 #5: Accepted, time = 21 ms, mem = 904 KiB, score = 10
测试数据 #6: Accepted, time = 15 ms, mem = 912 KiB, score = 10
测试数据 #7: Accepted, time = 0 ms, mem = 912 KiB, score = 10
测试数据 #8: Accepted, time = 1 ms, mem = 904 KiB, score = 10
测试数据 #9: Accepted, time = 15 ms, mem = 908 KiB, score = 10
Accepted, time = 184 ms, mem = 912 KiB, score = 100
代码
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int ans[100001];
char s[7];
int main()
{
int a=1;
int n;
while(scanf("%s %d", s, &n)==2&&a++)
{ if(a!=2)puts("");
memset(ans,0,sizeof(ans));
ans[1]=1;
int dot;
int b=0;
int zhen;
for(int i=5;i>=0;i--)
if(s[i]!='0')
{
zhen=i;
break;
}for(int i=0;i<=zhen;i++)
{
if(s[i]!='.')
b=b*10+s[i]-'0';
else
dot=(zhen-i)*n;
}
int max=1;
for(int i=1;i<=n;i++)
{int in=0,j;
for(j=1;j<=max||in;j++)
{
int now=b*ans[j]+in;
ans[j]=now;
in=ans[j]/10;
ans[j]%=10;
}
if(max<j-1)
max=j-1;
}
for(int i=max;i>dot;i--)
printf("%d",ans[i]);
if(dot!=0)
printf(".");
for(int i=dot;i>0;i--)
printf("%d",ans[i]);
}
return 0;
} -
02015-04-09 21:58:23@
#include <vector>
#include <list>
#include <limits.h>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <memory.h>
using namespace std;int n,big[40000],ans[40000],len,lena,point;
bool init()
{
int temp,now;
char s[15];
len = 0;
temp = 0;
memset(big,0,sizeof(big));
memset(ans,0,sizeof(ans));
if (scanf("%s",s) != 1)
return false;
scanf("%d",&n);
for (now = 0; s[now] != '.'; ++ now)
{
temp *= 10;
temp += s[now] - '0';
}
point = 0;
for (++ now; now < 6; ++ now)
{
temp *= 10;
temp += s[now] - '0';
++ point;
}
while (temp % 10 == 0 && point > 0)
{
temp /= 10;
-- point;
}
while (temp != 0)
{
++ len;
big[len] = temp % 10;
temp /= 10;
}
lena = len;
memcpy(ans,big,sizeof(ans));
return true;
}void large()
{
int a[40000],b[40000],c[40000];
memset(c,0,sizeof(c));
memcpy(a,big,sizeof(big));
memcpy(b,ans,sizeof(ans));
for (int i = 1; i <= lena; ++ i)
for (int j = 1; j <= len; ++ j)
c[i+j-1] += a[i] * b[j];
len += lena + 5;
for (int i = 1; i <= len; ++ i)
{
c[i+1] += c[i] / 10;
c[i] %= 10;
}
while (c[len] == 0)
-- len;
memcpy(ans,c,sizeof(c));
}int main()
{
while(init())
{
if (n == 0)
{
printf("1\n");
continue;
}
point *= n;
for (int i = 2; i <= n; ++ i)
large();
if (point >= len)
{
printf(".");
for (int j = point; j > 0; -- j)
printf("%d",ans[j]);
}
else
{
for (int j = len; j > point; -- j)
printf("%d",ans[j]);
if (point == 0)
{
printf("\n");
continue;
}
printf(".");
for (int j = point; j > 0; -- j)
printf("%d",ans[j]);
}
printf("\n");
}
return 0;
} -
02015-02-07 09:19:55@
表示蛋疼:
vijos,你出题人会睁眼说瞎话,靠那里是8,9列,是8,9,10列!!!也就是说最大幂次数为999,不是99。。。
你害我WA了4次!!!浪费了我多长宝贵的时间。。。。
注意多组数据,该Memset的一定不要忘,蓝后就跟小数点搞搞就可以过了
yuyilahanbao回复于2014-02-01 13:59
最大幂次数出题人说了n<200
搞得我最大5*200=1000位,然后开了1010位
但是实际上有一个n达到了226
这才是最坑的. -
02014-10-23 18:18:06@
本地测试各种数据都没问题,不知道为什么提交就全是wrong answer,,
无语中。。。。
#include<stdio.h>
#include<memory.h>
#define max 2000int main()
{
int i = 0,j = 0,k = 0;
int n;
char r[6];
int str_a[5];
int str_b[max] = {0},t_result[max]={0};
int point = 0,lb,la;
int cnt;
scanf("%6s %d",r,&n);
lb = 6;
for(i = lb - 1,j = 0;i >= 0;i--)
{
if(r[i]=='.')
{
point = lb - 1 - i;
}
else
{
str_a[j] = r[i] - '0';
str_b[j] = str_a[j];
j++;
}
}
lb--;
la = lb;
point *= n;
for(i = 0;i < n - 1;i++)
{
for(j = 0;j < la;j++)
{
cnt = j;
for(k = 0;k < lb;k++)
{
t_result[cnt++] += str_a[j] * str_b[k];
}
}
for(j = 0;j < cnt - 1;j++)
{
t_result[j + 1] += t_result[j] / 10;
t_result[j] %= 10;
str_b[j] = t_result[j];
}
str_b[j] = t_result[j] % 10;
if(t_result[j]/10 > 0)
{
int t = t_result[j] / 10;
str_b[++j] = t;
}
lb = j + 1;
memset(t_result,0,cnt*sizeof(int));
}
for(i = 0;i < lb;i++)
{
if(str_b[i]!=0)
{
break;
}
}
for(j = lb - 1;j >= i&&j >= point;j--)
{
if(str_b[j]!=0)
break;
}
for(k = j;k >= i;k--)
{
if(k==point - 1)
{
printf(".");
}
printf("%d",str_b[k]);
}
return 0;
} -
02014-02-01 14:02:08@
高精度乘,不解释
先把小数扩大为整数,然后次方,然后缩小会整数
注意题目说n<200,实际上n有一个达到226
搞得我一开始想:最大位数=5*200=1000,所以开1010
结果有一个点错了.原来是有一个竟然是226
于是改开成1210
肯定是写整数的高精度乘,然后#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<ctype.h>
#define M 10
#define N 1210
typedef int big[N];
void base_big(void *a)
{
memset(a,0,sizeof(big));
}
void smlt(int *r,int b,int *a,int n)
{
int static i;
for (i = 0;i < n-1;i++) {
*(r+i) += *(a+i)*b;
*(r+i+1) += *(r+i)/M;
*(r+i) %= M;
}
if (i < n) (r+i) = ((a+i)*b+*(r+i))%M;
}
void mlt(int *r,int *b,int a)
{
int static j;
//memset(r,0,sizeof(big));
base_big(r);
for (j = 0;j < N;j++) smlt(r+j,(b+j),a,N-j);
}
int init_big(int *a,int *p)
{
char s[N+1];
int i,l;
if (scanf("%s",s) != 1)
return 0;
l = strlen(s)-1;
base_big(a);
for (i = l;i >= 0;i--)
if (isdigit(s[i]))
*(a+l-i) = s[i]-'0';
else {
*p = l-i;
l--;
}
return 1;
}void print(big *a,int p)
{
int static i,l,r;
for (i = N-1;i >= p;i--) if ((*a)[i]) break; r = i;
for (;i >= p;i--) printf("%d",(*a)[i]);
for (i = 0;i < p;i++) if ((*a)[i]) break; l = i;
if (l != p) {
printf(".");
for (i = p-1;i >= l;i--) printf("%d",(*a)[i]);
} else {
if (r < p) printf("0");
}
printf("\n");
}//need changebig tmp;
void power(int *r,int *a,int n)
{
if (n == 0) {
base_big(r); *r = 1; return;
}
if (n == 1) {
memcpy(r,a,sizeof(big)); return;
} else if (n&1) {
power(r,a,n>>1);
mlt(tmp,r,r);
mlt(r,tmp,a);
} else {
power(r,a,n>>1);
mlt(tmp,r,r);
memcpy(r,tmp,sizeof(big));
}
}
big a,c;
int n,point;
int main()
{
while (1) {
if (init_big(a,&point) == 0)
break;
scanf("%d",&n);
point *= n;
power(c,a,n);
print(&c,point);
}
return 0;
} -
02013-08-31 14:05:20@
-
02013-07-29 17:55:11@
-
02013-04-22 16:53:26@
###Python被虐了
感觉没有人用Python做这题,不过真的很简单,完全不用手写高精度。但是不知道为什么全部WA,精度500位,本地各种恶心数据都对了。
import math
from decimal import *def main():
outList = []
getcontext().prec = 500
while True:
testLine = raw_input()
if len(testLine) == 0:
break
b = Decimal(testLine[testLine.rfind(" ") + 1:])
tmp = testLine[:testLine.find(" ")]
if tmp.find(".") != -1:
while tmp[-1] == '0':
tmp = tmp[0:-1]
while tmp[0] == '0':
tmp = tmp[1:]
a = Decimal(tmp)
out = str(pow(a, b))
if out[0] == '0':
out = out[1:]
outList.append(out)
for i in outList:
print i
returnmain()