BZOJ 1416: [NOI2006]神奇的口袋 高精度

来源:互联网 发布:3tier数据 编辑:程序博客网 时间:2024/05/14 05:42

1416: [NOI2006]神奇的口袋

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 528  Solved: 332
[Submit][Status][Discuss]

Description

Input

Output

 0/1,概率为1 应输出1/1。

Sample Input

input 1
2 3 1
1 1
1 1
2 2
3 1

input 2
3 1 2
1 1 1
5 1

Sample Output

output 1
1/12

output 2
1/3

HINT


1≤t,n≤1000, 1≤ak ,d≤10, 1≤x1


BJ又一次用分解质因数水了高精度。。。。


话说,现在代码很清真了,连register 都不用了。。。


#include<cmath>#include<ctime>#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>#include<iomanip>#include<vector>#include<bitset>#include<string>#include<queue>#include<set>#include<map>using namespace std;typedef long long ll;inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch<='9'&&ch>='0'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}void print(int x){if(x<0)putchar('-'),x=-x;if(x>=10)print(x/10);putchar(x%10+'0');}const int N=200100,rab=1000;int prime[N],pos[N],pcnt;bool book[N];void initial(){pcnt=0;for(int i=2;i<N;++i){if(!book[i])prime[++pcnt]=i,pos[i]=pcnt;for(int j=1;j<=pcnt&&prime[j]*i<N;++j){book[prime[j]*i]=1;if(i%prime[j]==0)break;}}}int num[N];inline void solve(int x,int p){for(int i=1;prime[i]<=x;++i){while(x%prime[i]==0){x/=prime[i];num[i]+=p;}if(!book[x]){num[pos[x]]+=p;return ;}}}struct bint{int a[2000];friend bint operator *(bint &x,int y){for(int i=1;i<=x.a[0];++i)x.a[i]*=y;for(int i=1;i<=x.a[0];++i)if(x.a[i]>=rab){if(i==x.a[0])x.a[0]++;x.a[i+1]+=x.a[i]/rab;x.a[i]%=rab;}return x;}friend void out(bint &x){printf("%d",x.a[x.a[0]]);for(int i=x.a[0]-1;i;i--)printf("%03d",x.a[i]);}};int a[N];int main(){initial();int n=read(),Q=read(),D=read();int sum=0;for(int i=1;i<=n;++i)a[i]=read(),sum+=a[i];while(Q--){int y=read();y=read();if(y>n){puts("0/1");return 0;}solve(sum,-1);solve(a[y],1);sum+=D;a[y]+=D;}bint son,mother;memset(son.a,0,sizeof(son.a));memset(mother.a,0,sizeof(mother.a));son.a[0]=mother.a[0]=son.a[1]=mother.a[1]=1;for(int i=1;i<=pcnt;++i)if(num[i]){if(num[i]>0){while(num[i]--)son=son*prime[i];}else {while(num[i]++)mother=mother*prime[i];}}out(son);putchar('/');out(mother);putchar('\n');return 0;}/*2 3 11 11 12 23 11/123 1 21 1 15 11/3*/