约瑟夫环
#include <bits/stdc++.h>
using namespace std;
const int N = 1000 + 10;
int main()
{
int n, k;
scanf("%d%d", &n, &k);
int ans = 0;
for(int i = 2; i <= n; i++) ans = (ans + k) % i;
printf("%d\n", ans + 1);
return 0;
}
染色
bool dfs(int v, int x)
{
color[v] = x;
for(int i = head[v]; ~i; i = g[i].next)
{
int u = g[i].to;
if(color[v] == color[u]) return false;
if(! color[u] && !dfs(u, -color[v])) return false;
}
return true;
}
bool bfs(int v, int x)
{
queue<int> que;
que.push(v), color[v] = x;
while(! que.empty())
{
int v = que.front(); que.pop();
for(int i = head[v]; ~i; i = g[i].next)
{
int u = g[i].to;
if(color[v] == color[u]) return false;
if(! color[u]) color[u] = -color[v], que.push(u);
}
}
return true;
}
自造随机数(不会重复)
inline int random(){
static int seed=703; //seed可以随便取
return seed=int(seed*48271LL%2147483647);
}