codeforces 1017F
codeforces-1017F The Neutral Zone
这题内存限制只有16MB,考虑将2和3的倍数筛去,剩下的数只有1e8个,然后用bitset做标记数组,内存就只需要12MB.
标记的数字下标刚好是i/3.
#include<bits/stdc++.h> #define me(a,x) memset(a,x,sizeof(a)) #define IN freopen("in.txt","r",stdin); #define OUT freopen("out.txt","w",stdout); #define sc scanf #define itn int #define STR clock_t startTime = clock(); #define END clock_t endTime = clock();cout << double(endTime - startTime) / CLOCKS_PER_SEC *1000<< "ms" << endl; using namespace std; const int N=1e8+2; const long long mod=1e9+7; const long long mod2=998244353; const int oo=0x7fffffff; const int sup=0x80000000; typedef long long ll; typedef unsigned int ui; template <typename it>void db(it *begin,it *end){while(begin!=end)cout<<(*begin++)<<" ";puts("");} template <typename it> string to_str(it n){string s="";while(n)s+=n%10+'0',n/=10;reverse(s.begin(),s.end());return s;} template <typename it>int o(it a){cout<<a<<endl;return 0;} inline ll mul_64(ll x,ll y,ll c){return (x*y-(ll)((long double)x/c*y)*c+c)%c;} inline ll ksm(ll a,ll b,ll c){ll ans=1;for(;b;b>>=1,a=a*a%c)if(b&1)ans=ans*a%c;return ans;} inline void exgcd(ll a,ll b,ll &x,ll &y){if(!b)x=1,y=0;else exgcd(b,a%b,y,x),y-=x*(a/b);} bitset<N>bit; int n,A,B,C,D; ui cal(ui x){ return ui(x*x*x*A+x*x*B+x*C+D); } ui num(int x){ ll ans=0,m=n; while(m)ans+=m/=x; return ans; } int main(){ cin>>n>>A>>B>>C>>D; if(n==1)return o(0),0; ui ans=cal(2)*num(2)+cal(3)*num(3); for(int i=5;i<=n;i++){ if(i%2==0||i%3==0)continue; if(bit[i/3]==0){ ans+=cal(i)*num(i); for(int j=i;j<=n;j+=i){ if(j%2==0||j%3==0)continue; bit[j/3]=1; } } } o(ans); }