标签:
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 79 Accepted Submission(s): 41
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; #define N 10000005 char str[N]; char result[N]; char b[100]; int cmp(char a,char b) { return a>b; } void reverse( char *s ) /*将字符串逆置*/ { int length; int i = 0; char temp; length = strlen( s ); while( i < length - i - 1 ) { temp = s[i]; s[i] = s[length - i - 1]; s[length - i - 1] = temp; i++; } } void AddBigNum( char* s1, char* s2, char* result ) { int len1 = strlen( s1 ); int len2 = strlen( s2 ); int acc = 0, temp, i; /*acc为进位标记*/ if( s1 == NULL || s2 == NULL || result == NULL ) { return; } reverse( s1 ); reverse( s2 ); for( i = 0; i < len1 && i < len2; i++ ) { temp = s1[i] - ‘0‘ + s2[i] - ‘0‘ + acc; /*计算每位的实际和*/ result[i] = temp % 10 + ‘0‘; /*通过求余数来确定每位的最终值*/ if( temp >= 10 ) /*通过这个if..else..条件来判断是否有进位,并设置进位值*/ acc = 1; else acc = 0; } if( i < len1 ) /*两个加数位数不同*/ { for( ; i < len1; i++ ) { temp = s1[i] - ‘0‘ + acc; /*依旧要考虑进位,比如9999 + 1的情况*/ result[i] = temp % 10 + ‘0‘; if( temp >= 10 ) acc = 1; else acc = 0; } } if( i < len2 ) { for( ; i < len2; i++ ) { temp = s2[i] - ‘0‘ + acc; result[i] = temp % 10 + ‘0‘; if( temp >= 10 ) acc = 1; else acc = 0; } } if( acc == 1 ) /*考虑如:123 + 911 = 1034的情况,如果不增加这个条件会得到结果为034,进位被舍弃*/ result[i++] = ‘1‘; result[i] = ‘\0‘; reverse( result ); } int main() { int tcase; scanf("%d",&tcase); while(tcase--) { scanf("%s",str); int len = strlen(str); sort(str,str+len,cmp); if(len==1) { printf("Uncertain\n"); } else { bool flag = true; int ans = 0; for(int i=len-1; i>=0; i--) { if(str[i]!=‘0‘) { ans = i; break; } } if(ans==0) { printf("Uncertain\n"); continue; } b[0] = str[ans]; int id = 0; for(int i=0; i<len; i++) { if(i==ans) continue; str[id++] = str[i]; } str[id]=‘\0‘; AddBigNum(str,b,result); printf("%s\n",result); } } return 0; }
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 74 Accepted Submission(s): 30
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> using namespace std; const long long mod = 998244353; const int N = 100005; int b[N],c[N]; long long dp[N]; int main() { int tcase; scanf("%d",&tcase); while(tcase--) { int n; scanf("%d",&n); int MIN = 9999999; int MAX = -1; bool flag = true; for(int i=1;i<=n;i++){ scanf("%d",&b[i]); if(b[i]<1||b[i]>n) flag = false; if(b[i]>MIN) flag = false; MIN = min(MIN,b[i]); } for(int i=1;i<=n;i++){ scanf("%d",&c[i]); if(c[i]<MAX) flag = false; if(c[i]<1||c[i]>n) flag = false; MAX = max(MAX,c[i]); if(c[i]<b[i]) flag = false; } if(!flag||c[1]!=b[1]) printf("0\n"); else{ memset(dp,0,sizeof(dp)); dp[1] = 1; int num =1 ; for(int i=2;i<=n;i++){ if(c[i]==c[i-1]&&b[i-1]==b[i]) { dp[i] = dp[i-1]*(c[i]-b[i]-num+1)%mod; } else if(b[i]<b[i-1]&&c[i-1]==c[i]||b[i]==b[i-1]&&c[i-1]<c[i]){ dp[i] = dp[i-1]; } num++; } printf("%I64d\n",dp[n]); } } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5679702.html