Pentium.Labs

System全家桶:https://zhuanlan.zhihu.com/c_1238468913098731520

导航

bc#54 div2

用小号做的div2

 

A:竟然看错了排序顺序。。。白白WA了两发

注意读入一整行(包括空格):getline(cin,st)    【gets也是资瓷的

 1 #include<iostream>
 2 using namespace std;
 3 string s[1000];
 4 int d[1000];
 5 int T,N;
 6 
 7 void qsort(int l,int r)
 8 {
 9     int i=l,j=r;
10     int m=(l+r)/2;
11     int mid=d[m];
12     while(i<j)
13     {
14         while(d[i]<mid)    i++;
15         while(d[j]>mid)    j--;
16         if(i<=j)
17         {
18             int tmp=d[i];
19             d[i]=d[j];
20             d[j]=tmp;
21             string ts=s[i];
22             s[i]=s[j];
23             s[j]=ts;
24             i++;
25             j--;
26         }
27     }
28     if(l<j)    qsort(l,j);
29     if(i<r)    qsort(i,r);
30 }
31 
32 int main()
33 {
34     string st;
35     cin>>T;
36     while(T--)
37     {
38         cin>>N;  
39         for(int i=0;i<=N;i++)
40         {
41             getline(cin,st);
42             //cin>>s[i]>>d[i];
43             int tl=st.length();
44             int la=st[tl-1]-'0';
45             int lb=st[tl-2]-'0';
46             int lc=st[tl-3]-'0';
47             int ld=st[tl-4]-'0';
48             d[i]=ld*1000+lc*100+lb*10+la;
49             //cout<<i<<" "<<d[i]<<endl;
50             s[i]=st;
51         }
52 
53         qsort(1,N);
54         for(int i=N;i>=1;i--)
55         {
56             //cout<<s[i]<<endl;
57             int tl=s[i].length();
58             string tms=s[i];
59             for(int j=0;j<=tl-6;j++)
60                 cout<<tms[j];
61             cout<<endl;
62         }
63     }
64 }
View Code

 

B:自己YY的方法

其实就是找乘积的最小非质数因子

先判断无解的情况:所有数都是1 or 只有一个非1的数且是质数,剩下的都是1

若有解:对每个a[i]都分解质因子,找出每个a[i]最小的和第二小的质因子

然后把所有的质因子们放一起排序,取最小的两个相乘既是解。

  1 #include<iostream>
  2 #include<algorithm>
  3 #include<cstring>
  4 #include<ctime>
  5 using namespace std;
  6 #define LL long long
  7 int T,N;
  8 LL a[110];
  9 LL sm[100100];
 10 
 11 //**********************************************
 12 // pollard_rho算法进行质因素分解
 13 //*********************************************
 14 long long factor[100];//质因素分解结果(刚返回时是无序的)
 15 //注意factor里是每个质因数。比如N=8,那么factor[0..2]={2,2,2}   (2^3=8)
 16 int tol;//质因素的个数,编号0~tol-1
 17 
 18 
 19 const int S = 8; //随机算法判定次数,一般8~10就够了
 20 //计算ret  = (a*b)%c
 21 long long mult_mod(long long a,long long b,long long  c)
 22 {
 23     a %= c;
 24     b %= c;
 25     long long ret = 0;
 26     long long tmp = a;
 27     while(b)
 28     {
 29         if(b & 1)
 30         {
 31             ret += tmp;
 32             if(ret > c)ret -= c;//直接取模慢很多
 33         }
 34         tmp <<= 1;
 35         if(tmp > c)tmp -= c;
 36         b >>= 1;
 37     }
 38     return  ret;
 39 }
 40 //计算  ret = (a^n)%mod
 41 long long pow_mod(long long a,long long n,long long  mod)
 42 {
 43     long long ret = 1;
 44     long long temp = a%mod;
 45     while(n)
 46     {
 47         if(n & 1)ret = mult_mod(ret,temp,mod);
 48         temp = mult_mod(temp,temp,mod);
 49         n >>= 1;
 50     }
 51     return  ret;
 52 }
 53 //通过  a^(n-1)=1(mod n)来判断n是不是素数
 54 // n-1 = x*2^t中间使用二次判断
 55 //是合数返回true,不一定是合数返回false
 56 bool check(long long a,long long n,long long x,long long  t)
 57 {
 58     long long ret = pow_mod(a,x,n);
 59     long long last = ret;
 60     for(int i = 1; i <= t; i++)
 61     {
 62         ret = mult_mod(ret,ret,n);
 63         if(ret == 1 && last != 1 && last != n-1)return  true;//合数
 64         last = ret;
 65     }
 66     if(ret != 1)return true;
 67     else return false;
 68 }
 69 //**************************************************
 70 bool Miller_Rabin(long long  n)
 71 //是素数返回true,(可能是伪素数)
 72 //不是素数返回false
 73 {
 74     if(n==1)  return true;
 75     if( n < 2)return false;
 76     if( n == 2)return true;
 77     if( (n&1) == 0)return false;//偶数
 78     long long x = n - 1;
 79     long long t = 0;
 80     while( (x&1)==0 )
 81     {
 82         x >>= 1;
 83         t++;
 84     }
 85     rand();/* *************** */
 86     for(int i = 0; i < S; i++)
 87     {
 88         long long a =  rand()%(n-1) + 1;
 89         if( check(a,n,x,t) )
 90             return false;
 91     }
 92     return true;
 93 }
 94 
 95 
 96 long long gcd(long long a,long long  b)
 97 {
 98     long long  t;
 99     while(b)
100     {
101         t = a;
102         a = b;
103         b = t%b;
104     }
105     if(a >= 0)return  a;
106     else return  -a;
107 }
108 //出一个因子
109 long long pollard_rho(long long x,long long  c)
110 {
111     long long i = 1, k = 2;
112     srand(time(NULL));
113     long long x0 = rand()%(x-1) + 1;
114     long long y = x0;
115     while(1)
116     {
117         i ++;
118         x0 = (mult_mod(x0,x0,x) + c)%x;
119         long long d = gcd(y - x0,x);
120         if( d != 1 && d != x)return  d;
121         if(y == x0)return  x;
122         if(i == k)
123         {
124             y = x0;
125             k += k;
126         }
127     }
128 }
129 //对 n进行素因子分解,存入factor. k设置为107左右即可
130 void findfac(long long n,int  k)
131 {
132     if(n == 1)return;
133     if(Miller_Rabin(n))
134     {
135         factor[tol++] = n;
136         return;
137     }
138     long long p = n;
139     int c = k;
140     while( p >= n)
141         p = pollard_rho(p,c--);//值变化,防止死循环k
142     findfac(p,k);
143     findfac(n/p,k);
144 }
145 
146 
147 
148 int main()
149 {
150     cin>>T;
151     while(T--)
152     {
153         cin>>N;
154         int acc=0,occ=0;
155         for(int i=1;i<=N;i++)
156         {
157             cin>>a[i];
158             if(Miller_Rabin(a[i]))     acc++;
159             if(a[i]==1)                 occ++;
160         }
161         if(((occ==N-1)&&(acc==1))||(occ==N))
162             cout<<"-1"<<endl;
163         else
164         {
165             int tot=0;
166             for(int i=1;i<=N;i++)
167             {
168                 tol = 0;
169                 memset(factor,0,sizeof(factor));
170                 findfac(a[i],107);
171                 //此时factor[0..tol-1]存放的就是所有质因数(无序)
172                 sort(factor,factor+tol);
173                 //cout<<a[i]<<" "<<tol<<endl;
174                 //cout<<factor[0]<<factor[1]<<factor[2]<<endl;
175                 if(factor[0]!=0)
176                 {
177                     tot++;
178                     sm[tot]=factor[0];
179                 }            
180                 if(factor[1]!=0)
181                 {
182                     tot++;
183                     sm[tot]=factor[1];
184                 }
185             }
186             sort(sm+1,sm+tot+1);
187             cout<<sm[1]*sm[2]<<endl;
188         }
189 
190 
191     }
192 }
View Code

 

posted on 2015-09-06 16:55  Pentium.Labs  阅读(214)  评论(0编辑  收藏  举报



Pentium.Lab Since 1998