51Nod 1073 约瑟夫环(数学解决)
来源:互联网 发布:oecd怎么下载数据啊 编辑:程序博客网 时间:2024/06/18 12:09
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1073
题目大意:
分析:
模拟会超时 然后问题可以推导一下
首先将问题变换一下 编号 0~N-1 报 K-1 出列 和原问题没有区别最后答案+1即可 但是处理起来就很方便了
第一个出队列人的编号为 (k-1)%N
然后队列就变成了 0 1 2 3 4 ... ... k-2 k k+1 ... ... N-1
这时候需要从K+1开始重新报数 也就是
0 1 ... ... ... ... ... ... N-2
k k+1 ... ... N-1 0 1 2 3 4 ... ... k-1
然后报数报到k-1的出列 现在我们发现问题变成N-2个人报数了 问题规模缩小为N-2
新序列出队编号为 (k-1)%(N-1) 旧序列出队编号为((k-1)%(N-1)+k)%(N-1) 然后可以依次从新编号 出队列
直到问题规模为1 然后得出新序列最后出队列的人的编号 一级一级推回去得到最初序列的编号
然后问题就变成了 我们知道问题规模为N-1的最后一个出队列的编号 就可以推出问题规模为N的最后一个出队列的编号 公式为
f(1) = 0;
f(i) = (f(i-1) + k) % i;
AC代码:
#include <iostream>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <math.h>#include <vector>#include <stack>#include <queue>#include <map>#include <set>#include<list>#include <bitset>#include <climits>#include <algorithm>#define gcd(a,b) __gcd(a,b)#define mset(a,n) memset(a,n,sizeof(a))#define FINfreopen("input.txt","r",stdin)#define FOUT freopen("output.txt","w",stdout)typedef long long LL;const LL mod=1e9+7;const int INF=0x3f3f3f3f;const double PI=acos(-1.0);using namespace std;int main (){ int n,k; while (scanf ("%d%d",&n,&k)!=EOF){ int f,fr=0; for (int i=2;i<=n;i++){ f=(fr+k)%i; fr=f; } printf ("%d\n",f+1); } return 0;}
- 51Nod 1073 约瑟夫环(数学解决)
- 51nod 1073 约瑟夫环 (数学)
- 51nod:1073 约瑟夫环(数学,递推)
- 51nod 1073 约瑟夫环 (数学递推)
- 约瑟夫问题 51nod 1073 约瑟夫环
- 51nod 1073 约瑟夫环
- 51nod-1073约瑟夫环
- 51nod 1073 约瑟夫环
- 51nod 1073 约瑟夫环
- 51nod 1073 约瑟夫环
- 51NOD 1073 约瑟夫环
- 51Nod 1073 约瑟夫环
- 51 nod 1073 约瑟夫环
- 51Nod-1073-约瑟夫环
- 51nod 1073 约瑟夫环
- 51Nod 1073 约瑟夫环
- 51Nod-1073 约瑟夫环
- 51nod 1073 约瑟夫环
- Linux之文件跳转
- idea svn 使用
- arcgis 版本相关理解
- ios学习之基于 OpenCV的人脸识别
- caffe MNIST手写数字识别例程
- 51Nod 1073 约瑟夫环(数学解决)
- ios plist 权限访问大全
- HorizontalScrollView头部滑动
- ${pageContext.request.contextPath} :JSP取得绝对路径方法
- 解决IE浏览器兼容性问题:“JSON”未定义
- 微机原理之八大通用寄存器
- Unity低级本地插件接口--Low-level Native Plugin Interface
- ElemType是什么?
- NIO 02 阻塞式和非阻塞式[Socket]