ACM-ICPC 2018 南京赛区网络预赛

菜鸡就过了四道题,其中一道是改的板子,一道是大佬抬了一手,一道是水题,就一道线段树是我自己写的Orz、菜哭。大佬队友又不在,慌得一批,写的自闭。

先把过了的题记录一下吧,题以后再补,心态有点崩我怎么这么菜。

 A. An Olympian Math Problem

Alice, a student of grade 66, is thinking about an Olympian Math problem, but she feels so despair that she cries. And her classmate, Bob, has no idea about the problem. Thus he wants you to help him. The problem is:

We denote k!k!:

k! = 1 \times 2 \times \cdots \times (k - 1) \times kk!=1×2×⋯×(k−1)×k

We denote SS:

S = 1 \times 1! + 2 \times 2! + \cdots +S=1×1!+2×2!+⋯+
(n - 1) \times (n-1)!(n−1)×(n−1)!

Then SS module nn is ____________

You are given an integer nn.

You have to calculate SS modulo nn.

Input

The first line contains an integer T(T \le 1000)T(T≤1000), denoting the number of test cases.

For each test case, there is a line which has an integer nn.

It is guaranteed that 2 \le n\le 10^{18}2≤n≤1018.

Output

For each test case, print an integer SS modulo nn.

Hint

The first test is: S = 1\times 1!= 1S=1×1!=1, and 11 modulo 22 is 11.

The second test is: S = 1\times 1!+2 \times 2!= 5S=1×1!+2×2!=5 , and 55 modulo 33 is 22.

样例输入复制

2
2
3

样例输出复制

1
2

题目来源

ACM-ICPC 2018 南京赛区网络预赛

因为(n - 1)(n - 1) ! = n! - (n - 1)!,所以S = (2! - 1!) + (3! - 2!) ……(n! - (n - 1)!),也就是S = n! - 1。所以S ≡ (n - 1) % n。

#include <queue>
#include <cstdio>
#include <cstring>
#include <utility>
#include <iostream>
#include <map>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
#define N 400010
#define M 3645
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1);
#define fi first
#define se second
#define L o<<1
#define R o<<1|1
#define tl tree[o].l
#define tr tree[o].r
#define tw tree[o].w
#define to tree[o].ok
#define rep(i, lll, nnn) for(int i = (lll); i <= (nnn); i++)

int t;
ll n;

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("data.txt", "r", stdin);
    #endif

    scanf("%d", &t);
    while(t--) {
        scanf("%lld", &n);
        printf("%lld\n", n - 1);
    }

    return 0;
}

 G. Lpl and Energy-saving Lamps

During tea-drinking, princess, amongst other things, asked why has such a good-natured and cute Dragon imprisoned Lpl in the Castle? Dragon smiled enigmatically and answered that it is a big secret. After a pause, Dragon added:

— We have a contract. A rental agreement. He always works all day long. He likes silence. Besides that, there are many more advantages of living here in the Castle. Say, it is easy to justify a missed call: a phone ring can't reach the other side of the Castle from where the phone has been left. So, the imprisonment is just a tale. Actually, he thinks about everything. He is smart. For instance, he started replacing incandescent lamps with energy-saving lamps in the whole Castle...

Lpl chose a model of energy-saving lamps and started the replacement as described below. He numbered all rooms in the Castle and counted how many lamps in each room he needs to replace.

At the beginning of each month, Lpl buys mm energy-saving lamps and replaces lamps in rooms according to his list. He starts from the first room in his list. If the lamps in this room are not replaced yet and Lpl has enough energy-saving lamps to replace all lamps, then he replaces all ones and takes the room out from the list. Otherwise, he'll just skip it and check the next room in his list. This process repeats until he has no energy-saving lamps or he has checked all rooms in his list. If he still has some energy-saving lamps after he has checked all rooms in his list, he'll save the rest of energy-saving lamps for the next month.

As soon as all the work is done, he ceases buying new lamps. They are very high quality and have a very long-life cycle.

Your task is for a given number of month and descriptions of rooms to compute in how many rooms the old lamps will be replaced with energy-saving ones and how many energy-saving lamps will remain by the end of each month.

Input

Each input will consist of a single test case.

The first line contains integers nn and m (1 \le n \le 100000, 1 \le m \le 100)m(1≤n≤100000,1≤m≤100) — the number of rooms in the Castle and the number of energy-saving lamps, which Lpl buys monthly.

The second line contains nn integers k_1, k_2, ..., k_nk1​,k2​,...,kn​
(1 \le k_j \le 10000, j = 1, 2, ..., n)(1≤kj​≤10000,j=1,2,...,n) — the number of lamps in the rooms of the Castle. The number in position jjis the number of lamps in jj-th room. Room numbers are given in accordance with Lpl's list.

The third line contains one integer q (1 \le q \le 100000)q(1≤q≤100000) — the number of queries.

The fourth line contains qq integers d_1, d_2, ..., d_qd1​,d2​,...,dq​
(1 \le d_p \le 100000, p = 1, 2, ..., q)(1≤dp​≤100000,p=1,2,...,q) — numbers of months, in which queries are formed.

Months are numbered starting with 11; at the beginning of the first month Lpl buys the first m energy-saving lamps.

Output

Print qq lines.

Line pp contains two integers — the number of rooms, in which all old lamps are replaced already, and the number of remaining energy-saving lamps by the end of d_pdp​ month.

Hint

Explanation for the sample:

In the first month, he bought 44 energy-saving lamps and he replaced the first room in his list and remove it. And then he had 11 energy-saving lamps and skipped all rooms next. So, the answer for the first month is 1,1------11,1−−−−−−1 room's lamps were replaced already, 11 energy-saving lamp remain.

样例输入复制

5 4
3 10 5 2 7
10
5 1 4 8 7 2 3 6 4 7

样例输出复制

4 0
1 1
3 6
5 1
5 1
2 0
3 2
4 4
3 6
5 1

题目来源

ACM-ICPC 2018 南京赛区网络预赛

G做了我好久,原来build里写错了一行Orz。

线段树维护了最小值、未被置换的房间数量和房间值总和。

我看我代码就运行了72ms,大概不需要维护这么多。

#include <queue>
#include <cstdio>
#include <cstring>
#include <utility>
#include <iostream>
#include <map>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
#define N 100010
#define M 3645
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1);
#define fi first
#define se second
#define L o<<1
#define R o<<1|1
#define tl tree[o].l
#define tr tree[o].r
#define tw tree[o].w
#define tmw tree[o].mw
#define to tree[o].ok
#define tle tree[o].len
#define rep(i, lll, nnn) for(int i = (lll); i <= (nnn); i++)
inline int read(){
    int date=0,w=1;char c=0;
    while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
    while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}
    return date*w;
}

int n, m, cnt, ans[N], q, ans2[N], cnt2, maxi;
struct Node {
  int l, r, w, len, mw;
  bool ok;
}tree[N * 4];

void up(int o)
{
    tw = 0;
    tle = 0;
    tmw = INF;
    if(tree[L].ok) tw += tree[L].w, tle += tree[L].len, tmw = tree[L].mw;
    if(tree[R].ok) tw += tree[R].w, tle += tree[R].len, tmw = min(tmw, tree[R].mw);
    if(tw == 0) to = false;
}

void build(int o, int l, int r)
{
    tl = l; tr = r; to = true;
    tle = r - l + 1;
    if(l == r) {
        tmw = tw = read();
        return;
    }
    int mid = (tl + tr) >> 1;
    build(L, l, mid);
    build(R, 1 + mid, r);
    up(o);
}

void change(int o)
{
    if(cnt <= 0) return;
    if(!to) return;
    if(tmw > cnt) return;

    if(tw <= cnt) {
        cnt -= tw;
        to = false;
        cnt2 += tle;
        tle = 0;
        return;
    }

    if(tl == tr) return;

    change(L);
    change(R);

    up(o);
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("data.txt", "r", stdin);
    #endif

    n = read(); m = read();
    build(1, 1, n);

    cnt = 0;
    cnt2 = 0;
    rep(i, 1, 100000) {
        cnt += m;
        change(1);
        ans[i] = cnt;
        ans2[i] = cnt2;
        maxi = i;
        if(cnt2 >= n) break;
    }

    q = read();
    while(q--) {
        int d = read();
        if(d > maxi) d = maxi;
        printf("%d %d\n", ans2[d], ans[d]);
    }

    return 0;
}

J. Sum

A square-free integer is an integer which is indivisible by any square number except 11. For example, 6 = 2 \cdot 36=2⋅3 is square-free, but 12 = 2^2 \cdot 312=22⋅3 is not, because 2^222 is a square number. Some integers could be decomposed into product of two square-free integers, there may be more than one decomposition ways. For example, 6 = 1\cdot 6=6 \cdot 1=2\cdot 3=3\cdot 2, n=ab6=1⋅6=6⋅1=2⋅3=3⋅2,n=ab and n=ban=ba are considered different if a \not = ba̸=b. f(n)f(n) is the number of decomposition ways that n=abn=ab such that aa and bb are square-free integers. The problem is calculating \sum_{i = 1}^nf(i)∑i=1n​f(i).

Input

The first line contains an integer T(T\le 20)T(T≤20), denoting the number of test cases.

For each test case, there first line has a integer n(n \le 2\cdot 10^7)n(n≤2⋅107).

Output

For each test case, print the answer \sum_{i = 1}^n f(i)∑i=1n​f(i).

Hint

\sum_{i = 1}^8 f(i)=f(1)+ \cdots +f(8)∑i=18​f(i)=f(1)+⋯+f(8)
=1+2+2+1+2+4+2+0=14=1+2+2+1+2+4+2+0=14.

样例输入复制

2
5
8

样例输出复制

8
14

题目来源

ACM-ICPC 2018 南京赛区网络预赛

分块,先求出所有因子个数,再减去平方因子个数,再加上重复减掉的。

来自大佬原话,看不懂,弄懂了再来补吧Orz。

#include <queue>
#include <cstdio>
#include <cstring>
#include <utility>
#include <iostream>
#include <map>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
#define N 20000002
#define M 3645
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1);
#define fi first
#define se second
#define L o<<1
#define R o<<1|1
#define tl tree[o].l
#define tr tree[o].r
#define tw tree[o].w
#define to tree[o].ok
#define rep(i, lll, nnn) for(int i = (lll); i <= (nnn); i++)

int num[N + 1];
int n, tmp, t;
ll ans;

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("data.txt", "r", stdin);
    #endif

    for(int i = 2; i * i <= N; i++){
        tmp = i * i;
        for(int j = tmp; j <= N; j += tmp){
            num[j] = 1;
        }
    }
    for(int i = 1; i <= N; i++){
        num[i] += num[i - 1];
    }

    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        ans = 0;

        for(int i = 1, j; i <= n; i = j) {
            j = n / (n / i) + 1;
            ans += (j - i) * (ll)(n / i);
            ans -= 2 * (num[j - 1] - num[i - 1]) * (ll)(n / i);
            ans += (num[j - 1] - num[i - 1]) * (ll)num[n / i];
        }

        printf("%lld\n",ans);
    }
    return 0;
}

J题预处理,先打个线性筛,把1到n全都分解质因数,计算贡献,每个次幂为一的质因子贡献*2,次幂为2的质因子不算贡献,有超过2次幂的质因子那么这个数字就可以直接置为0了 

以上是一队大佬原话,这个就好理解多了。

一队大佬这么写的,这个好懂多了,,,

就是在想欧拉筛只是用最小质因子筛去这个数啊会不会有问题想了好久,发现自己对筛理解还是不够透彻啊,,,

#include <queue>
#include <cstdio>
#include <cstring>
#include <utility>
#include <iostream>
#include <map>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
#define N 20000002
#define M 3645
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1);
#define fi first
#define se second
#define L o<<1
#define R o<<1|1
#define tl tree[o].l
#define tr tree[o].r
#define tw tree[o].w
#define to tree[o].ok
#define rep(i, lll, nnn) for(int i = (lll); i <= (nnn); i++)

int cnt = 0;
const int maxn = 2e7 + 1;
bool isPri[maxn];//0为素数,1为合数
int Pri[maxn / 10];//记录素数

int ans[maxn];

void getPri(){
    memset(isPri, 0, sizeof(isPri));
    isPri[0] = isPri[1] = 1;

    for(int i = 2; i < maxn; ++i){
        if(!isPri[i]){
            Pri[cnt++] = i;
            ans[i] = 2;
        }
        for(int j = 0; j < cnt && Pri[j] * i < maxn; ++j){
            isPri[i * Pri[j]] = 1;
            if(i % Pri[j] == 0) {
                if(i % (Pri[j] * Pri[j]) == 0) ans[i * Pri[j]] = 0;
                else ans[i * Pri[j]] = ans[i] / 2;
                break;
            }
            else ans[i * Pri[j]] = ans[i] * 2;
        }
    }
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("data.txt", "r", stdin);
    #endif

    ans[1] = 1;
    getPri();
    rep(i, 1, maxn - 1) ans[i] += ans [i - 1];

    int t, n;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        printf("%d\n", ans[n]);
    }

    return 0;
}

 L. Magical Girl Haze

There are NN cities in the country, and MM directional roads from uu to v(1\le u, v\le n)v(1≤u,v≤n). Every road has a distance c_ici​. Haze is a Magical Girl that lives in City 11, she can choose no more than KK roads and make their distances become 00. Now she wants to go to City NN, please help her calculate the minimum distance.

Input

The first line has one integer T(1 \le T\le 5)T(1≤T≤5), then following TT cases.

For each test case, the first line has three integers N, MN,M and KK.

Then the following MM lines each line has three integers, describe a road, U_i, V_i, C_iUi​,Vi​,Ci​. There might be multiple edges between uu and vv.

It is guaranteed that N \le 100000, M \le 200000, K \le 10N≤100000,M≤200000,K≤10,
0 \le C_i \le 1e90≤Ci​≤1e9. There is at least one path between City 11 and City NN.

Output

For each test case, print the minimum distance.

样例输入复制

1
5 6 1
1 2 2
1 3 4
2 4 3
3 4 1
3 5 6
4 5 2

样例输出复制

3

题目来源

ACM-ICPC 2018 南京赛区网络预赛

分层最短路板子题,板子是无向图的板子Orz比赛时忘改了,菜的抠脚。就Kuangbin专题里写过一次分层最短路。

#include <queue>
#include <cstdio>
#include <cstring>
#include <utility>
#include <iostream>
#include <map>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
#define N 2000010
#define M 3645
const ll INF = 100000000000000000;
const double eps = 1e-8;
const double PI = acos(-1);
#define fi first
#define se second
#define L o<<1
#define R o<<1|1
#define tl tree[o].l
#define tr tree[o].r
#define tw tree[o].w
#define rep(i, lll, nnn) for(int i = (lll); i <= (nnn); i++)

int n, m, k, cnt;
int head[N];
ll dis[N];
bool v[N];

struct node{
    int from, to, next;
    ll val;
}edge[N * 4 + 10];
struct Node {
    ll val;
    int id;
};
bool operator < (const Node & a, const Node & b) {
    if(a.val == b.val)return a.id < b.id;
    return a.val > b.val;
}
priority_queue<Node> q;
void dijikstra(int s)
{
    fill(dis, dis + N, INF);
    memset(v, 0, sizeof v);
    while(!q.empty()) q.pop();
    Node fir;
    fir.val=0, fir.id=s;
    dis[s]=0;
    q.push(fir);
    while(!q.empty())
    {
        Node u=q.top();
        q.pop();
        if(v[u.id]) continue;
        v[u.id]=1;
        for(int i=head[u.id];i!=-1;i=edge[i].next)
        {
            int to=edge[i].to;
            if(dis[u.id]+edge[i].val<dis[to])
            {
                dis[to]=dis[u.id]+edge[i].val;
                Node pus;
                pus.id=to,pus.val=dis[to];
                q.push(pus);
            }
        }
    }
}
void init() {
    memset(head, -1, sizeof(head));
   // memset(edge, 0, sizeof edge);
    cnt=1;
}
void edgeadd(int from, int to, ll val){
    edge[cnt].from=from;
    edge[cnt].to=to;
    edge[cnt].val=val;
    edge[cnt].next=head[from];
    head[from]=cnt++;
}

int main()
{
    #ifndef ONLINE_JUDGE
    freopen("data.txt", "r", stdin);
    #endif

    int t;
    scanf("%d", &t);

    while(t--) {
        init();
        scanf("%d%d%d", &n, &m, &k);
//cout << n << ' ' << m << k << endl;
        rep(i, 1, m) {
            int x,y;
            ll z;
            scanf("%d%d%lld", &x, &y, &z);
            rep(i, 0, k) {
                edgeadd(x + i * n, y + i * n, z);
                if(i != k) {
                    edgeadd(x + i * n, y + ( i + 1 ) * n, 0);
                }
            }
        }

        dijikstra(1);
        ll ans = INF;
        rep(i, 0, k) {
            ans = min(ans, dis[n + i * n]);
        }
        printf("%lld\n", ans);
    }

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值