一题写残了的 大数

//CodeForce 18D - Seller Bob

 1 #include"iostream"
 2 #include"cstdio"
 3 #include"cstring"
 4 #include"algorithm"
 5 using namespace std;
 6 int num[2010][700],pre_pow[5010];
 7 int n;
 8 char cmd[5010][7];
 9 int pow[5010],res[700];
10 struct s1
11 {
12     int l,r,pow;
13 }ans[5010];
14 bool vis[5010];
15
16 bool cmp(s1 a,s1 b)
17 {
18     return a.pow>b.pow;
19 }
20
21 int main()
22 {
23     int i,j,c = 0;
24     int tot = 0;
25     memset(res,0,sizeof(res));
26     num[0][0] = num[0][1] = 1;
27     for(i = 1; i<=2000; ++i) {
28         num[i][0] = num[i-1][0]+1;
29         for(j = 1; j<=num[i][0]; ++j) {
30             num[i][j] = (num[i-1][j]<<1)+c;
31             c = num[i][j]/10;
32             num[i][j] %= 10;
33         }
34         if(!num[i][num[i][0]])
35             --num[i][0];
36     }
37     scanf("%d",&n);
38     for(i = 1; i<=n; ++i) {
39         scanf("%s%d",cmd[i],&pow[i]);
40         if(cmd[i][0]==‘w‘)
41             pre_pow[pow[i]] = i;
42         else {
43             if(pre_pow[pow[i]]) {
44                 ans[++tot].l = pre_pow[pow[i]];
45                 ans[tot].r = i;
46                 ans[tot].pow = pow[i];
47                 pre_pow[pow[i]] = 0;
48             }
49         }
50     }
51     sort(ans+1,ans+1+tot,cmp);
52     for(i = 1; i<=tot; ++i) {
53         bool ok = 1;
54         for(j = ans[i].l; j<=ans[i].r; ++j) {
55             if(vis[j]) {
56                 ok = 0;
57                 break;
58             }
59         }
60         if(ok) {
61             for(j = ans[i].l; j<=ans[i].r; ++j) {
62                 vis[j] = 1;
63             }
64             int tar = ans[i].pow;
65             res[0] = max(res[0],num[tar][0])+1;
66             for(j = 1; j<=res[0]; ++j) {
67                 res[j] += num[tar][j]+c;
68                 c = res[j]/10;
69                 res[j] %= 10;
70             }
71             if(!res[res[0]])
72                 --res[0];
73         }
74     }
75     if(!res[0])
76         ++res[0];
77     for(j = res[0]; j>=1; --j) {
78         printf("%d",res[j]);
79     }
80     printf("\n");
81 }

//还是把功能分给各个函数好一些,这样也易于调试

时间: 2024-08-07 22:28:57

一题写残了的 大数的相关文章

Educational Codeforces Round 1 (C) (atan2 + long double | 大数)

这题只能呵呵了. 东搞西搞,折腾快一天,最后用了一个800多行的代码AC了. 好好的题目你卡这种精度干啥. 还有要卡您就多卡点行不,为什么long double 又可以过... 废了N长时间写个了不管精度的解法,结果网上看别人都是几十行代码轻松搞定,真是要吐血. 不过 还是学了一些东西的. 第一个 :atan2() 函数 想当年sb的我还自己写了个,已知(x,y)的坐标求与x正半轴夹角的模板,但是在这题中精度就过不了了. 用法: 对于坐标(x,y),atan2(y,x) 返回 [-pi,pi]

HDU 1250 大数加法

Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6948    Accepted Submission(s): 2285 Problem Description A Fibonacci sequence is calculated by adding the previous two members the

最短的计算大数乘法的c程序

#include <stdio.h> char s[99],t[99]; int m,n; void r(int i,int c) { int j=0,k=i; while(k)c+=s[j++]*t[k---1]; if(i)r(i-1,c/10); printf("%d",c%10); } void main() { gets(s);gets(t); while(s[n])s[n++]-=48; while(t[m])t[m++]-=48; r(m+n-1,0); }

light oj 1236 【大数分解】

给定一个大数,分解质因数,每个质因子的个数为e1,e2,e3,--em, 则结果为((1+2*e1)*(1+2*e2)--(1+2*em)+1)/2. //light oj 1236 大数分解素因子 #include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include <math.h> #include <ctype.h> #i

nyoj 73 比大小 【java大数】

java大数. 代码: import java.util.Scanner; import java.math.*; public class Main{ public static void main(String[] args){ Scanner cin = new Scanner(System.in); BigInteger a, b; BigInteger t = BigInteger.valueOf(0); a = cin.nextBigInteger(); b = cin.nextBi

uva 1478 - Delta Wave(递推+大数+卡特兰数+组合数学)

题目链接:uva 1478 - Delta Wave 题目大意:对于每个位置来说,可以向上,水平,向下,坐标不能位负,每次上下移动最多为1, 给定n问说有多少种不同的图.结果对10100取模. 解题思路:因为最后都要落回y=0的位置,所以上升的次数和下降的次数是相同的,并且上升下降的关系满足出栈入栈的关系.即卡特兰数. 所以每次枚举i,表示有i个上升,i个下降,用组合数学枚举出位置,然后累加求和. C(2?in)?f(i)=C(2?i?2n)?f(i?1)?(n?2?i+1)?(n?2?i+2)

各类大数模板

ps:转自http://blog.csdn.net/y990041769/article/details/20116995 大数加法模板(本人验证过,其他还没用,只是写在这) 1 string sum(string s1,string s2) 2 { 3 if(s1.length()<s2.length()) 4 { 5 string temp=s1; 6 s1=s2; 7 s2=temp; 8 } 9 int i,j; 10 for(i=s1.length()-1,j=s2.length()-

【51Nod】1005 大数加法

给出2个大整数A,B,计算A+B的结果. Input 第1行:大数A 第2行:大数B (A,B的长度 <= 10000 需注意:A B有可能为负数) Output 输出A + B Input示例 68932147586 468711654886 Output示例 537643802472 ==================================================================================================== 问题解法

HDU 1018 大数(求N!的位数/相加)

Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 35382    Accepted Submission(s): 16888 Problem Description In many applications very large integers numbers are required. Some of these