求M的N次方的後三位

 1 #include <cstdlib>
 2 #include <iostream>
 3 using namespace std;
 4 int main(int argc, char *argv[])
 5 {
 6     int x, y;
 7     cin >> x >> y;
 8     
 9     int result = 1;
10     for(int i=0; i<y; i++)
11         result = result * x % 1000;
12     
13     cout << result << endl;
14     
15     system("PAUSE");
16     return EXIT_SUCCESS;
17 }

當M, N很大時, M的N次方沒法用基本的數據類型表示...分析能夠發現, 乘積的最後三位只與乘數和被乘數的最後三位有關, 而與高位無關, 因此每次將乘積結果對1000取模便可...ios

相關文章
相關標籤/搜索