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 of them.

Now, BaoBao is curious to know the minimum sum of the products if he plays at least  turns. Can you tell him?

Input

The first line of input contains a positive integer  (about 30), the number of test cases. For each test case:

The first line contains two integers  and  (, ). Their meanings are described above.

The second line contains  integers  (), indicating the numbers.

Output

For each test case output one integer, indicating the minimum sum of the products.

Sample Input

3
4 2
1 3 2 4
3 1
2 3 1
4 0
1 3 2 4

Sample Output

10
2
0

Hint

For the first sample test case, the answer is 1 × 4 + 3 × 2 = 10.

For the second sample test case, the answer is 2 × 1 = 2.

n个数找m组数,使乘积的和最小

让前m小最大和最小的相乘就好了

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int a[N];
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int n,m;
        cin>>n>>m;
        for(int i=0; i<n; i++)
            cin>>a[i];
        sort(a,a+n);
        int r=2*m-1;
        long long s=0;
        for(int i=0; i<m; i++)
            s+=a[i]*a[r-i];
        cout<<s<<endl;
    }
}

Super Brain


Time Limit: 1 Second      Memory Limit: 65536 KB


Super Brain is a famous scientific reality and talent show aiming to find people with exceptional brainpower.

In one of the challenges, two integer sequences  and  of length  are given to the contestant. It‘s guaranteed that  and  hold for all , and there is exactly one integer in the first sequence which also appears in the second sequence. The contestant has to memorize the two sequences in a very short time, and find the integer which appears in both sequences correctly.

As a technical staff of the show, you are required to write a program and find out the correct integer.

Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains an integer  (), indicating the length of the sequence.

The second line contains  integers  (), indicating the first sequence.

The third line contains  integers  (), indicating the second sequence.

It‘s guaranteed that  and  hold for all , and there is exactly one integer in the first sequence which also appears in the second sequence.

It‘s also guaranteed that the sum of  over all test cases will not exceed .

Output

For each test case output one line containing one integer, indicating the integer which appears in both sequences.

Sample Input

3
3
3 1 2
5 3 4
2
38324 14122
38323 14122
1
180310
180310

Sample Output

3
14122
180310
我可能被卡常了,memset就过了找到两个数组唯一相同的
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int a[N],n;
int main()
{
    ios::sync_with_stdio(false);
    int T;
    cin>>T;
    while(T--)
    {
        memset(a,0,sizeof a);
        cin>>n;
        int ans;
        for(int i=0,x; i<n; i++)cin>>x,a[x]++;
        for(int i=0,x; i<n; i++)
        {
            cin>>x;
            if(a[x])ans=x;
        }
        cout<<ans<<"\n";
    }
}

Happy Sequence


Time Limit: 3 Seconds      Memory Limit: 65536 KB


A sequence of  integers  () is called a happy sequence if each number divides (without a remainder) the next number in the sequence. More formally, we can say  for all , or we can say  for all .

Given  and , find the number of happy sequences of length . Two sequences  and  are different, if and only if there exists an such that  and .

As the answer can be rather large print it modulo  ().

Input

There are multiple test cases. The first line of the input contains an integer  (about 50), indicating the number of test cases. For each test case:

The first and only line contains two integers  and  (), indicating the upper limit of the elements in the sequence and the length of the sequence.

Output

For each case output a single integer, indicating the number of happy sequences of length  modulo

Sample Input

1
3 2

Sample Output

5

Hint

In the sample test case, the happy sequences are: ,?,?,?,?.

一个数组是按照倍数关系的

这个题利用埃筛的思想去枚举i个数s*j作为最后一个元素就好

using namespace std;
typedef long long ll;
const ll MD=1e9+7;
const int N=2005;
ll dp[N][N];
int main()
{
    ios::sync_with_stdio(false);
    int T;
    cin>>T;
    while(T--)
    {
        int n,k;
        cin>>n>>k;
        memset(dp,0,sizeof dp);
        for(int i=1; i<N; i++) dp[1][i]=1;
        for(int i=1; i<k; i++)
            for(int j=1; j<=n; j++)
                for(int s=1; s*j<=n; s++)
                    dp[i+1][s*j]=(dp[i+1][s*j]+dp[i][j])%MD;
        ll sum=0;
        for(int i=1; i<=n; i++) sum=(sum+dp[k][i])%MD;
        cout<<(sum+MD)%MD<<endl;
    }
    return 0;
}

Travel along the Line


Time Limit: 1 Second      Memory Limit: 65536 KB


BaoBao is traveling along a line with infinite length.

At the beginning of his trip, he is standing at position 0. At the beginning of each second, if he is standing at position , with  probability he will move to position , with  probability he will move to position , and with  probability he will stay at position . Positions can be positive, 0, or negative.

DreamGrid, BaoBao‘s best friend, is waiting for him at position . BaoBao would like to meet DreamGrid at position  after exactly  seconds. Please help BaoBao calculate the probability he can get to position  after exactly  seconds.

It‘s easy to show that the answer can be represented as , where  and  are coprime integers, and  is not divisible by . Please print the value of  modulo , where  is the multiplicative inverse of  modulo .

Input

There are multiple test cases. The first line of the input contains an integer  (about 10), indicating the number of test cases. For each test case:

The first and only line contains two integers  and  (). Their meanings are described above.

Output

For each test case output one integer, indicating the answer.

Sample Input

3
2 -2
0 0
0 1

Sample Output

562500004
1
0


1s分为2s,只要n+m挑对了就行了

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MD=1e9+7;
const int N=2e5+5;
ll la(ll a,ll b)
{
    a%=MD;
    ll ans=1;
    while(b>0)
    {
        if(b&1)ans=ans*a%MD;
        b>>=1;
        a=a*a%MD;
    }
    return ans;
}
ll f[N],v[N],ans;
ll C(ll n,ll m)
{
    if(m<0||m>n) return 0;
    return f[n]*v[m]%MD*v[n-m]%MD;
}
int main()
{
    ios::sync_with_stdio(false);
    f[0]=1;
    for (ll i=1; i<N; i++) f[i]=f[i-1]*i%MD;
    v[N-1]=la(f[N-1],MD-2);
    for (ll i=N-2; i>=0; i--) v[i]=v[i+1]*(i+1LL)%MD;
    int T;
    cin>>T;
    while(T--)
    {
        int n,m;
        cin>>n>>m;
        ans=C(2*n,m+n)*la(la(2,2*n),MD-2)%MD;
        cout<<ans<<"\n";
    }
    return 0;
}

原文地址:https://www.cnblogs.com/BobHuang/p/8541184.html

时间: 2024-10-11 04:26:21

ZOJ Monthly, March 2018的相关文章

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 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"

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 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]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