标签:
题目链接:http://acm.swust.edu.cn/problem/137/
10 11 190000 960000 2 |
191919
383838
575757
767676
959595
|
1 #include<iostream> 2 using namespace std; 3 int x, y, a, b, k; 4 short Hash[10000001];//占两个字节,减少内存 5 int Len(int x, int k){ 6 int cnt = 0; 7 while (x){ 8 x /= k; 9 ++cnt; 10 } 11 return cnt; 12 } 13 int make(int a, int b, int len, int k){ 14 //在k进制范围下计算构造波浪数的值 15 int x = 0, i; 16 for (i = 1; i <= len; i++){ 17 if (i & 1) 18 x = x*k + a; 19 else 20 x = x*k + b; 21 } 22 return x; 23 } 24 void Search(int k){ 25 int l = Len(a, k), r = Len(b, k), i, j, u; 26 //数字首位不为零,构造波浪数,减少计算次数 27 for (i = 1; i < k; i++) 28 for (j = 0; j < k; j++){ 29 if (i == j) continue; 30 for (u = l; u <= r; u++){ 31 int num = make(i, j, u, k); 32 if (num >= a&&num <= b) Hash[num]++; 33 } 34 } 35 } 36 int main(){ 37 cin >> x >> y >> a >> b >> k; 38 for (int i = x; i <= y; i++) 39 Search(i); 40 for (int i = a; i <= b; i++) 41 if (Hash[i] == k) 42 cout << i << endl; 43 return 0; 44 }
[Swust OJ 137]--波浪数(hash+波浪数构造)
标签:
原文地址:http://www.cnblogs.com/zyxStar/p/4584193.html