关于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

posted @ 2016-08-30 22:49  Chen_Xiaoyu  阅读(3471)  评论(1)    收藏  举报