ZOJ Monthly, March 2018 Solution

A - Easy Number Game

水。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3
 4 #define ll long long
 5 #define N 100010
 6 ll arr[N];
 7 int n, m;
 8
 9 int main()
10 {
11     int t; scanf("%d", &t);
12     while (t--)
13     {
14         scanf("%d%d", &n, &m);
15         for (int i = 1; i <= n; ++i) scanf("%lld", arr + i);
16         sort(arr + 1, arr + 1 + n);
17         ll res = 0;
18         for (int i = 1; i <= m; ++i)
19             res += arr[i] * arr[2 * m - i + 1];
20         printf("%lld\n", res);
21     }
22     return 0;
23 }

B - Lucky Man

题意:判断大数开根后的奇偶性

思路:牛顿迭代法

 1 import java.io.BufferedInputStream;
 2 import java.util.Scanner;
 3 import java.math.*;
 4
 5 public class Main {
 6
 7     public static void main(String[] args) {
 8         Scanner in = new Scanner(new BufferedInputStream(System.in));
 9         int t = in.nextInt();
10         BigInteger a, x, two; String n;
11         two = BigInteger.valueOf(2);
12         while (t-- != 0)
13         {
14             n = in.next();
15             a = new BigInteger(n);
16             x = new BigInteger(n.substring(0, n.length() / 2 + 1));
17             while (a.compareTo(x.multiply(x)) < 0)
18                 x = x.add(a.divide(x)).divide(two);
19             if (x.mod(two).compareTo(BigInteger.ZERO) == 0) System.out.println(0);
20             else System.out.println(1);
21         }
22         in.close();
23     }
24 }

C - Travel along the Line

题意:一维坐标系中,刚开始位于原点,有$\frac{1}{4}$的概率 坐标 +1 和 -1  有$\frac {1}{2} 的概率 不动$  求在第n秒的时候恰好到达第m个位置的概率

思路:考虑把一个0拆成两个0,变成四种操作,这样四种操作是等概率的,那么所有的可能性就是 $4^n$ 再考虑符合条件的方案数

可以考虑将m通过坐标变换转化成正直,那么一个满足题意的操作序列肯定是 1 的个数 减去 -1的 个数 恰好为m

那么我们只需要枚举1的个数,排列组合一下即可

16说 假如用a 表示 1 的个数  b 表示 -1 的个数 c 表示 0的个数

那么有$\frac {n!} {a! \cdot b! \cdot c!}$ 但是这里要考虑 多乘上$2^c$ 因为每个0都有两种选择 ,可以是$0_1 或者 是 0_2$

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3
 4 #define ll long long
 5 #define N 100010
 6 ll MOD = (ll)1e9 +7;
 7
 8 ll fac[N], Bit[N];
 9 ll qmod(ll base, ll n)
10 {
11     ll res = 1;
12     while (n)
13     {
14         if (n & 1) res = res * base % MOD;
15         base = base * base % MOD;
16         n >>= 1;
17     }
18     return res;
19 }
20
21 void Init()
22 {
23     fac[0] = 1;
24     Bit[0] = 1;
25     for (int i = 1; i < N; ++i) fac[i] = fac[i - 1] * i % MOD;
26     for (int i = 1; i < N; ++i) Bit[i] = Bit[i - 1] * 2 % MOD;
27 }
28
29 int n, m;
30
31 int main()
32 {
33     Init();
34     int t; scanf("%d", &t);
35     while (t--)
36     {
37         scanf("%d%d", &n, &m);
38         if (m < 0) m = -m;
39         ll p = 0, q = qmod(4, n);
40         for (int i = 0; 2 * i + m <= n; ++i)
41             p = (p + (fac[n] * qmod(fac[i], MOD - 2) %MOD * qmod(fac[i + m], MOD - 2) % MOD * qmod(fac[n - 2 * i - m], MOD - 2) % MOD * Bit[n - 2 * i - m] % MOD)) % MOD;
42         ll res = p * qmod(q, MOD - 2) % MOD;
43         printf("%lld\n", res);
44     }
45     return 0;
46 }

D - Machine Learning on a Tree

留坑。

E - Yet Another Tree Query Problem

留坑。

F - And Another Data Structure Problem

留坑。

G - Neighboring Characters

留坑。

H - Happy Sequence ZOJ

题意:用1-n的数,每个数可以用无限次,组成长度为m的序列,求有多少个序列满足 $gcd(b_i, b_{i +1}) = b_{i}$

思路:考虑枚举序列里面不同的数的个数,根据题目范围,最多有10个不同的数,然后隔板法求方案数

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3
 4 #define ll long long
 5 long long f[15];
 6 long long C[15];
 7 long long MOD;
 8 int p[15];
 9 int n, m;
10
11 void dp(int t) {
12     int j;
13     j = 2;
14     while (p[t - 1] * j <= n) {
15         p[t] = p[t - 1] * j;
16         f[t]++;
17         dp(t + 1);
18         j++;
19     }
20 }
21
22 ll qmod(ll base, ll n)
23 {
24     ll res = 1;
25     while (n)
26     {
27         if (n & 1) res = res * base % MOD;
28         base = base * base % MOD;
29         n >>= 1;
30     }
31     return res;
32 }
33
34 int main()
35 {
36     int i, j, t;
37     long long ans;
38     scanf("%d", &t);
39     MOD = 1000000007;
40     while (t--) {
41         scanf("%d %d", &n, &m);
42         memset(f, 0, sizeof f);
43         memset(p, 0, sizeof p);
44         ans = 0;
45         C[0] = 1;
46         for (i = 1; i <= 11 ; ++i)
47             C[i] = (C[i - 1] * (m - i) % MOD * qmod(i, MOD - 2)) % MOD;
48         for (i = 1; i <= n; ++i) {
49             p[1] = i;
50             ++f[1];
51             dp(2);
52         }
53         for (i = 1; i <= 11; ++i) {
54             ans = (ans + f[i] * C[i - 1] % MOD) % MOD;
55         }
56         printf("%lld\n",ans);
57     }
58     return 0;
59 }

I - Your Bridge is under Attack

留坑。

J - Super Brain

水。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3
 4 #define N 100010
 5 int n;
 6 int cnt[N * 10], a[N], b[N];
 7
 8 int main()
 9 {
10     int t; scanf("%d", &t);
11     while (t--)
12     {
13         scanf("%d", &n);
14         for (int i = 1; i <= n; ++i) scanf("%d", a + i);
15         for (int i = 1; i <= n; ++i) scanf("%d", b + i);
16         memset(cnt, 0, sizeof cnt);
17         for (int i = 1; i <= n; ++i) ++cnt[a[i]];
18         int res = 0;
19         for (int i = 1; i <= n; ++i) if (cnt[b[i]] == 1)
20         {
21             res = b[i];
22             break;
23         }
24         printf("%d\n", res);
25     }
26     return 0;
27 }

原文地址:https://www.cnblogs.com/Dup4/p/9845982.html

时间: 2024-08-02 01:21:38

ZOJ Monthly, March 2018 Solution的相关文章

ZOJ 4009 And Another Data Structure Problem(ZOJ Monthly, March 2018 Problem F,发现循环节 + 线段树)

题目链接  ZOJ Monthly, March 2018 Problem F 题意很明确 这个模数很奇妙,在$[0, mod)$的所有数满足任意一个数立方$48$次对$mod$取模之后会回到本身. 所以开$48$棵线段树,和一个永久标记.当对某个区间操作时对这个区间加一层永久标记. 即当前我要查找的第$x$层,实际找的是第$up[i] + x$层. 时间复杂度$O(48nlogn)$ #include <bits/stdc++.h> using namespace std; #define

ZOJ Monthly, March 2018

Easy Number Game Time Limit: 2 Seconds      Memory Limit: 65536 KB The bored BaoBao is playing a number game. In the beginning, there are  numbers. For each turn, BaoBao will take out two numbers from the remaining numbers, and calculate the product

ZOJ Monthly, January 2018 Solution

A - Candy Game 水. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 #define N 1010 5 int t, n; 6 int a[N], b[N]; 7 8 int main() 9 { 10 scanf("%d", &t); 11 while (t--) 12 { 13 scanf("%d", &n); 14 for (int i = 1; i <

ZOJ Monthly, January 2018

A. Candy Game 显然最优策略是一个一个吃,故比较哪种糖果的个数比较多即可. #include<cstdio> int T,n,i,x,sum; int main(){ scanf("%d",&T); while(T--){ scanf("%d",&n); sum=0; for(i=1;i<=n;i++)scanf("%d",&x),sum+=x; for(i=1;i<=n;i++)sca

[ZOJ]ZOJ Monthly, January 2018

solved 4 rank 1 题挺好的,就是没见过这么卡常的.. A(签到) 题意:有n个盒子,每个盒子里都有若干AB两种糖,甲只能吃A,乙只能吃B,每次至少吃一个,最多把一个盒子里的吃光,没有糖之后就不能吃,吃掉最后一颗糖的获胜,问谁能获胜. 显然一次吃一颗最优,谁的糖多谁赢. #include<bits/stdc++.h> using namespace std; #define rep(i,n) for(int i=1;i<=n;i++) #define LL long long

135 - ZOJ Monthly, August 2014

135 - ZOJ Monthly, August 2014 A:构造问题,推断序列奇偶性.非常easy发现最小值不是1就是0.最大值不是n就是n - 1,注意细节去构造就可以 E:dp.dp[i][j]表示长度i,末尾状态为j的最大值,然后每一个位置数字取与不取,不断状态转移就可以 G:就一个模拟题没什么好说的 H:dfs,每次dfs下去,把子树宽度保存下来,然后找最大值,假设有多个.就是最大值+cnt宽度 I:构造,假设r * 2 > R,肯定无法构造.剩下的就二分底边.按等腰三角形去构造就

ZOJ Monthly, September 2003【部分题解】

今天比赛做了一下这套题目.出了四道.两道水题,两道DP 比赛链接:http://vjudge.net/contest/view.action?cid=51404#problem/B 上来搞了一道水题之后就搞B题 题意很好理解,上来看了一下就懂了.以为是规律有循环节,没看wa那么多毅然决然提交,wa了一发. A = "^__^" and B = "T.T",C = BA = "T.T^__^".然后A=B,B=C,一直重复这个操作,问最后第n位的字

ZOJ Monthly, June 2014——Grouping

题目连接 题意: n个点,m条边 每条边两个整数a.b,表示a到b的有向边 求,至少需要几个集合,使得:每个集合中的元素互相不能到达 N(1≤ N≤ 100000), M(1≤ M≤ 300000) 分析: 相连的两个点不能在同一个集合中,那么,对于一个长度为n的链,至少需要n个集合:如果链中有环,相当于把环展开,这个就需要缩点处理 就是缩点之后求点权最长路 注意:模板中scc_cnt是从1开始的,如果使用缩点后的图,初始化时需要初始化总点数加一 因为总点数有限,用拓扑排序每次删除所有入度为零的

记次浙大月赛 134 - ZOJ Monthly, June 2014

链接 虽做出的很少,也记录下来,留着以后来补..浙大题目质量还是很高的 B 并查集的一些操作,同类和不同类我是根据到根节点距离的奇偶判断的,删点是直接新加一个点,记得福大月赛也做过类似的,并差集的这类关系题目还是比较常见的,有空深究一下. 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6