Topcoder SRM 652 DIV1 250

题意:大概是求前n个数的最大公倍数。

解题思路:筛法+质因子分解,敲玩就去睡觉了,没想到没优化被cha了。。。

解题代码:

  1 // BEGIN CUT HERE
  2 /*
  3 
  4 */
  5 // END CUT HERE
  6 #line 7 "ThePermutationGame.cpp"
  7 #include <cstdlib>
  8 #include <cctype>
  9 #include <cstring>
 10 #include <cstdio>
 11 #include <cmath>
 12 #include <algorithm>
 13 #include <vector>
 14 #include <string>
 15 #include <iostream>
 16 #include <sstream>
 17 #include <map>
 18 #include <set>
 19 #include <queue>
 20 #include <stack>
 21 #include <fstream>
 22 #include <numeric>
 23 #include <iomanip>
 24 #include <bitset>
 25 #include <list>
 26 #include <stdexcept>
 27 #include <functional>
 28 #include <utility>
 29 #include <ctime>
 30 using namespace std;
 31 
 32 #define PB push_back
 33 #define MP make_pair
 34 
 35 #define REP(i,n) for(i=0;i<(n);++i)
 36 #define FOR(i,l,h) for(i=(l);i<=(h);++i)
 37 #define FORD(i,h,l) for(i=(h);i>=(l);--i)
 38 
 39 typedef vector<int> VI;
 40 typedef vector<string> VS;
 41 typedef vector<double> VD;
 42 typedef long long LL;
 43 typedef pair<int,int> PII;
 44 #define maxn 1000005
 45 #define M 1000000007
 46 int hs[maxn];
 47 int a[maxn];
 48 int t = 0 ; 
 49 int solve()
 50 {
 51    for(int i = 2;i <= sqrt(100000);i ++)
 52    {
 53       if(hs[i] == 0)
 54       {
 55          int k = i * i ; 
 56          while(k <= 100000)
 57          {
 58             hs[k] = 1;
 59             k = k + i ; 
 60          }
 61       }
 62    }
 63    for(int i = 2;i <= 100000;i ++)
 64    {
 65       if(hs[i] == 0 )
 66       {
 67          t ++;
 68          a[t] = i ; 
 69       }
 70    }
 71 
 72 }
 73 int ans[10000];
 74 int tmp[10000];
 75 LL P(LL x,LL y)
 76 {
 77   if(y == 0 )
 78       return 1;
 79   LL ttt = P(x,y/2);
 80   if(y%2 == 0 )
 81       return ttt*ttt % M;
 82   else return ((ttt*ttt)%M)*x%M;
 83 }
 84 class ThePermutationGame
 85 {
 86         public:
 87         int findMin(int N)
 88         {
 89           memset(hs,0,sizeof(hs));
 90           t = 0 ; 
 91           solve();
 92           printf("%d
",t);
 93           for(int i = 1;i <= N;i++)
 94           {
 95              memset(tmp,0,sizeof(tmp));
 96              int k = i ;
 97              int tt = 1 ;
 98              while(k!=1)
 99              {
100                 if(k %a[tt] == 0 )
101                 {
102                   k = k/a[tt];
103                   tmp[tt] ++ ; 
104                 }else{
105                    tt ++ ; 
106                 }
107              }
108              for(int j = 1; j <= tt;j ++)
109              {
110                ans[j] = max(ans[j],tmp[j]);
111              }
112           }
113           LL sum = 1; 
114           for(int j = 1;j <= t; j ++)
115           {
116              sum = sum * P(a[j],ans[j]) % M;
117           }
118           return sum; 
119         }
120         
121 // BEGIN CUT HERE
122     public:
123     void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
124     private:
125     template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '"' << *iter << "","; os << " }"; return os.str(); }
126     void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "	Expected: "" << Expected << '"' << endl; cerr << "	Received: "" << Received << '"' << endl; } }
127     void test_case_0() { int Arg0 = 2; int Arg1 = 2; verify_case(0, Arg1, findMin(Arg0)); }
128     void test_case_1() { int Arg0 = 3; int Arg1 = 6; verify_case(1, Arg1, findMin(Arg0)); }
129     void test_case_2() { int Arg0 = 11; int Arg1 = 504; verify_case(2, Arg1, findMin(Arg0)); }
130     void test_case_3() { int Arg0 = 102; int Arg1 = 841777601; verify_case(3, Arg1, findMin(Arg0)); }
131     void test_case_4() { int Arg0 = 9999; int Arg1 = 804862568; verify_case(4, Arg1, findMin(Arg0)); }
132 
133 // END CUT HERE
134 
135 };
136 
137 // BEGIN CUT HERE
138 int main()
139 {
140         ThePermutationGame ___test;
141         ___test.run_test(-1);
142         return 0;
143 }
144 // END CUT HERE
View Code
没有梦想,何谈远方
原文地址:https://www.cnblogs.com/zyue/p/4324989.html