關於memset()函數復雜度的問題


memset()函數只是稍稍常數小一些而已,其復雜度任然是O(n)的。這一點需要稍稍注意。

代碼


#include 
  
  
  
          
            #include 
           
             #include 
            
              #define Mod 1000000007 #define maxn 200005 //#define mmm 1000000005 using namespace std; int n; int num; long long to[maxn]; long long use[maxn]; long long vis[maxn]; long long po[maxn]; long long ans = 1; void get(int x){ int whe = to[x]; int now = 1; while(whe != x){ now++; whe = to[whe]; } if(now <= 1) return; ans *= (po[now] - 2); ans %= Mod; num -= now; } void update(int x){ int whe = x; while(vis[whe] && !use[whe]){ use[whe] = 1; whe = to[whe]; // vis[whe] = 0; // cout << whe << " "; } //cout << endl; } void deal(int x){ //memset(vis,0,sizeof(vis)); int whe = x; while(!vis[whe] && !use[whe]) { vis[whe] = 1; whe = to[whe]; } if(!use[whe]) { get(whe); } update(x); } void work(){ for(int i = 1; i <= n; i++) if(!use[i]) { deal(i); } ans *= po[num]; ans %= Mod; cout << ans << endl; } void pre_work(){ po[0] = 1; for(int i = 1; i <= 200000; i ++) { po[i] = po[i-1] * 2; po[i] %= Mod; } } /*void show(){ for(int i = 1; i <= n; i++) cout << ci[i] << " "; cout << endl; }*/ int main(){ //freopen("main.in","r",stdin); ios::sync_with_stdio(false); pre_work(); cin >> n; num = n; for(int i = 1; i <= n; i++) cin >> to[i]; work(); // show(); return 0; } 
             
            
          

這里如果加入了memset()就會tle的飛起。

題目:codeforces round_369 - D


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM