魔数
来源:CCF
标签:
参考资料:
相似题目:
题目背景
“魔数”是指代码中出现但没有解释的数字常量。即使代码是你亲手写下的,很可能在几个月以后你也不记得这些魔数的意义了。而这道题中魔数的含义,就需要你自己探索了…
在本题中,我们定义:
U0 = 314882150829468584
U1= 427197303358170108
U2= 1022292690726729920
U3= 1698479428772363217
U4= 2006101093849356424
f(x)=(x mod 2009731336725594113)mod 2019
题目描述
有一个长度为n的序列A,初始时所有元素依次为从1到n的正整数,即Ai=i(l<=i<=n)。
有q次查询,每次查询输入两个整数l,r(1<=l<=r<=n),你需要依次进行以下操作:
·设s=f(Al)+ f(Al+1)+…+f(Ar);
·输出s;
·设t=s mod 5;
·令Al,Al+1,…,Ar,的每个数都乘以Ut请你依次输出每一次查询的答案。
输入格式
从标准输入读入数据。
第一行包含两个正整数n,q
接下来q行,每行输入两个正整数1,r.
输出格式
从标准输入读入数据。
第一行包含两个正整数n,q。
接下来q行,每行输入两个正整数1,r
样例1输入
4 4
1 3
3 4
3 3
1 3
样例1输出
6
1105
1735
4744
样例2输入
100 100
45 74
38 50
7 45
42 62
83 100
50 51
8 11
93 98
64 70
15 87
30 87
13 79
14 81
18 79
70 88
25 39
13 57
55 85
80 92
83 90
54 75
1 61
17 42
25 49
39 77
32 45
83 87
30 47
59 84
25 50
1 82
21 45
72 96
3 85
16 64
52 92
28 29
84 88
26 93
10 67
27 76
57 62
43 69
63 66
5 59
9 46
49 53
35 50
3 19
23 62
38 73
17 68
34 83
42 91
13 92
19 62
17 70
18 75
95 99
35 90
81 91
59 63
5 90
22 87
51 88
25 61
56 91
50 78
11 60
11 18
27 45
57 82
16 54
3 94
33 56
9 71
68 88
24 36
7 64
48 85
58 76
20 43
9 90
24 27
71 97
25 95
73 97
55 83
22 43
53 55
68 88
12 44
25 87
14 46
34 56
15 35
7 80
46 87
23 71
88 93
样例2输出
1785
5741
10423
24915
1647
2154
1872
8559
12936
60048
52916
79974
61897
50541
16819
15646
48044
30156
14581
6906
17346
45885
25217
29837
44539
12602
5964
16894
23972
30665
64047
28029
26086
89745
49102
40236
2297
6040
64456
57625
48151
8311
27574
4166
52887
37703
4922
17603
17729
35771
35915
52458
54055
44140
70298
39690
49407
48808
4775
55131
9378
2839
75644
58663
40660
29344
38759
31862
51760
7924
22539
22003
31095
86980
25718
61094
18995
13703
56434
35626
18678
22776
82576
3233
24072
76470
23887
28161
26150
2017
19769
31420
63547
31533
24513
20199
62729
39286
43276
6109
提示
所有最终测试数据的生成方式为:
按照上表指定n,q(1<n<1000000,1<q<100000)
对于每次查询,l,r均在合法范围内独立均匀随机确定。
要求:运行时间上限:4.0s,内存上限:1G
解题思路
利用大数运算暴力计算,运行结果争取,内存占用500M左右,但运行时间超了,导致得分20,希望会优化得小伙伴可以帮帮忙
import java.math.BigInteger;
import java.util.Scanner;
public class Main{
//计算f(x)
public static BigInteger resultF(BigInteger x){
BigInteger z = new BigInteger("2019");
BigInteger i = new BigInteger("2009731336725594113");
BigInteger resultF = x.remainder(i).remainder(z);
return resultF;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int q = scanner.nextInt();
BigInteger t = new BigInteger("5");
BigInteger []L = new BigInteger[5];
L[0] = new BigInteger("314882150829468584");
L[1] = new BigInteger("427197303358170108");
L[2] = new BigInteger("1022292690726729920");
L[3] = new BigInteger("1698479428772363217");
L[4] = new BigInteger("2006101093849356424");
int [][]sz = new int[q][2];
for(int i = 0 ; i < q ; i++){
for(int j = 0 ; j < 2 ; j++){
sz[i][j] = scanner.nextInt();
}
}
//定义BigInteger类型得A1~A1000000
BigInteger []A = new BigInteger[1000000+1];
for(int i = 1 ; i <= 1000000 ; i++)
A[i] = BigInteger.valueOf(i);
//
for(int i = 0 ; i < q ; i++){
BigInteger s = new BigInteger("0");
for(int j = sz[i][0] ; j <= sz[i][1] ; j++){
s = s.add(resultF(A[j])); //求s
}
System.out.println(s);
BigInteger temp = s.remainder(t);//求t
for(int j = sz[i][0] ; j <= sz[i][1] ; j++){
if((temp.intValue()) == 0)
A[j] = A[j].multiply(L[0]);
else if((temp.intValue()) == 1)
A[j] = A[j].multiply(L[1]);
else if((temp.intValue()) == 2)
A[j] = A[j].multiply(L[2]);
else if((temp.intValue()) == 3)
A[j] = A[j].multiply(L[3]);
else if((temp.intValue()) == 4)
A[j] = A[j].multiply(L[4]);
}
}
}
}