CCSU2023暑期结训测试赛(一)


前言

头脑清醒,实力才是硬道理

A-666

在这里插入图片描述
#解题思路:对每一个数进行质因数分解,得到每个质因数所有的下标索引,再进行二分查找,得到质因数x在范围[l,r]中的所占个数(不料这题是被自己害了,认错了原题,唉)

#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
const ll Mod = 1e9 + 7;
const ll mod9 = 998244353;
const ll Max = 1e5 + 7;
const ll MAX = 2e5 + 100;
const ll N = 2e5 + 10;
const ll inf = 1e12 + 100;
const double pi=acos(-1.0);
const double E  = exp(1);
const double eps=1e-8;
//char buf[1<<20],*p1,*p2;
#define nl "\n"
#define fir first
#define sec second
//#define re register
//#define int ll
//#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf,1,1<<20,stdin), p1 == p2) ? 0 : *p1++)
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define setbits1(x, n, m) (x) | ((~(0U)<<(sizeof(x)-(m-n+1)))>>n)
#define setbits0(x, n, m) (x) & ~((~(0U)<<(sizeof(x)-(m-n+1)))>>n)
#define getbits(x, n, m) (x) & ((~(0U)<<(sizeof(x)-(m-n+1)))>>n)
#define get_reverse_bits(x, n, m) (x) ^ ((~(0U)<<(sizeof(x)-(m-n+1)))>>n)
#define pii pair<int,int>
#define pll pair<long long,long long>
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;

inline ll read(){
    ll x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch>'9'){
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9'){
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}

template<typename I,typename L,I mod> struct Modint {
    I v;
    I pow(L b) const {
        L res=1,a=v;
        while(b) { if(b&1) res=res*a%mod; b>>=1; a=a*a%mod; }
        return res;
    }
    I inv() const { return pow(mod-2); }

    Modint &operator+=(const Modint &x) { v+=x.v; v-=v>=mod?mod:0; return *this; }
    Modint &operator-=(const Modint &x) { v-=x.v; v+=v<0?mod:0; return *this; }
    Modint &operator*=(const Modint &x) { v=L(1)*v*x.v%mod; return *this; }
    Modint &operator/=(const Modint &x) { v=L(1)*v*x.inv()%mod; return *this; }

    friend Modint operator+(Modint l,const Modint &r) { return l+=r; }
    friend Modint operator-(Modint l,const Modint &r) { return l-=r; }
    friend Modint operator*(Modint l,const Modint &r) { return l*=r; }
    friend Modint operator/(Modint l,const Modint &r) { return l/=r; }

    Modint operator++(int) { auto res=*this; *this+=1; return res; }
    Modint operator--(int) { auto res=*this; *this-=1; return res; }
    Modint operator-  () { return *this*-1; }
    Modint &operator++() { return *this+=1; }
    Modint &operator--() { return *this-=1; }

    bool operator< (const Modint &x) const { return v< x.v; }
    bool operator> (const Modint &x) const { return v> x.v; }
    bool operator<=(const Modint &x) const { return v<=x.v; }
    bool operator>=(const Modint &x) const { return v>=x.v; }
    bool operator==(const Modint &x) const { return v==x.v; }
    bool operator!=(const Modint &x) const { return v!=x.v; }

    friend istream &operator>>(istream &is,Modint &x) { is>>x.v; x=Modint(x.v); return is; }
    friend ostream &operator<<(ostream &os,const Modint &x) { return os<<x.v; }

    Modint(L x=0): v((x%=mod)<0?x+mod:x) {}
    static_assert(0ULL+mod+mod-2<1ULL<<(sizeof(I)*8-1), "Modint overflow");
    static_assert(1ULL*(mod-1)*(mod-1)<1ULL<<(sizeof(L)*8-1), "Modint overflow");
};
using mint=Modint<int,long long,Mod>;
char arr[1000][1000];
ll dp[N][2],cntn[26],dt[N];
ll suma[Max],sumb[Max],sum[N];
int to[N];
string s;
char chs[27];
int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};
const int dxn[8] = { -1,-2,-2,-1,1,2,2,1 };
const int dyn[8] = { 2,1,-1,-2,2,1,-1,-2 };
vector<int>e[N];
map<int,vector<int>>mp;
//mp<pii,int>mp;
ll n,m,k;

bool check(int x,int y){
    return x>=1&&x<=n&&y>=1&&y<=m;
}

void solved(){
    int i,j;
}

void init(int x,int pos){
    //ios;
    int i,j;
    for (i = 2; i * i <= x; i++) { //循环查找判断质因数
        bool flag=false;
        while ((x % i) == 0)  {  //若i为num的质因数,则输出i
            //cout << i;
            x /= i;    //对num除以i后的数求取质因数
            flag=true;
            //if (x != 1)//判断num是否除尽 
                //cout << "*";
        }
        
        if(flag)
            mp[i].push_back(pos);
    }
    
    mp[x].push_back(pos);
}

int main() {
    ios;
    //cout << fixed;
    //cout.precision(18);
    //cout.flush();
    int i, j;
    int T,u,v,w,t,g,h,t1,t2,p,q,op,mx,mn,x,y,z,l,r,mid,pos,now,idx,dis,len,all,ave,cnt,cur,last,sumn,other;
    string str,s1,s2,s3,te1,te2,sf,sk;
    char ch,ch1,ch2;
    bool flag,f1,f2,f3,cle;
    suma[0]=0;sumb[0]=0;sum[0]=0;
    T=1;
    //cin>>T;
    while(T--) {
        //solved();
        cin>>n;
        for(i=1;i<=n;i++){
            cin>>x;
            //if(x==1)
                //mp[1].push_back(i);//避免加两次位置
            //else
            init(x,i);
        }
        cin>>m;
        while(m--){
            cin>>l>>r>>x;
            /*if(x>r){
                cout<<"666666!"<<nl;
                continue;
            }*/
            
            u=lower_bound(mp[x].begin(),mp[x].end(),l)-mp[x].begin();
            v=upper_bound(mp[x].begin(),mp[x].end(),r)-mp[x].begin();
            //v=upper_bound(mp[x].begin(),mp[x].end(),r)-mp[x].begin()-1;
            
            //cout<<u<<" "<<v<<nl;
            
            if(u>=v)
                cout<<"666666!"<<nl;
            else
                cout<<v-u<<nl;
        }
    }

    //system("pause");
    return 0;
}

/*int lt=0,rt=mp[x].size()-1;
            while(lt<rt){
                mid=(lt+rt)>>1;
                if(mp[x][mid]>=l) rt=mid;
                else lt=mid+1;
            }
            u=rt;
            
            
            lt=0,rt=mp[x].size()-1;
            while(lt<rt){
                mid=(lt+rt)>>1;
                if(mp[x][mid]>=r) rt=mid;
                else lt=mid+1;
            }
            v=rt;
            if(mp[x][v]==r)
                v++;
*/

B-求和

在这里插入图片描述

#题目思路:查找规律,得知规律为n*(n+1)/2(div3/div4 A题常见)

#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
const ll Mod = 1e9 + 7;
const ll mod9 = 998244353;
const ll Max = 1e6 + 7;
const ll MAX = 1e3 + 100;
const ll N = 2e5 + 10;
const ll inf = 1e12 + 100;
const double pi=acos(-1.0);
const double eps=1e-8;
//char buf[1<<20],*p1,*p2;
#define nl "\n"
#define fir first
#define sec second
//#define re register
//#define int ll
//#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf,1,1<<20,stdin), p1 == p2) ? 0 : *p1++)
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define setbits1(x, n, m) (x) | ((~(0U)<<(sizeof(x)-(m-n+1)))>>n)
#define setbits0(x, n, m) (x) & ~((~(0U)<<(sizeof(x)-(m-n+1)))>>n)
#define getbits(x, n, m) (x) & ((~(0U)<<(sizeof(x)-(m-n+1)))>>n)
#define get_reverse_bits(x, n, m) (x) ^ ((~(0U)<<(sizeof(x)-(m-n+1)))>>n)
#define pii pair<int,int>
#define pll pair<long long,long long>
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;

inline ll read(){
    ll x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch>'9'){
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9'){
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}

template<typename I,typename L,I mod> struct Modint {
    I v;
    I pow(L b) const {
        L res=1,a=v;
        while(b) { if(b&1) res=res*a%mod; b>>=1; a=a*a%mod; }
        return res;
    }
    I inv() const { return pow(mod-2); }

    Modint &operator+=(const Modint &x) { v+=x.v; v-=v>=mod?mod:0; return *this; }
    Modint &operator-=(const Modint &x) { v-=x.v; v+=v<0?mod:0; return *this; }
    Modint &operator*=(const Modint &x) { v=L(1)*v*x.v%mod; return *this; }
    Modint &operator/=(const Modint &x) { v=L(1)*v*x.inv()%mod; return *this; }

    friend Modint operator+(Modint l,const Modint &r) { return l+=r; }
    friend Modint operator-(Modint l,const Modint &r) { return l-=r; }
    friend Modint operator*(Modint l,const Modint &r) { return l*=r; }
    friend Modint operator/(Modint l,const Modint &r) { return l/=r; }

    Modint operator++(int) { auto res=*this; *this+=1; return res; }
    Modint operator--(int) { auto res=*this; *this-=1; return res; }
    Modint operator-  () { return *this*-1; }
    Modint &operator++() { return *this+=1; }
    Modint &operator--() { return *this-=1; }

    bool operator< (const Modint &x) const { return v< x.v; }
    bool operator> (const Modint &x) const { return v> x.v; }
    bool operator<=(const Modint &x) const { return v<=x.v; }
    bool operator>=(const Modint &x) const { return v>=x.v; }
    bool operator==(const Modint &x) const { return v==x.v; }
    bool operator!=(const Modint &x) const { return v!=x.v; }

    friend istream &operator>>(istream &is,Modint &x) { is>>x.v; x=Modint(x.v); return is; }
    friend ostream &operator<<(ostream &os,const Modint &x) { return os<<x.v; }

    Modint(L x=0): v((x%=mod)<0?x+mod:x) {}
    static_assert(0ULL+mod+mod-2<1ULL<<(sizeof(I)*8-1), "Modint overflow");
    static_assert(1ULL*(mod-1)*(mod-1)<1ULL<<(sizeof(L)*8-1), "Modint overflow");
};
using mint=Modint<int,long long,Mod>;
char arr[N],f[150][150];
ll vis[N],date[N],dp[N],cntn[26];
ll suma[Max],sumb[Max],sum[N];
char chs[27];
string s;
int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};
vector<int>e[N];

//map<pii,int>mp;
int n,m;

int main() {
    //ios;
    //cout << fixed;
    //cout.precision(18);
    //cout.flush();
    int i, j;
    ll T,k,u,v,t,g,h,t1,t2,p,q,op,mx,mn,x,y,z,l,r,len,mid,pos,idx,dis,all,ave,cnt,last,sumn,other;
    string str,s1,s2,s3,te1,te2,sf,sk;
    char ch,ch1,ch2;
    bool flag,f1,f2,f3,cle;
    suma[0]=0;sumb[0]=0;
    T=1;
    cin>>T;
    while(T--) {
        cin>>n;
        ll ans=n*(n+1ll)/2;
        cout<<ans<<nl;
    }

    //system("pause");
    return 0;
}

C-迷宫

在这里插入图片描述
#题目思路:BFS搜索,设初始数now为1,每次搜索过后判断是否经过(now+1),即是否能从now走至now+1,若经过则now++,不然则判定不能走出迷宫,若最后now==n*m,则可以走出迷宫

#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
const ll Mod = 1e9 + 7;
const ll mod9 = 998244353;
const ll Max = 1e5 + 7;
const ll MAX = 2e5 + 100;
const ll N = 2e5 + 10;
const ll inf = 1e12 + 100;
const double pi=acos(-1.0);
const double E  = exp(1);
const double eps=1e-8;
//char buf[1<<20],*p1,*p2;
#define nl "\n"
#define fir first
#define sec second
//#define re register
//#define int ll
//#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf,1,1<<20,stdin), p1 == p2) ? 0 : *p1++)
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define setbits1(x, n, m) (x) | ((~(0U)<<(sizeof(x)-(m-n+1)))>>n)
#define setbits0(x, n, m) (x) & ~((~(0U)<<(sizeof(x)-(m-n+1)))>>n)
#define getbits(x, n, m) (x) & ((~(0U)<<(sizeof(x)-(m-n+1)))>>n)
#define get_reverse_bits(x, n, m) (x) ^ ((~(0U)<<(sizeof(x)-(m-n+1)))>>n)
#define pii pair<int,int>
#define pll pair<long long,long long>
#define rep(i,a,n) for (int i=a;i<=n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
#pragma comment (linker,"/STACK:102400000,102400000")
using namespace std;

inline ll read(){
    ll x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' || ch>'9'){
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9'){
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f;
}

template<typename I,typename L,I mod> struct Modint {
    I v;
    I pow(L b) const {
        L res=1,a=v;
        while(b) { if(b&1) res=res*a%mod; b>>=1; a=a*a%mod; }
        return res;
    }
    I inv() const { return pow(mod-2); }

    Modint &operator+=(const Modint &x) { v+=x.v; v-=v>=mod?mod:0; return *this; }
    Modint &operator-=(const Modint &x) { v-=x.v; v+=v<0?mod:0; return *this; }
    Modint &operator*=(const Modint &x) { v=L(1)*v*x.v%mod; return *this; }
    Modint &operator/=(const Modint &x) { v=L(1)*v*x.inv()%mod; return *this; }

    friend Modint operator+(Modint l,const Modint &r) { return l+=r; }
    friend Modint operator-(Modint l,const Modint &r) { return l-=r; }
    friend Modint operator*(Modint l,const Modint &r) { return l*=r; }
    friend Modint operator/(Modint l,const Modint &r) { return l/=r; }

    Modint operator++(int) { auto res=*this; *this+=1; return res; }
    Modint operator--(int) { auto res=*this; *this-=1; return res; }
    Modint operator-  () { return *this*-1; }
    Modint &operator++() { return *this+=1; }
    Modint &operator--() { return *this-=1; }

    bool operator< (const Modint &x) const { return v< x.v; }
    bool operator> (const Modint &x) const { return v> x.v; }
    bool operator<=(const Modint &x) const { return v<=x.v; }
    bool operator>=(const Modint &x) const { return v>=x.v; }
    bool operator==(const Modint &x) const { return v==x.v; }
    bool operator!=(const Modint &x) const { return v!=x.v; }

    friend istream &operator>>(istream &is,Modint &x) { is>>x.v; x=Modint(x.v); return is; }
    friend ostream &operator<<(ostream &os,const Modint &x) { return os<<x.v; }

    Modint(L x=0): v((x%=mod)<0?x+mod:x) {}
    static_assert(0ULL+mod+mod-2<1ULL<<(sizeof(I)*8-1), "Modint overflow");
    static_assert(1ULL*(mod-1)*(mod-1)<1ULL<<(sizeof(L)*8-1), "Modint overflow");
};
using mint=Modint<int,long long,Mod>;
int arr[1000][1000];
ll dp[N][2],cntn[26],dt[N];
ll suma[Max],sumb[Max],sum[N];
int to[N];
string s;
char chs[27];
int dx[]={-1,0,1,0};
int dy[]={0,-1,0,1};
const int dxn[8] = { -1,-2,-2,-1,1,2,2,1 };
const int dyn[8] = { 2,1,-1,-2,2,1,-1,-2 };
vector<int>e[N];
//mp<pii,int>mp;
ll n,m,k;

bool check(int x,int y){
    return x>=1&&x<=n&&y>=1&&y<=m;
}

void solved(){
    int i,j;
}

int main() {
    ios;
    //cout << fixed;
    //cout.precision(18);
    //cout.flush();
    int i, j;
    ll T,u,v,w,t,g,h,t1,t2,p,op,mx,mn,x,y,z,l,r,mid,pos,now,idx,dis,len,all,ave,cnt,cur,last,sumn,other;
    string str,s1,s2,s3,te1,te2,sf,sk;
    char ch,ch1,ch2;
    bool flag,f1,f2,f3,cle;
    suma[0]=0;sumb[0]=0;sum[0]=0;
    T=1;
    cin>>T;
    while(T--) {
        //solved();
        cin>>n>>m;
        cnt=1;now=1;
        map<int,pii>posn;
        map<int,int>vis;
        for(i=1;i<=n;i++)
            for(j=1;j<=m;j++){
                cin>>arr[i][j];
                posn[arr[i][j]]={i,j};
            }
        vis[1]=1;
        queue<pii>q;
        q.push(posn[1]);
        
        while(!q.empty()){
            pii pt=q.front();
            q.pop();
            for(i=0;i<4;i++){
                x=pt.fir+dx[i];
                y=pt.sec+dy[i];
                if(check(x,y)&&!vis[arr[x][y]]){
                    //q.push({x,y});
                    vis[arr[x][y]]=1;
                }
            }
            
            if(vis[now+1]){
                now++;
                q.push({posn[now].fir,posn[now].sec});
            }
            else
                break;
        }
        if(now==n*m)
            cout<<"YES"<<nl;
        else
            cout<<"NO"<<nl;
    }

    //system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值