数学 2015百度之星初赛2 HDOJ 5255 魔法因子

 

题目传送门

 1 /*  2  数学:不会写,学习一下这种解题方式:)  3  思路:设符合条件的数的最高位是h,最低位是l,中间不变的部分为mid,由题意可得到下面的公式(这里对X乘上1e6用a表示,b表示1e6)  4  (h*power+l+mid)*a = (l*power+h+mid)*b  5  可推得:mid = ((h*power+l) * a - (l*power+h) * b) / (a - b);  6  所以可以枚举h,l然后求mid,注意mid的最低位一定是0,因为留出最低位加l或者h  7  详细解释:http://blog.csdn.net/u010660276/article/details/46290703  8 */  9 #include <cstdio> 10 #include <algorithm> 11 #include <cstring> 12 #include <cmath> 13 #include <vector> 14 using namespace std; 15 16 typedef long long ll; 17 const int MAXN = 1e4 + 10; 18 const int INF = 0x3f3f3f3f; 19 vector<ll> V; 20 21 int main(void) //2015百度之星初赛2 HDOJ 5255 魔法因子 22 { 23 int t, cas = 0; scanf ("%d", &t); 24 while (t--) 25  { 26  V.clear (); 27 double x; scanf ("%lf", &x); 28 ll a = (ll) (x * 1e6 + 0.1); ll b = 1e6; 29 30 for (int k=2; k<=10; ++k) 31  { 32 ll p = 1; for (int i=1; i<k; ++i) p *= 10; 33 for (int r=1; r<=9; ++r) 34  { 35 for (int l=0; l<=9; ++l) 36  { 37 ll tmp = (l * p + r) * b - (r * p + l) * a; 38 if (tmp % (10 * (a - b)) == 0) 39  { 40 ll mid = tmp / 10 / (a - b); ll tmp_mid = mid; 41 if (tmp_mid < 0) continue; 42 int cnt = 0; while (tmp_mid) {++cnt; tmp_mid /= 10;} 43 if (cnt <= k - 2) V.push_back (r * p + mid * 10 + l); 44  } 45  } 46  } 47  } 48 49 printf ("Case #%d:\n", ++cas); 50 printf ("%d\n", V.size ()); 51 for (int i=0; i<V.size (); ++i) 52  { 53 printf ("%I64d%c", V[i], (i == V.size ()-1) ? '\n' : ' '); 54  } 55  } 56 57 return 0; 58 } 59 60 61 /* 62 3 63 3.1312 64 3.1215 65 0.3 66 */

 

你可能感兴趣的:(百度)