关于memset()函数复杂度的问题

本文介绍了一种解决 Codeforces Round 369-D 的算法思路,使用了动态规划技巧来求解特定的数列问题,并讨论了 memset() 函数的使用对于时间效率的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

转载于:https://www.cnblogs.com/chenxiaoyu-blook/p/5823974.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值