Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. // Test1.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #define _CRT_SECURE_NO_WARNINGS
  5. #include <iostream>
  6. #include <vector>
  7. #include <cstdio>
  8. #include <cassert>
  9.  
  10. #define A_MILLI 1000 * 1000
  11.  
  12. typedef long long ll_t;
  13.  
  14. using namespace std;
  15.  
  16. vector<int> euler_tot(const ll_t m) {
  17.     vector<int> tot(m + 1, 0);
  18.     tot[1] = 1;
  19.  
  20.     for (ll_t p = 2; p <= m; ) {
  21.         for (ll_t pk = p; pk <= m; pk += p) {
  22.             if (tot[pk] == 0) tot[pk] = 1;
  23.             tot[pk] *= (p - 1);
  24.         }
  25.  
  26.         for (ll_t pp = p * p; pp <= m; pp *= p)
  27.             for (ll_t pk = pp; pk <= m; pk += pp)
  28.                 tot[pk] *= p;
  29.  
  30.         do ++p;
  31.         while (p <= m && tot[p] != 0);
  32.     }
  33.  
  34.     return tot;
  35. }
  36.  
  37. inline string itos(const int n) {
  38. }
  39.  
  40. inline bool isPerm(const string& a, const string& b) {
  41.  
  42.     if (a.length() != b.length()) return false;
  43.  
  44.     int cnt[10] = { 0 };
  45.     for (int i = 0, n = a.length(); i < n; ++i)
  46.         cnt[a[i] - '0']++;
  47.  
  48.     for (int i = 0, n = b.length(); i < n; ++i)
  49.         if ( (--cnt[b[i] - '0']) < 0)
  50.             return false;
  51.  
  52.     return true;
  53. }
  54.  
  55. inline bool isLess(const ll_t a, const ll_t b, const ll_t c, const ll_t d) {
  56.     return b * c - d * a > 0;
  57. }
  58.  
  59.  #include <time.h>
  60.  #include <stdio.h>
  61.  #include <stdlib.h>
  62.  #include <windows.h>
  63.  #include <winbase.h>
  64.  
  65. int main2() {
  66.     const int max = 10 * A_MILLI;
  67.     vector<int> tot = euler_tot(max);
  68.    
  69.     int bestn; double bestr = 1e10;
  70.     #pragma omp parallel for schedule(static, 12)
  71.     for (int n = 8 * A_MILLI; n <= max; ++n) {
  72.         char ca[25], cb[25];
  73.         sprintf(ca, "%d", n);
  74.         sprintf(cb, "%d", tot.at(n));
  75.         string sa(ca), sb(cb);
  76.  
  77.         const double r = n / double(tot[n]);
  78.  
  79.         #pragma omp critical    
  80.         if (isPerm(sa, sb) && (r < bestr))
  81.             bestn = n, bestr = r;
  82.     }
  83.  
  84.     cout << bestn << endl;
  85.  
  86.     return 0;
  87. }
  88.  
  89. int main()
  90. {
  91.     int count,a;
  92. LARGE_INTEGER ticksPerSecond;
  93. LARGE_INTEGER tick;   // A point in time
  94. LARGE_INTEGER start_ticks, end_ticks, cputime;  
  95. if (!QueryPerformanceFrequency(&ticksPerSecond))
  96.  printf("\tno go QueryPerformance not present");
  97. printf ("\tfreq test:   %I64Ld ticks/sec\n",ticksPerSecond    );
  98.  // what time is it?
  99.   if (!QueryPerformanceCounter(&tick) ) printf("no go counter not installed");  
  100. printf ("\QueryPerformanceCounter testpoint :   %I64Ld  ticks\n",tick);
  101.  
  102. QueryPerformanceCounter(&start_ticks);
  103.   main2();
  104.  
  105. QueryPerformanceCounter(&end_ticks);
  106. cputime.QuadPart = end_ticks.QuadPart- start_ticks.QuadPart;
  107.  
  108. printf ("\tElapsed CPU time test:   %.9f  sec  ticks %d\n",
  109. ((float)cputime.QuadPart/(float)ticksPerSecond.QuadPart),cputime.QuadPart);
  110. getchar();
  111. return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement