牛客编程巅峰赛S1第5场 - 黄金&钻石&王者 ABC

56 旭日东升BJFU 北京林业大学 2 00:35:00

C卡mod了。不然就上王者了。。淦

A:

x*x=(x%1000)  * (x%1000);

所以1000以内就能包括所有

class Solution {
public:
    /**
     *
     * @param x int整型
     * @return bool布尔型
     */
 
    bool solve(int x) {
        // write code here
        int f[1010]={0};
        for(int i=1;i<=1000;i++)
            f[i*i%1000]=1;
        if(f[x])return true;
        return false;
    }
};

B:

直接k步拿出来,求个逆序对即可。

class Solution {
public:
    /**
     * 
     * @param s string字符串 s.size() <= 1e5
     * @param k int整型 k <= s.size()
     * @return int整型
     */
	int a[100007],b[27];
    int turn(string s, int k) {
        // write code here
        int n=s.length();
        int ans=0;
        for(int i=0;i<k;i++)
        {
        	memset(b,0,sizeof(b));
        	for(int j=i;j<n;j+=k)
        	{
        		int tp=s[j]-'a';
        //		cout<<i<<" "<<j<<" "<<tp<<endl;
        		for(int l=0;l<tp;l++)ans+=b[l];
        		b[tp]++;
			}
		}
		return ans;
    }
};

C:

二维前缀和+蛇形填数。。

面试题考的更多是实现能力。

#include <bits/stdc++.h>
using namespace std;
const double PI= acos(-1.0);
const int M =2007;
/*
int head[M],cnt=1;
void init(){cnt=1,memset(head,0,sizeof(head));}
struct EDGE{int to,nxt,w;}ee[M*2];
void add(int x,int y,int w){ee[++cnt].nxt=head[x],ee[cnt].w=w,ee[cnt].to=y,head[x]=cnt;}
*/

long long a[2007][2007];
pair<int,int>mp[2007*2007];
long long sl[2007],sh[2007],sml[2007],smh[2007],sm[2007][2007];
vector<int>ans;
    vector<int> getScores(int n, vector<int>& p) {
        // write code here
        ans.clear();
        memset(a,0,sizeof(a));
        memset(sh,0,sizeof(sh));
        memset(sl,0,sizeof(sl));
        int mod=1e9+7;
        int x=1,y=1,tot=0;
        tot = a[x][y]=1;mp[1]={1,1};
        while (tot < n * n) {
        	while (y<n && !a[x][y+1]) a[x][++y] = ++tot,mp[tot]={x,y};
	        while (x<n && !a[x+1][y]) a[++x][y] = ++tot,mp[tot]={x,y};
	        while (y>1 && !a[x][y-1]) a[x][--y] = ++tot,mp[tot]={x,y};
	        while (x>1 && !a[x-1][y]) a[--x][y] = ++tot,mp[tot]={x,y};
   		 }
   		for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)sh[i]+=a[i][j],sh[i]%=mod;
   		for(int j=1;j<=n;j++)for(int i=1;i<=n;i++)sl[j]+=a[i][j],sl[i]%=mod;
   		for(int i=1;i<=n;i++)smh[i]=sh[i]+smh[i-1],smh[i]%=mod;
   		for(int i=1;i<=n;i++)sml[i]=sl[i]+sml[i-1],sml[i]%=mod;
   		for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			sm[i][j]=sm[i-1][j]+sm[i][j-1]-sm[i-1][j-1]+a[i][j],sm[i][j]%=mod,sm[i][j]=(sm[i][j]+mod)%mod;
		int now=1,q=p.size();
		long long rs=0;
		for(int i=0;i<q;i++)
		{
			now+=p[i];now%=n*n;
			if(now==1)now=n*n;
	//		cout<<now<<"  -"<<endl;
			pair<int,int> tp=mp[now];
			int xr=min(n,tp.first+p[i]-1);
			int yr=min(n,tp.second+p[i]-1);
			int xl=max(1,tp.first-(p[i]-1));
			int yl=max(1,tp.second-(p[i]-1));
			rs=(rs+smh[xr]-smh[xl-1]+sml[yr]-sml[yl-1])%mod;
			rs-=(sm[xr][yr]+sm[xl-1][yl-1]-sm[xl-1][yr]-sm[xr][yl-1])%mod;
			rs%=mod;
			rs=(rs+mod)%mod;
			ans.push_back(rs);
		}
		return ans;
    }
int main()
{
	ios::sync_with_stdio(false);
  	cin.tie(0);
	int n;
	vector<int>Q,p;
	cin>>n;
	int q,x;cin>>q;
	for(int i=1;i<=q;i++)cin>>x,Q.push_back(x);
//	cout<<"oko "<<endl;
  	p=getScores(n,Q);
  	
  	for(auto x:p)
  	cout<<x<<" ";
  	
	return 0;
}
/*
4 5
1 2 5 6 2
*/

猜你喜欢

转载自blog.csdn.net/bjfu170203101/article/details/107549740