1. 程式人生 > >P2429 制杖題

P2429 制杖題

iostream rim rime 最大 algorithm names n) gist clas

P2429 制杖題
這個題用線性篩會WA一個點,因為這個題是給定的質數集,最大的質數會比當前的倍數大,就會出現上面的情況。
怎辦?
判重用set啊!
set+線性篩就過掉了。16ms

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cmath>
 6 #include<set>
 7 #include<ctime>
 8 #include<cstring>
 9 #define
mod 376544743 10 #define inf 2147483647 11 #define For(i,a,b) for(register long long i=a;i<=b;i++) 12 #define p(a) putchar(a) 13 #define g() getchar() 14 //by war 15 //2017.10.19 16 using namespace std; 17 long long n,m; 18 long long prime[100]; 19 long long tot; 20 set<long long>s; 21 void in(long long
&x) 22 { 23 long long y=1; 24 char c=g();x=0; 25 while(c<0||c>9) 26 { 27 if(c==-) 28 y=-1; 29 c=g(); 30 } 31 while(c<=9&&c>=0)x=x*10+c-0,c=g(); 32 x*=y; 33 } 34 void o(long long x) 35 { 36 if(x<0) 37 { 38 p(-);
39 x=-x; 40 } 41 if(x>9)o(x/10); 42 p(x%10+0); 43 } 44 45 void Euler() 46 { 47 For(i,1,m/prime[1]) 48 { 49 for(int j=1;j<=n&&prime[j]*i<=m;j++) 50 { 51 if(!s.count(prime[j]*i)) 52 { 53 tot=(tot+prime[j]*i)%mod; 54 s.insert(prime[j]*i); 55 } 56 if(i%prime[j]==0) 57 break; 58 } 59 } 60 } 61 62 int main() 63 { 64 65 in(n),in(m); 66 For(i,1,n) 67 in(prime[i]); 68 sort(prime+1,prime+n+1); 69 Euler(); 70 o(tot%mod); 71 return 0; 72 }

P2429 制杖題