220 likes | 466 Views
10624 - Super Number. 99703005 資科二 謝濟謙 99703030 資科二 薛元昊 99703038 資科二 鄭凱原. Outline. Problem Analyze Solution. Problem - Super Number.
E N D
10624 - Super Number 99703005 資科二 謝濟謙 99703030 資科二 薛元昊 99703038 資科二 鄭凱原
Outline • Problem • Analyze • Solution
Problem - Super Number • Don't you think 162456723 very special? Look at the picture below if you are unable to find its speciality. (a | b means ‘b is divisible by a’) • Figure: Super Numbers
Problem - Super Number • Input: Standard Input • Output: Standard Output • Time Limit: 3 Seconds • Givenn, m (0 < n < m < 30), you are to find a m-digit positive integer X such that for every i (n <= i <= m), the first i digits of X is a multiple of i. If more than one such X exists, you should output the lexicographically smallest one. Note that the first digit of X should not be 0.
Problem - Super Number • Input • The first line of the input contains a single integer t(1 <= t <= 15), the number of test cases followed. For each case, two integers n and m are separated by a single space. • Output • For each test case, print the case number and X. If no such number, print -1. • Sample Input Output for Sample Input
Analyze • Example: • Input : 1 5 15 • Output:
Analyze • 大數運算: • 基於記憶體的有效運用,程式語言中規定了各種不同的資料型態,也因此變數所可以表達的最大整數受到限制,例如123456789123456789這樣的 整數就不可能儲存在long變數中(例如C/C++等),我們稱這為long數,這邊翻為超長整數(避免與資料型態的長整數翻譯混淆),或俗稱大數運算。
Analyze • 就C語言來看,long longint已經是最大的整數型態了! • (long longint型態的數值,範圍是:-264-1~ 264-1-1)
Analyze 這個是長除法,而以下所解釋的就是 ”大數除法”的實現
Analyze current depth x = 0 remain = ( remain * 10 + a[x] ) % Divisor =>( 0 * 10 + 3 ) % 195 = 3 remain = 3
Analyze current depth x = 1 =>( 3 * 10 + 0 ) % 195 = 30 remain = 30 current depth x = 2 =>( 30 * 10 + 1 ) % 195 = 106 remain = 106 current depth x = 3 =>( 106 * 10 + 0 ) % 195 = 85 remain = 85
Analyze current depth x = 4 remain = 74 =>( 85 * 10 + 4 ) % 195 = 74 current depth x = 5 remain = 163 =>( 74 * 10 + 8 ) % 195 = 163 Answer is 163
Analyze 1. intmain(void) { 2. int times = 0, count; 3. scanf(“%d”, ×); //輸入幾筆測資 4. for (count = 0; count < times; ++count) { 5. scanf(“%d %d”, &n, &m); // 輸入起始數字的總位數與結束數字的總位數 6. printf(“Case %d: ”, count + 1); //當該筆測資之答案找到時輸出 7. if(superNum(0)) { // 8. for (inti = 0; i < m; ++i) // 9. printf(“%d”, superNumber[i]); // 依位數依序印出答案 10. printf("\n"); // 11. } // 12. else 13. printf(“-1\n”); // 若該筆測資沒有符合的答案,則輸出-1 14. } 15. return 0; // 程式結束 16. }
Analyze 1. boolbigNumberMod(int depth) { 2. int remain = 0; 3. for (inti = 0; i < depth; ++i) 4. remain = (remain * 10 + superNumber[i]) % depth;// 引用大數運算方法計算 5. return !remain; • } 8. boolsuperNum(int depth) { 9. if (depth == m) 10. return true; • for (inti = 0; i < 10; ++i) { • if (depth == 0 && I == 0) ++i; 13. superNumber[depth] = i; 14. if (((depth < (n - 1)) || bigNumberMod(depth + 1)) && superNum(depth + 1)) //遞迴方法 15. return true; 16. } 17. return false; 18. }
Better Solution #include<stdio.h> int n, m, superNumber[30]; boolbigNumberMod(int depth) { int remain = 0; for (inti = 0; i < depth; ++i) remain = (remain * 10 + superNumber[i]) % depth; return !remain; } boolsuperNum(int depth) { if (depth == m) return true; for (inti = 0; i < 10; ++i) { superNumber[depth] = i; if (((depth < (n - 1)) || bigNumberMod(depth + 1)) && superNum(depth + 1)) return true; } return false; }
Better Solution int main(void) { int times = 0, count; scanf("%d", ×); for (count = 0; count < times; ++count) { bool found = false; scanf("%d %d", &n, &m); for (inti = 1; i < 10; ++i) { superNumber[0] = i; if (superNum(1)) {found = true; break;} } printf("Case %d: ", count + 1); if(found) { for (inti = 0; i < m; ++i) printf("%d", superNumber[i]); printf("\n"); } else printf("-1\n"); } return 0; }
Solution • Time complexipy • T = O(m^2) Approaching to TLE? ↑ (3s)
Solution Approaching to TLE ↑ (3s)
Solution <Data Base> Int main(){ int times, m, n, i; scanf("%d", ×); for( i = 1 TO times ){ scanf("%d %d", &n, &m); cout<<"Case "<<i<<": "; if( m == 29 ){ if( n < 6 )cout<<“-1”<<endl; else if( n == 7 )cout<<“out29[1]”<<endl; else if( n == 8 To 28 ) … } else if( m == 28 To 2 ) … } return 0; } string out29[23] = {"39208896001212006680167258804","18555536703606084000060008482","10211576408418088880443254681","10101279606082569080443254680","10015192000842085860660000924","10000351372830084060967250126","10000028419290081060024006085","10000003819134081660600004206","10000000299310567240867250403","10000000177423525880167258806","10000000012965287440500008129","10000000000552924880760008767","10000000000010730620467258241","10000000000000635540300008128","10000000000000024060207252565","10000000000000000485667250123","10000000000000000135423252801","10000000000000000007244006366","10000000000000000001503258209","10000000000000000000006750880","10000000000000000000000224923","10000000000000000000000141200","10000000000000000000000000086"};
Solution <Data Base> • Time complexipy • T = O(1) 2.960/0.008 = 370
End Thanks for your listening!