2014-11-22 21:44:23
总结:A快速的A掉了,后来发现没考虑0又交了一发QAQ(我的分数啊。。。)
B题比赛的时候过了,后来fst TLE了。后来发现长度枚举的时候智障了,因为长度尽量小,所以一旦发现答案就break醋来- -(智商下线了)
A:找最大素数因子,直接开循环除即可。
1 /************************************************************************* 2 > File Name: a.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Sat 22 Nov 2014 06:59:30 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <vector> 13 #include <map> 14 #include <set> 15 #include <stack> 16 #include <queue> 17 #include <iostream> 18 #include <algorithm> 19 using namespace std; 20 #define lp (p << 1) 21 #define rp (p << 1|1) 22 #define getmid(l,r) (l + (r - l) / 2) 23 #define MP(a,b) make_pair(a,b) 24 typedef long long ll; 25 const int INF = 1 << 30; 26 27 int n; 28 29 int main(){ 30 while(scanf("%d",&n) != EOF){ 31 if(n == 1){ 32 printf("0\n"); 33 continue; 34 } 35 int m = n; 36 int k = sqrt(1.0 * n); 37 int ans = 0; 38 for(int i = 2; i <= k; ++i) if(n % i == 0){ 39 while(n % i == 0){ 40 n /= i; 41 } 42 ans = max(ans,i); 43 } 44 ans = max(ans,n); 45 printf("%d\n",m / ans); 46 } 47 return 0; 48 } 49 50
B:枚举要加进去的数列的长度,然后再把S插入所枚举的数列,比如:枚举145,S是12,那么S可以插在4个位置,形成12145 , 11245 , 14125 , 14512四个数,得到四个T,再判断所枚举到的T能否整除A,若能就的得到B。(可以证明:枚举的长度<=4)
1 /************************************************************************* 2 > File Name: b.cpp 3 > Author: Nature 4 > Mail: 564374850@qq.com 5 > Created Time: Sat 22 Nov 2014 07:08:41 PM CST 6 ************************************************************************/ 7 8 #include <cstdio> 9 #include <cstring> 10 #include <cstdlib> 11 #include <cmath> 12 #include <vector> 13 #include <map> 14 #include <set> 15 #include <stack> 16 #include <queue> 17 #include <iostream> 18 #include <algorithm> 19 using namespace std; 20 #define lp (p << 1) 21 #define rp (p << 1|1) 22 #define getmid(l,r) (l + (r - l) / 2) 23 #define MP(a,b) make_pair(a,b) 24 typedef long long ll; 25 const int INF = 1 << 30; 26 27 int A,len,flag; 28 char s[100]; 29 ll val[100]; 30 ll ans,st,stval,tl; 31 32 void Solve(int p,int limit,ll t){ 33 if(p > limit){ 34 int base = 1; 35 for(int i = 0; i <= limit; base *= 10,++i){ 36 ll tmp = 0; 37 if(i != limit && val[1] == 0) 38 continue; 39 if(i == limit && s[1] == '0') 40 continue; 41 tmp = (t / base); 42 tmp = tmp * st + stval; 43 tmp = tmp * base + t % base; 44 if(tmp % A == 0 && tmp / A < ans){ 45 flag = 1; 46 ans = tmp / A; 47 } 48 } 49 return; 50 } 51 for(int i = 0; i <= 9; ++i){ 52 val[p] = i; 53 Solve(p + 1,limit,t * 10 + i); 54 } 55 } 56 57 int main(){ 58 while(scanf("%d%s",&A,s + 1) != EOF){ 59 ans = 1000000000000000000LL; 60 len = strlen(s + 1); 61 st = 1; 62 stval = 0; 63 flag = 0; 64 for(int i = 1; i <= len; ++i){ 65 stval = stval * 10 + s[i] - '0'; 66 st *= 10; 67 } 68 for(int i = 0; i <= 4; ++i){ 69 tl = 1; 70 for(int j = 1; j <= i; ++j) tl *= 10; 71 Solve(1,i,0); 72 if(flag) break; 73 } 74 printf("%I64d\n",ans); 75 } 76 return 0; 77 }