1452 - Jump(dp+约瑟夫环变形)

Integers 1, 2, 3,..., n are placed on a circle in the increasing order as in the following figure. We want to construct a sequence from these numbers on a circle. Starting with the number 1, we continually go round by picking out each k-th number and send to a sequence queue until all numbers on the circle are exhausted. This linearly arranged numbers in the queue are called Jump(nk) sequence where 1$ \le$nk.

Let us compute Jump(10, 2) sequence. The first 5 picked numbers are 2, 4, 6, 8, 10 as shown in the following figure. And 3, 7, 1, 9 and 5 will follow. So we get Jump(10, 2) = [2,4,6,8,10,3,7,1,9,5]. In a similar way, we can get easily Jump(13, 3) = [3,6,9,12,2,7,11,4,10,5,1,8,13], Jump(13, 10) = [10,7,5,4,6,9,13,8,3,12,1,11,2] and Jump(10, 19) = [9,10,3,8,1,6,4,5,7,2].

\epsfbox{p4727.eps}

Jump(10,2) = [2,4,6,8,10,3,7,1,9,5]

You write a program to print out the last three numbers of Jump(nk) for nk given. For example suppose that n = 10k = 2, then you should print 1, 9 and 5 on the output file. Note that Jump(1, k) = [1].

Input 

Your program is to read the input from standard input. The input consists of T test cases. The number of test cases T is given in the first line of the input. Each test case starts with a line containing two integers n and k, where 5$ \le$n$ \le$500, 000 and 2$ \le$k$ \le$500, 000.

Output 

Your program is to write to standard output. Print the last three numbers of Jump(nk) in the order of the last third, second and the last first. The following shows sample input and output for three test cases.

Sample Input 

3 
10 2 
13 10 
30000 54321

Sample Output 

1 9 5 
1 11 2 
10775 17638 23432

题意:给定一个1-n的数字环,每次跳k步,求最后3个数。

思路:约瑟夫环的变形,最后1个数的求法就是基本的约瑟夫环,对于2,3个数而言,就当做是在i==2和i==3的时候,和其他编号不一样的就是他们当前的编号,那么就从这个位置推到n即可。

代码:

#include <stdio.h>
#include <string.h>

int t, n, k;

int main() {
	scanf("%d", &t);
	while (t--) {
		scanf("%d%d", &n, &k);
		int ans1 = 0, ans2 = 0, ans3 = 0;
		for (int i = 2; i <= n; i++) {
			ans1 = (ans1 + k) % i;
			if (i == 2) ans2 = 1 - ans1;
			else {
				ans2 = (ans2 + k) % i;
				if (i == 3) ans3 = 3 - ans2 - ans1;
				else ans3 = (ans3 + k) % i;
			}
		}
		printf("%d %d %d\n", ans3 + 1, ans2 + 1, ans1 + 1);
	}
	return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用rugarch包实现GARCH-Jump模型: 首先要安装rugarch包: ``` install.packages("rugarch") ``` 加载rugarch包: ``` library(rugarch) ``` 读取数据: ``` data <- read.csv("data.csv") ``` 创建spec对象: ``` spec <- ugarchspec(variance.model = list(model = "sGARCH", garchOrder = c(1,1)), mean.model = list(armaOrder = c(1, 1)), distribution.model = "norm", fixed.pars = list(ar1 = 0.5, omega = 0.01, alpha1 = 0.1, beta1 = 0.8), fixed.mixtures = 1, mixtures = 1, start.pars = c(0.01, 0.5, 0.1, 0.8, 0.1), fixed.jump = list(jump.mean = TRUE, jump.volatility = FALSE), jump.model = list(model = "JW"), jump.type = "OU") ``` 其中,variance.model表示波动率模型为sGARCH,garchOrder为(1,1);mean.model表示均值模型为ARMA(1,1);distribution.model表示分布为正态分布;fixed.pars表示固定的参数值;fixed.mixtures表示固定的混合数(在本例中为1);mixtures表示混合数;start.pars表示初始参数值;fixed.jump表示是否固定跳跃的均值和波动率;jump.model表示跳跃模型为JW(Jump with Normal Distribution);jump.type表示跳跃类型为OU(Ornstein-Uhlenbeck)。 估计GARCH-Jump模型: ``` fit <- ugarchfit(spec, data = data$returns) ``` 其中,spec为创建的spec对象,data为数据,data$returns表示收益率。 查看模型结果: ``` summary(fit) ``` 输出结果包括模型参数估计值、标准误、t值、p值等信息。 预测未来收益率: ``` forecast <- ugarchforecast(fit, n.ahead = 10) ``` 其中,fit为估计的GARCH-Jump模型,n.ahead为预测未来的时间步数。 输出结果: ``` forecast@forecast$seriesFor[,"Series1"] ``` 其中,Series1为数据中的列名,表示预测的未来收益率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值