[DG Special Changsheng 2016 T2] [luogu P1965] Game problem / circle game

Game issues / spinning games

Title link: luogu P1965

General idea

There is a circle where people circle clockwise according to the number, and every time people move.
The movement method is that each person walks m steps in a clockwise direction (m is given).
Ask you where the person who was originally at position x is now after taking the 10th power of k.
Multiple groups of inquiries.

Ideas

Obviously, the answer is this: (x + 1 0 k × m) mod n (x+10^k\times m)\mod n(x+10k×m)modn

I ran out of the loop section first, and then determined where I went to the loop section at the end, and output it.
Because there are powers, we can use fast powers to speed it up.

Code

#include<cstdio>
#define ll long long

using namespace std;

ll n, m, k, x;
ll q[1000001], zq;

ll read() {
    
    
	ll re = 0, zf = 1;
	char c = getchar();
	while (c < '0' || c > '9') {
    
    
		if (c == '-') zf = -zf;
		c = getchar();
	}
	while (c >= '0' && c <= '9') {
    
    
		re = (re << 3) + (re << 1) + c - '0';
		c = getchar();
	}
	return re * zf;
}

void write(ll now) {
    
    
	if (now < 0) {
    
    
		printf("-");
		write(-now);
		return ;
	}
	if (now > 9ll) write(now / 10);
	putchar(now % 10 + '0');
}

ll ksm(ll X, ll y) {
    
    //快速幂
	ll re = 1ll;
	while (y) {
    
    
		if (y & 1) re = (re * X) % zq;
		X = (X * X) % zq;
		y >>= 1;
	}
	return re % zq;
}

int main() {
    
    
//	freopen("game.in", "r", stdin);
//	freopen("game.out", "w", stdout);
	
	n = read();
	m = read();
	k = read();
	x = read();
	
	int now = x;
	q[zq++] = now;
	while ((now + m) % n != x) {
    
    
		now = (now + m) % n; 
		q[zq++] = now;
	}
	
	k = ksm(10ll, k);
	
	write(q[k]);
	
	fclose(stdin);
	fclose(stdout);
	
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43346722/article/details/115029529