• CF1312F Attack on Red Kingdom


    Attack on Red Kingdom

    The Red Kingdom is attacked by the White King and the Black King!

    The Kingdom is guarded by (n) castles, the (i)-th castle is defended by (a_i) soldiers. To conquer the Red Kingdom, the Kings have to eliminate all the defenders.

    Each day the White King launches an attack on one of the castles. Then, at night, the forces of the Black King attack a castle (possibly the same one). Then the White King attacks a castle, then the Black King, and so on. The first attack is performed by the White King.

    Each attack must target a castle with at least one alive defender in it. There are three types of attacks:

    • a mixed attack decreases the number of defenders in the targeted castle by (x) (or sets it to (0) if there are already less than x defenders);

    • an infantry attack decreases the number of defenders in the targeted castle by (y) (or sets it to (0) if there are already less than y defenders);

    • a cavalry attack decreases the number of defenders in the targeted castle by (z) (or sets it to (0) if there are already less than z defenders).

    The mixed attack can be launched at any valid target (at any castle with at least one soldier). However, the infantry attack cannot be launched if the previous attack on the targeted castle had the same type, no matter when and by whom it was launched. The same applies to the cavalry attack. A castle that was not attacked at all can be targeted by any type of attack.

    The King who launches the last attack will be glorified as the conqueror of the Red Kingdom, so both Kings want to launch the last attack (and they are wise enough to find a strategy that allows them to do it no matter what are the actions of their opponent, if such strategy exists). The White King is leading his first attack, and you are responsible for planning it. Can you calculate the number of possible options for the first attack that allow the White King to launch the last attack? Each option for the first attack is represented by the targeted castle and the type of attack, and two options are different if the targeted castles or the types of attack are different.

    (1 le n le 3 cdot 10^5)(1 le x, y, z le 5)(1 le a_i le 10^{18})

    题解

    https://codeforces.com/blog/entry/74640
    目的明确地找规律。

    This problem seems like a version of Nim with some forbidden moves, so let's try to apply Sprague-Grundy theory to it.

    First of all, we may treat each castle as a separate game, compute its Grundy value, and then XOR them to determine who is the winner of the game. When analyzing the state of a castle, we have to know two things: the number of remaining soldiers in it and the type of the last attack performed on it. So, the state of the game can be treated as a pair.

    We can compute Grundy values for each state in a straightforward way, but the constraints are too large to do it. Instead, we should try to search for a period: five consecutive rows (by row we mean a vector of Grundy values for the same number of remaining soldiers, but different types of last attacks) of Grundy values determine all of the values after them, so as soon as we get the same five rows of Grundy values that we already met, we can determine the period.

    (f(i,j)) 表示 (i) 个人,上一次操作是 (j) 的SG值。那么若 (f(i)sim f(i+4)) 与之前的五行重复了,则 (f(i+5)) 一定与之前的五行的下一行一样。

    There are (15) values stored in these five rows, so the period can be up to (4^{15}) — but that's a really generous upper bound. Some intuition can help us to prove something like (10^5) or (10^6) as an upper bound, but it is better to check all cases with brute force and find out that the period is at most (36).

    After we've found the period of Grundy values, it's easy to get them in (O(1)) for any castle. To count the number of winning moves for the first player, we can compute the XOR-sum of all castles, and for each castle check what happens if we make some type of attack on it: if the XOR-sum becomes (0), then this move is winning.

    CO int N=3e5+10,K=5;
    int64 a[N];
    typedef vector<array<int,3> > state;
    map<state,int> d;
    state s;
    
    void real_main(){
    	int n=read<int>();
    	int x=read<int>(),y=read<int>(),z=read<int>();
    	for(int i=1;i<=n;++i) read(a[i]);
    	
    	function<int(vector<int>)> mex=[&](vector<int> a)->int{
    		sort(a.begin(),a.end());
    		a.erase(unique(a.begin(),a.end()),a.end());
    		if(a[0]>0) return 0;
    		for(int i=1;i<(int)a.size();++i)
    			if(a[i]>a[i-1]+1) return a[i-1]+1;
    		return a.size();
    	};
    	function<state(state)> go=[&](state s)->state{
    		array<int,3> f;
    		f[0]=mex({s[K-x][0],s[K-y][1],s[K-z][2]});
    		f[1]=mex({s[K-x][0],s[K-z][2]});
    		f[2]=mex({s[K-x][0],s[K-y][1]});
    		s.erase(s.begin()),s.push_back(f);
    		return s;
    	};
    	d.clear();
    	state cur(K,{0,0,0});
    	int cnt=0;
    	s.clear();
    	while(!d.count(cur)){
    		d[cur]=cnt;
    		s.push_back(cur.back());
    		cur=go(cur),++cnt;
    	}
    	int p=cnt-d[cur],pp=cnt-p;
    	
    	function<int(int64,int)> calc=[&](int64 x,int t)->int{
    		if(x<cnt) return s[x][t];
    		else return s[pp+(x-pp)%p][t];
    	};
    	int sum=0;
    	for(int i=1;i<=n;++i) sum^=calc(a[i],0);
    	int ans=0;
    	for(int i=1;i<=n;++i){
    		sum^=calc(a[i],0);
    		ans+=sum==calc(max(0LL,a[i]-x),0);
    		ans+=sum==calc(max(0LL,a[i]-y),1);
    		ans+=sum==calc(max(0LL,a[i]-z),2);
    		sum^=calc(a[i],0);
    	}
    	printf("%d
    ",ans);
    }
    int main(){
    	for(int T=read<int>();T--;) real_main();
    	return 0;
    }
    
  • 相关阅读:
    邮件发送的存储过程写法
    FormsAuthentication.GetRedirectUrl 方法
    视图性能优化——索引视图
    删除多字段的重复行保留最大最小行
    c#知识点
    sql存储过程exec执行字符串select 的区别
    UVALive5198 UVA512 Spreadsheet Tracking
    UVA129 HDU1627 Krypton Factor
    UVA129 HDU1627 Krypton Factor
    UVA10603 Fill
  • 原文地址:https://www.cnblogs.com/autoint/p/12597202.html
Copyright © 2020-2023  润新知