普通的做法,大数除小数。
复杂度o( log(n)*log(n) ),其实就是位数的平方。
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 4913 | Accepted: 2246 |
Description
Input
Output
Sample Input
8 62 2 abcdefghiz 10 16 1234567890123456789012345678901234567890 16 35 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2 35 23 333YMHOUE8JPLT7OX6K9FYCQ8A 23 49 946B9AA02MI37E3D3MMJ4G7BL2F05 49 61 1VbDkSIMJL3JjRgAdlUfcaWj 61 5 dl9MDSWqwHjDnToKcsWE1S 5 10 42104444441001414401221302402201233340311104212022133030
Sample Output
62 abcdefghiz 2 11011100000100010111110010010110011111001001100011010010001 10 1234567890123456789012345678901234567890 16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2 16 3A0C92075C0DBF3B8ACBC5F96CE3F0AD2 35 333YMHOUE8JPLT7OX6K9FYCQ8A
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <stdlib.h> #include <math.h> using namespace std; char str[10100]; int num[10100]; int ans[10100]; int chg(char c) { if( c>=‘0‘&&c<=‘9‘ ) return c-‘0‘; if( c>=‘A‘&&c<=‘Z‘ ) return c-‘A‘+10; if( c>=‘a‘&&c<=‘z‘ ) return c-‘a‘+36; return -1; } char unchg(int x) { if(x>=0&&x<=9) return x+‘0‘; if(x>=10&&x<=35) return ‘A‘+x-10; else return ‘a‘+x-36; } int main() { int T; cin>>T; while(T--) { int from,to; scanf("%d%d",&from,&to); scanf("%s",str); int len=strlen(str); for(int i=0;i<len;i++) { num[i] = chg(str[i]); } int cnt=0; int wei=0; while( cnt < len ) { //然后做一次除法 for(int i=cnt;i<len;i++) { num[ i+1 ] += (num[i]%to)*from; num[ i ] /= to; } ans[ wei++ ] = num[len]/from; num[len]=0; for(int i=cnt;i<len;i++) { if(num[i]==0) cnt++; else break; } } printf("%d %s\n",from,str); printf("%d ",to); for(int i=wei-1;i>=0;i--) { printf("%c",unchg(ans[i])); } printf("\n\n"); } return 0; }
原文:http://www.cnblogs.com/chenhuan001/p/4981126.html