CCF-CSP_201912(第18次)

1.报数

#include<cstdio>

bool judge(int n)
{
    int t = 0;
    while(n && t!=7){
        t = n%10;
        n /= 10;
    }
    return t == 7 ? true : false;
}

int main()
{
    int num ,total = 1;
    int skip[5] = {0};
    
    scanf("%d", &num);
    for(int i = 1; total <= num; ++i){ //注意这里是<不是<=
        if( i%7 == 0|| judge(i)) skip[(i-1)%4]++;
        else total++;
    }
    for(int i = 0;i < 4; i++)
        printf("%d\n",skip[i]);
    
    return 0;
}
66

7
5
11
5

2.回收站选址


#include<cstdio>
#include<set>
using namespace std;

struct Point{
    int x, y;
    
    Point(int _x, int _y): x(_x), y(_y){}
    
    bool operator < (const Point &rhs) const {
        if(x == rhs.x) return y<rhs.y;
        return x < rhs.x;
    }
};
set<Point> pts;

bool Pfind(int x, int y){
    if(pts.find(Point(x, y)) != pts.end()) return true;
    return false;
}

int main(){
    int n, x, y;
    int ans[5]={0};

    scanf("%d", &n);//n trash locations
    while(n--){
        scanf("%d%d", &x, &y);
        pts.insert(Point(x, y));
    }
    
    set<Point>::iterator it;
    for(it = pts.begin(); it != pts.end(); it++){
        x = (*it).x;
        y = (*it).y;
        if(Pfind(x, y+1) && Pfind(x, y-1) && Pfind(x+1, y) && Pfind(x-1, y) ){
            int cnt = 0;
            if(Pfind(x-1, y+1)) cnt++;
            if(Pfind(x+1, y+1)) cnt++;
            if(Pfind(x-1, y-1)) cnt++;
            if(Pfind(x+1, y-1)) cnt++;
            ans[cnt]++;
        }
    }
    
    for(int i=0; i<5; ++i)
        printf("%d\n", ans[i]);
    
    return 0;
}
7
1 2
2 1
0 0
1 1
1 0
2 0
0 1

0
0
1
0
0

3.化学方程式


#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
using namespace std;

int get(int &pos, string &s){
    int k = 0;
    while (s[pos] <= 57 && s[pos] >= 48){//digit:0~9
        k = (s[pos] - 48) + k * 10;
        pos++;
    }
    //pos points to the last digit, pos is -1 when s[0] is not a digit
    pos--;
    if (k != 0) return k;
    else return 1;
}

//The process can be divided into equation, chemical formula and element.
void Count(string &s, size_t len, map<int, int> &ma){
    int ex[1000][3];
    for (int i = 0; i < 1000; i++) {
  //negative:Mark the first element after the left bracket
    //1:Mark the beginning of a chemical formula
        ex[i][0] = 0;
        ex[i][1] = 1; //Store letters and coefficients
        ex[i][2] = 1; //Number of elements
    }
    
    int pos = 0, num = 0;
    int co = get(pos, s);
    ex[num][0] = 1;//A chemical formula begins
    ex[num][1] = co;//Coefficient before chemical formula
    pos++; num++;
    
    while (pos < len){
        if (s[pos] <= 90 && s[pos] >= 65){//upper letter
            ex[num][1] = s[pos];
            num++;
        }else if (s[pos] <= 122 && s[pos] >= 97){//lower letter
            ex[num - 1][1] = ex[num - 1][1] * 123 + s[pos];
            //with previous upper letter form an element,and mapped to a number,not ascall code
        }else if (s[pos] == '+'){
            pos++;
            int u = get(pos, s);
            ex[num][0] = 1;//A chemical formula begins
            ex[num][1] = u;//Coefficient before chemical formula
            num++;
        }else if (s[pos] <= 57 && s[pos] >= 48){//digit:0~9
            co = get(pos, s);//Coefficient after the letter
            ex[num - 1][2] = co;
        }else if (s[pos] == '('){
            ex[num][0]--;//negative:Mark the first element after the left bracket
        }else if (s[pos] == ')'){
            pos++;
            co = get(pos, s);//Coefficient after closing bracket
            int j = num - 1;
            while (ex[j][0] >= 0){
        //elements in bracket double,except for the first element after the left bracket
                ex[j][2] *= co;
                j--;
            }
            ex[j][2] *= co;//the first element after the left bracket
            ex[j][0]++;//revoke mark
        }
        pos++;
    }
    
    int coef= 0;
    for (int u = 0; u < num; u++){
        if (ex[u][0] == 1) coef = ex[u][1];//Coefficient before chemical formula
        else ma[ex[u][1]] += ex[u][2] * coef;//mapped
    }
}

int Deal(){//Input and Statistic
    string str, lstr, rstr;
    map<int, int> lmap, rmap;  //first:element->digit   second:number
    
    cin >> str;
    stringstream ss(str);
    getline(ss, lstr, '=');
    getline(ss, rstr);
    
    Count(lstr, lstr.length(), lmap);
    Count(rstr, rstr.length(), rmap);
    
    map<int, int>::iterator iter;
    map<int, int>::iterator iter2;
    if (lmap.size() != rmap.size()) return 1;
    //Compare two maps
    for (iter = lmap.begin(),iter2 = rmap.begin(); iter != lmap.end(); iter++){
        if ((iter->first != iter2->first) || (iter->second != iter2->second)) return 1;
        iter2++;
    }
    return 0;
}

int main(){
    char str[2] = {'Y', 'N'};
    int n;
    
    cin >> n;//n line chemical formula
    for (int i = 0; i < n; i++){
        int k = Deal();
        
        cout << str[k] << endl;
    }
    
    return 0;
}
#include<iostream>
#include<string>
#include<sstream>
#include<map>
#include<vector>
using namespace std;

struct Element{
    string name; //名称
    int num;
    
    Element(string _name, int _num): name(_name), num(_num){}
};

int getNum(string s, int &pos){
    int k = 0;
    while(isdigit(s[pos])){
        k = k * 10 + s[pos] - '0';
        pos ++;
    }
    return k;
}

void Deal(string &s, map<string, int> &mp){
    stringstream ss(s);
    string fo;//formula
    
    while(getline(ss, fo, '+')){ //Get a chemical formula
        vector<Element> et;
        int pos = 0, co, coef = 1;
        
        if(isdigit(fo[pos]))
            coef = getNum(fo, pos); //Chemical formula coefficient
    
        while(pos < fo.size()){
            if(isdigit(fo[pos])){ //digit
                co = getNum(fo, pos);
                if(et[et.size() - 1].name == ")"){ //previous element is right bracket
                    size_t j = et.size() - 1;
                    et[j].name = "*"; //mark right bracket,easy to deal
                    while(et[--j].name != "("){
                        et[j].num *= co;//element in bracker double
                    }
                    et[j].name="*"; //mark left bracket,easy to deal
                }else//previous element is letter
                    et[et.size()-1].num *= co;
            }
            else if(fo[pos] == '('){
                et.push_back(Element("(", 0));
                pos++;
            }
            else if(fo[pos] == ')'){
                et.push_back(Element(")", 0));
                if(pos + 1 == fo.size() || !isdigit(fo[pos + 1]))
                    fo.insert(pos + 1, "1"); //easy to deal
                pos++;
            }
            else if(isupper(fo[pos])){
                string name = "";
                name += fo[pos];
                pos++;
                if(islower(fo[pos])){
                    name += fo[pos];
                    pos++;
                }
                et.push_back(Element(name, 1));
            }
        }
        
        for(int i = 0; i != et.size(); ++i){
            if(et[i].name == "*") continue;//delete bracket element
            mp[et[i].name] += et[i].num * coef;
        }
    }
}

bool judge(map<string, int> &lmp, map<string, int> &rmp){ //compare 2 maps
    if(lmp.size() != rmp.size()) return false;
    for(map<string, int>::iterator it = lmp.begin(); it != lmp.end(); ++it){
        if(rmp[it->first] != it->second) return false;
    }
    return true;
}

int main(){
    int n;
    
    scanf("%d", &n);
    for(int i = 0; i < n; ++i){
        map<string, int> lmap, rmap;
        string str, lstr, rstr;
    
        cin >> str;
        stringstream ss(str);
        getline(ss, lstr, '=');
        getline(ss, rstr);
    
        Deal(lstr, lmap);
        Deal(rstr, rmap);
        
        judge(lmap, rmap) ? cout <<"Y"<< endl : cout <<"N"<< endl;
    }
    
    return 0;
}
11
H2+O2=H2O
2H2+O2=2H2O
H2+Cl2=2NaCl
H2+Cl2=2HCl
CH4+2O2=CO2+2H2O
CaCl2+2AgNO3=Ca(NO3)2+2AgCl
3Ba(OH)2+2H3PO4=6H2O+Ba3(PO4)2
3Ba(OH)2+2H3PO4=Ba3(PO4)2+6H2O
4Zn+10HNO3=4Zn(NO3)2+NH4NO3+3H2O
4Au+8NaCN+2H2O+O2=4Na(Au(CN)2)+4NaOH
Cu+As=Cs+Au
 
N
Y
N
Y
Y
Y
Y
Y
Y
Y
N

4.区块链

// 201912-4 区块链.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#include <iostream>
#include <vector>
#include <deque>
#include <string.h>
#define MAXN 510
#define MAXM 10010
using namespace std;
int n, m, cnt;//n个点,m条边
int cur; //当前时间
int interval;

struct Packet
{
    vector<int> link;
    int t; //送达时刻
};

struct Edge
{
    int to;
    int next;
}edge[MAXM << 1];
int head[MAXN];
vector<int> node[MAXN];

//数据包队列
deque<Packet> que[MAXN];
//数据包时刻队列
deque<int> tque;
void sendPacket(int i, int t) //发送节点i,发送时刻t
{
    Packet p = { node[i], t + interval };

    if (!que[i].empty() && que[i].back().t == p.t) //清除过期数据包
        que[i].pop_back();

    que[i].push_back(p);
    if (tque.empty() || tque.back() != p.t) //更新数据包时刻队列
        tque.push_back(p.t);
}

void transmitOneSec()
{
    cur++;
    if (tque.empty() || tque.front() != cur)return;
    tque.pop_front();
    for (int i = 1; i <= n; i++)
    {
        bool flag = false;
        for (int j = head[i]; ~j; j = edge[j].next)
        {
            int u = edge[j].to;

            while (!que[u].empty() && que[u].front().t < cur)
            {
                que[u].pop_front();
            }

            if (!que[u].empty() && que[u].front().t == cur)
            {
                //数据包(主链)
                vector<int>& link = que[u].front().link;

                if (node[i].size() < link.size() ||
                    node[i].back() > link.back() && node[i].size() == link.size())
                {
                    node[i] = link;
                    flag = true;
                }
            }
        }
        if (flag)
            sendPacket(i, cur); //接收完邻居的数据包后向外传递数据包
    }
}

void transmitToCurrent(int t)
{
    //传递到当前时刻(每秒更新一次)
    while (t != cur)
    {
        transmitOneSec();
    }
}

//a在t时刻产生了编号为c的块
void generate(int a, int t, int c)
{
    transmitToCurrent(t);
    node[a].push_back(c);
    sendPacket(a, t);
}

void search(int a, int t)
{
    transmitToCurrent(t);
    cout << node[a].size() << " ";
    size_t size = node[a].size();
    for (int i = 0; i < size; i++)
    {
        cout << node[a][i];
        if (i != size - 1)
            cout << " ";
    }
    cout << endl;
}

void init()
{
    memset(head, -1, sizeof(head));

    for (int i = 1; i <= n; i++)
    {
        node[i].push_back(0);
    }
}

void addEdge(int u, int v)
{
    edge[cnt].to = v;
    edge[cnt].next = head[u]; //与这个边起点相同的上一条边的编号
    head[u] = cnt++; //更新以u为起点最后一条边的编号
}

int main()
{
    IOS
    cin >> n >> m;
    init();
    int u, v, k, a, b, c;
    for (int i = 0; i < m; i++)
    {
        cin >> u >> v;
        addEdge(u, v);
        addEdge(v, u);
    }
    cur = 0;
    cin >> interval >> k;
    for (int i = 0; i < k; i++)
    {
        cin >> a >> b;
        if (cin.get() == '\n')
        {
            search(a, b);
        }
        else
        {
            cin >> c;
            generate(a, b, c);
        }
    }
}
5 10
1 2
1 3
1 4
1 5
2 3
2 4
2 5
3 4
3 5
4 5
1 27
1 1 1
2 1 2
3 1 3
4 1 4
5 1 5
1 1
2 1
3 1
4 1
5 1
1 2
2 2
3 2
4 2
5 2
1 10 10
2 11 9
1 11
2 11
3 11
4 11
5 11
1 12
2 12
3 12
4 12
5 12


2 0 1
2 0 2
2 0 3
2 0 4
2 0 5
2 0 1
2 0 1
2 0 1
2 0 1
2 0 1
3 0 1 10
4 0 1 10 9
3 0 1 10
3 0 1 10
3 0 1 10
4 0 1 10 9
4 0 1 10 9
4 0 1 10 9
4 0 1 10 9
4 0 1 10 9
15 13
1 2
2 3
3 4
4 5
1 6
6 7
7 8
8 9
1 10
10 11
11 12
12 13
14 15
6 28
1 1 1
1 2 2
1 6
2 7
13 7
9 7
5 7
3 14
8 14
5 14
11 14
9 25
5 25
13 25
9 29 3
5 29 4
13 29 5
1 53
2 59 6
2 59
1 1000
3 1000
8 1000
9 1000
10 1000
13 1000
14 1000
15 1000


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

5.魔数(未解决)

//(https://blog.csdn.net/qq_21555569/article/details/108318407?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.control)
// 201912-5 魔数.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
//#define LOCAL
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#include <iostream>
#include <queue>
#include <unordered_map>
#include <string.h>
#define MOD 2009731336725594113
#define MAXN 1000010
typedef unsigned long long ull;
using namespace std;
int l, r, n, q;
ull f[32];
int g[32][32];
int a[32][MAXN];
unordered_map<ull, int>mp;
ull U[5] = {
    314882150829468584,
    427197303358170108,
    1022292690726729920,
    1698479428772363217,
    2006101093849356424
};

inline ull quickMul(ull a, ull b, ull mod)
{
    a %= mod;
    b %= mod;
    ull res = 0;
    while (b) {
        if (b & 1) {
            res += a;
            if (res >= mod)
                res -= mod;
        }
        b >>= 1;
        a <<= 1;
        if (a >= mod)  a -= mod;
    }
    return res;
}

void transmit()
{
    int index = 0;
    queue<ull> que;
    for (int i = 0; i < 5; i++)
    {
        que.push(U[i]);
        mp[U[i]] = index++;
        f[i] = U[i];
    }
    while (!que.empty())
    {
        ull e = que.front();
        que.pop();
        for (int i = 0; i < 5; i++)
        {
            ull x = quickMul(e, U[i], MOD);
            if (mp.count(x))
            {
                g[mp[e]][i] = g[i][mp[e]] = mp[x]; //转移矩阵[index1][index2]-->[index3]
                continue;
            }
            g[mp[e]][i] = g[i][mp[e]] = index;
            f[index] = x; //f[index] = value
            mp[x] = index; //mp[value] = index
            que.push(x);
            index++;
        }
    }

    for (int i = 5; i < 32; i++)
    {
        for (int j = 5; j < 32; j++)
        {
            g[i][j] = g[j][i] = mp[quickMul(f[i], f[j], MOD)];
        }
    }
}
int temp[32];
struct Tree
{
    int l, r;
    int res = 0;
    int tag = -1;
    int s[32]; //s[i]中存储如果当前区间乘以序号为i的数,res会转移成哪个数
    void trans(int t)
    {
        //因为s[i]中保存的是(该区间)乘以f[i]之后的结果(和)
        //因为当前要乘f[t],**所以s[i]需要转移成(该区间)乘以f[t]之后**再乘以f[i]的结果
        //要知道乘以f[t]之后再乘以f[i]相当于乘了一个f[g[i][t]]
        //我们只需要找(该区间)乘以f[g[i][t]]之后的结果即s[g[i][t]]
        //即s[i]-->s[g[i][t]]
        for (int i = 0; i < 32; i++)
        {
            temp[i] = s[g[i][t]]; //转移
        }
        memcpy(s, temp, sizeof(temp));
        res = s[27]; //res修改为转移后的当前值
        if (~tag)tag = g[tag][t];
        else tag = t;
    }
}tree[MAXN<<2];

inline void pushDown(int i)
{
    if (~tree[i].tag)
    {
        tree[i << 1].trans(tree[i].tag);
        tree[i << 1 | 1].trans(tree[i].tag);
        tree[i].tag = -1;
    }
}

inline void transUp(int i)
{
    Tree& lnode = tree[i << 1], & rnode = tree[i << 1 | 1];
    for (int j = 0; j < 32; j++)
        tree[i].s[j] = lnode.s[j] + rnode.s[j];
    tree[i].res = tree[i].s[27]; //相当于当前区间的和乘以f[27]=1之后的结果,即当前区间和
}

void build(int i, int l, int r)
{
    tree[i].l = l;
    tree[i].r = r;
    if (l == r) //到达叶子结点
    {
        for (int j = 0; j < 32; j++)
        {
            tree[i].s[j] = a[j][l]; //初始化s[i]
        }
        //tree[i].res = ((l * 1) % 2009731336725594113) % 2019
        tree[i].res = tree[i].s[27];
        return;
    }
    int mid = (l + r) >> 1;
    build(i << 1, l, mid);
    build(i << 1 | 1, mid + 1, r);
    transUp(i);
}

inline void mul(int i, int l, int r, int k) //k为乘数序号
{
    if (tree[i].r < l || tree[i].l > r)
        return;
    if (tree[i].l >= l && tree[i].r <= r) //完全包含
    {
        tree[i].trans(k);
        return;
    }
    pushDown(i);
    if (tree[i << 1].r >= l)
        mul(i << 1, l, r, k);
    if (tree[i << 1 | 1].l <= r)
        mul(i << 1 | 1, l, r, k);
    transUp(i);
}

inline int getSum(int i, int l, int r)
{
    if (tree[i].r < l || tree[i].l > r)
        return 0;
    if (tree[i].l >= l && tree[i].r <= r)
        return tree[i].res;
    pushDown(i);
    int res = 0;
    if (tree[i << 1].r >= l)
        res += getSum(i << 1, l, r);
    if (tree[i << 1 | 1].l <= r)
        res += getSum(i << 1 | 1, l, r);
    return res;
}

void init()
{
    transmit();
    cin >> n >> q;
    for (int i = 0; i < 32; i++)
    {
        ull res = 0;
        for (int j = 0; j <= n; j++)
        {
            a[i][j] = res % 2019;
            res = (res + f[i]) % MOD;
        }
    }
    build(1, 1, n);
}

int main()
{
#ifdef LOCAL
    FILE* stream;
    freopen_s(&stream, "in.txt", "r", stdin);
#endif // LOCAL
    IOS
    init();
    for (int i = 0; i < q; i++)
    {
        cin >> l >> r;
        int s = getSum(1, l, r);
        cout << s << endl;
        int t = s % 5;
        mul(1, l, r, t);
    }
}

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

1785
5741
10423
24915
1647
2154
1872
8559
12936
60048
52916
79974
61897
50541
16819
15646
48044
30156
14581
6906
17346
45805
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






## 1. 报数
```cpp
/* CCF201912-1 报数 */
//简单模拟

using namespace std;

int have7(int n){
    while(n)
        if(n%10==7)
            return 1;
        else  n/=10;
    return 0;
}

int main(){
    int n,num=1,count=1,cnt[4]={0};
    
    cin>>n;
    while(count<=n){
        if(num%7==0||have7(num))
            cnt[(num-1)%4]++;
        else
            count++;
        num++;
    }
    
    for(int i=0;i<4;i++)
        cout<<cnt[i]<<endl;
    
    return 0;
}
/*
66

7
5
11
5
*/

2.回收站选址

/* CCF201912-2 回收站选址 */
//序列+map映射+pair绑定
#include<iostream>
#include<cstdio>
#include<map>
using namespace std;

const int N = 1000;
const int Mark = 4;
int cnt[Mark + 1];
pair<int, int> p[N];     //相当于结构体

int main()
{
    int n;
    map<pair<int, int>, int> ps;//映射
    
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        int x, y;
        scanf("%d%d", &x, &y);
        p[i] = make_pair(x, y);//绑定
        ps[p[i]] = 1;//映射
    }
    for(int i = 0; i < n; i++) {
        int x = p[i].first;
        int y = p[i].second;
        //四个邻居位置都存在
        if(ps[make_pair(x, y -1)] && ps[make_pair(x, y + 1)] &&
           ps[make_pair(x - 1, y)] && ps[make_pair(x + 1, y)])
        //四个角存在个数
            cnt[ps[make_pair(x - 1, y - 1)] + ps[make_pair(x - 1, y + 1)] +
                ps[make_pair(x + 1, y - 1)] + ps[make_pair(x + 1, y + 1)]]++;
    }
        //输出分数0~4选址的个数
    for(int i = 0; i <= Mark; i++)
        printf("%d\n", cnt[i]);
    
    return 0;
}
11
9 10
10 10
11 10
12 10
13 10
11 9
11 8
12 9
10 9
10 11
12 11
 
0
2
1
0
0

3. 化学方程式

/* CCF201912-3 化学方程式 */
//序列处理
#include<iostream>
#include<sstream> //istringstream、ostringstream 和 stringstream
#include<cstdio>
#include<cstring>
#include<cctype>
#include<string>
#include<map>
#include<vector>
using namespace std;

struct Element{ //元素
    string name;
    int num;
    Element(string _name, int _num): name(_name), num(_num){}
};

int get_whole_number(string str, int &pos){ //从str的pos位置开始,得到一个完整数字
    int num=0;
    while(isdigit(str[pos])){
        num=num*10+str[pos]-'0';
        pos++;
    }
    return num;
}

void calc(string &from_equation_str, map<string, int> &mp){
    stringstream ss(from_equation_str);
    string _formula;//单个化学式
    
    while(getline(ss, _formula, '+')){ //以+号为分割,获取每一个化学式,如 32Ba((OH)2(CO3)2)3
        vector<Element> singal_elem; //存储化学式的分解序列,如 Ba、(、(、O、H、)、(、C、O、)、)
        int i=0,formula_factor=1; //整个化学式的系数,默认为1
        
        if(isdigit(_formula[i]))//计算化学式项系数
            formula_factor=get_whole_number(_formula,i);
         
        while(i<_formula.size()){
            //1.该位置为数字(不是项系数)
            if(isdigit(_formula[i])){
                int num=get_whole_number(_formula,i);
                //1.1如果上个元素化学为右括号,统计括号内字母
                if(singal_elem[singal_elem.size()-1].name==")"){
                    int j=(int)singal_elem.size()-1;
                    
                    singal_elem[j].name="*"; //将右括号标记为*,忽略它的存在
                    while(singal_elem[--j].name!="("){//括号内所有字符翻倍
                        singal_elem[j].num*=num;
                    }
                    singal_elem[j].name="*"; //将左括号标记为*,忽略它的存在
                }
                else//1.2如果上个元素为字母
                    singal_elem[singal_elem.size()-1].num*=num;
            }
             //2.该位置为左括号
            else if(_formula[i]=='('){
                singal_elem.push_back(Element("(", 0));
               
                i++;
            }
            //3.该位置为右括号
            else if(_formula[i]==')'){
                singal_elem.push_back(Element(")", 0));
                
                if(i+1==_formula.size() || !isdigit(_formula[i+1]))
                    _formula.insert(i+1,"1"); //考虑到右括号右边可能不出现数字,补充底数1,便于计算
                i++;
            }
            //4.该位置为大写字母
            else if(isupper(_formula[i])){
                string name="";
                name+=_formula[i];
                i++;
                if(islower(_formula[i])){ //如果后面有小写字母
                    name+=_formula[i];
                    i++;
                }
                singal_elem.push_back(Element(name,1));
            }
        }
        
        
        for(int i=0; i!=singal_elem.size(); ++i){ //将“元素->个数”保存到map中
            if(singal_elem[i].name=="*")
                continue; //忽略序列中括号的存在
            mp[singal_elem[i].name]+=singal_elem[i].num*formula_factor;
        }    
    }   
}

bool judge(map<string, int> &left, map<string, int> &right){ //判断两个map是否相同
    if(left.size()!=right.size())
        return false;
    for(map<string, int>::iterator it=left.begin(); it!=left.end(); ++it){
        if(right[it->first]!=it->second)
            return false;
    }
    return true;
}

int main(){
    int n;
    
    scanf("%d", &n);
    
    for(int i=0; i<n; ++i){
        map<string, int> equation_left, equation_right;//hash映射
        string input_str, equal_sign_left_str, equal_sign_right_str;
        
        cin>>input_str;
        stringstream ss(input_str);
        getline(ss, equal_sign_left_str,'='); //得到等号左边的字符串
        getline(ss, equal_sign_right_str); //得到等号右边的字符串
    
        //计算左字符串
        calc(equal_sign_left_str, equation_left);
        calc(equal_sign_right_str, equation_right);
        
        if(judge(equation_left, equation_right))
            cout<<"Y"<<endl;
        else
            cout<<"N"<<endl;
    }
    return 0;
}
#include <algorithm>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
using namespace std;

int get(int &num, string &s){
    int k = 0;
    while (s[num] <= 57 && s[num] >= 48){//数字0~9
        k = (s[num] - 48) + k * 10;
        num++;
    }
    num--;
    if (k != 0){
        return k;
    }else{
        return 1;
    }
}

void doo(string &s, int len, map<int, int> &ma){
    int ex[1000][3];
    
    for (int i = 0; i < 1000; i++) {
        ex[i][0] = 0; //括号
        ex[i][1] = 1; //元素:”字母“、”化学式前系数“
        ex[i][2] = 1; //元素个数(默认1)
    }
    int num = 0,element_num = 0;
    int co = get(num, s);
    ex[element_num][0] = 1;//1:化学式开始
    ex[element_num][1] = co;//co:化学式前系数
    num++;element_num++;
    while (num < len){
        if (s[num] <= 90 && s[num] >= 65){//大写字母元素
            ex[element_num][1] = s[num];
            element_num++;
        }else if (s[num] <= 122 && s[num] >= 97){//小写字母元素
            ex[element_num - 1][1] = ex[element_num - 1][1] * 123 + s[num];//并到大写字母元素中,大写和小写字母映射为数字,并不是ascall码
        }else if (s[num] == '+'){
            num++;
            int u = get(num, s);
            ex[element_num][0] = 1;//1:化学式开始
            ex[element_num][1] = u;//u:化学式前系数
            element_num++;
        }else if (s[num] <= 57 && s[num] >= 48){//数字0~9
            co = get(num, s);
            ex[element_num - 1][2] = co;//字母后的系数
        }else if (s[num] == '('){
            ex[element_num][0]--;//--:左括号开始后一个元素标记
        }else if (s[num] == ')'){
            num++;
            co = get(num, s);//co:括号后的系数
            int j = element_num - 1;
            while (ex[j][0] >= 0){//括号内的元素加倍
                ex[j][2] *= co;
                j--;
            }
            ex[j][2] *= co;//左括号开始后一个元素位置
            ex[j][0]++;//恢复
        }
        num++;
    }
    
    int coef= 0;
    for (int u = 0; u < element_num; u++){//获取化学式前系数,并将字母元素和其个数进行绑定
        if (ex[u][0] == 1) {//化学式前系数
            coef = ex[u][1];
        }else{
            ma[ex[u][1]] += ex[u][2] * coef;//绑定<元素、个数>(只含字母)
        }
    }
}
int handle(){
    string str, lstr, rstr;
    map<int, int> lmap, rmap;  //first:(复合字母)元素映射为数字   second:为元素个数
    
    cin >> str;
    stringstream ss(str);
    getline(ss, lstr, '=');
    getline(ss, rstr);
    doo(lstr, (int)lstr.length(), lmap);
    doo(rstr, (int)rstr.length(), rmap);
    map<int, int>::iterator iter;
    map<int, int>::iterator iter2;
    if (lmap.size() != rmap.size()){
        return 1;
    }
    for (iter = lmap.begin(),iter2 = rmap.begin(); iter != lmap.end(); iter++){
        if ((iter->first != iter2->first) || (iter->second != iter2->second)){
            return 1;
        }
        iter2++;
    }
    return 0;
}

int main(){
    char str[2] = {'Y', 'N'};
    int n;
    cin >> n;
    for (int i = 0; i < n; i++){
        int k = handle();
        cout << str[k] << endl;
    }
}
11
H2+O2=H2O
2H2+O2=2H2O
H2+Cl2=2NaCl
H2+Cl2=2HCl
CH4+2O2=CO2+2H2O
CaCl2+2AgNO3=Ca(NO3)2+2AgCl
3Ba(OH)2+2H3PO4=6H2O+Ba3(PO4)2
3Ba(OH)2+2H3PO4=Ba3(PO4)2+6H2O
4Zn+10HNO3=4Zn(NO3)2+NH4NO3+3H2O
4Au+8NaCN+2H2O+O2=4Na(Au(CN)2)+4NaOH
Cu+As=Cs+Au
 
N
Y
N
Y
Y
Y
Y
Y
Y
Y
N

4. 区域链



5.魔数

// 201912-5 魔数.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
//#define LOCAL
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#include <iostream>
#include <queue>
#include <unordered_map>
#include <string.h>
#define MOD 2009731336725594113
#define MAXN 1000010
typedef unsigned long long ull;
using namespace std;
int l, r, n, q;
ull f[32];
int g[32][32];
int a[32][MAXN];
unordered_map<ull, int>mp;
ull U[5] = {
    314882150829468584,
    427197303358170108,
    1022292690726729920,
    1698479428772363217,
    2006101093849356424
};

inline ull quickMul(ull a, ull b, ull mod)
{
    a %= mod;
    b %= mod;
    ull res = 0;
    while (b) {
        if (b & 1) {
            res += a;
            if (res >= mod)
                res -= mod;
        }
        b >>= 1;
        a <<= 1;
        if (a >= mod)  a -= mod;
    }
    return res;
}

void transmit()
{
    int index = 0;
    queue<ull> que;
    for (int i = 0; i < 5; i++)
    {
        que.push(U[i]);
        mp[U[i]] = index++;
        f[i] = U[i];
    }
    while (!que.empty())
    {
        ull e = que.front();
        que.pop();
        for (int i = 0; i < 5; i++)
        {
            ull x = quickMul(e, U[i], MOD);
            if (mp.count(x))
            {
                g[mp[e]][i] = g[i][mp[e]] = mp[x]; //转移矩阵[index1][index2]-->[index3]
                continue;
            }
            g[mp[e]][i] = g[i][mp[e]] = index;
            f[index] = x; //f[index] = value
            mp[x] = index; //mp[value] = index
            que.push(x);
            index++;
        }
    }

    for (int i = 5; i < 32; i++)
    {
        for (int j = 5; j < 32; j++)
        {
            g[i][j] = g[j][i] = mp[quickMul(f[i], f[j], MOD)];
        }
    }
}
int temp[32];
struct Tree
{
    int l, r;
    int res = 0;
    int tag = -1;
    int s[32]; //s[i]中存储如果当前区间乘以序号为i的数,res会转移成哪个数
    void trans(int t)
    {
        //因为s[i]中保存的是(该区间)乘以f[i]之后的结果(和)
        //因为当前要乘f[t],**所以s[i]需要转移成(该区间)乘以f[t]之后**再乘以f[i]的结果
        //要知道乘以f[t]之后再乘以f[i]相当于乘了一个f[g[i][t]]
        //我们只需要找(该区间)乘以f[g[i][t]]之后的结果即s[g[i][t]]
        //即s[i]-->s[g[i][t]]
        for (int i = 0; i < 32; i++)
        {
            temp[i] = s[g[i][t]]; //转移
        }
        memcpy(s, temp, sizeof(temp)); 
        res = s[27]; //res修改为转移后的当前值
        if (~tag)tag = g[tag][t];
        else tag = t;
    }
}tree[MAXN<<2];

inline void pushDown(int i)
{
    if (~tree[i].tag)
    {
        tree[i << 1].trans(tree[i].tag);
        tree[i << 1 | 1].trans(tree[i].tag);
        tree[i].tag = -1;
    }
}

inline void transUp(int i)
{
    Tree& lnode = tree[i << 1], & rnode = tree[i << 1 | 1];
    for (int j = 0; j < 32; j++)
        tree[i].s[j] = lnode.s[j] + rnode.s[j];
    tree[i].res = tree[i].s[27]; //相当于当前区间的和乘以f[27]=1之后的结果,即当前区间和
}

void build(int i, int l, int r)
{
    tree[i].l = l;
    tree[i].r = r;
    if (l == r) //到达叶子结点
    {
        for (int j = 0; j < 32; j++)
        {
            tree[i].s[j] = a[j][l]; //初始化s[i]
        }
        //tree[i].res = ((l * 1) % 2009731336725594113) % 2019
        tree[i].res = tree[i].s[27]; 
        return;
    }
    int mid = (l + r) >> 1;
    build(i << 1, l, mid);
    build(i << 1 | 1, mid + 1, r);
    transUp(i);
}

inline void mul(int i, int l, int r, int k) //k为乘数序号
{
    if (tree[i].r < l || tree[i].l > r)
        return;
    if (tree[i].l >= l && tree[i].r <= r) //完全包含
    {
        tree[i].trans(k);
        return;
    }
    pushDown(i);
    if (tree[i << 1].r >= l)
        mul(i << 1, l, r, k);
    if (tree[i << 1 | 1].l <= r)
        mul(i << 1 | 1, l, r, k);
    transUp(i);
}

inline int getSum(int i, int l, int r)
{
    if (tree[i].r < l || tree[i].l > r)
        return 0;
    if (tree[i].l >= l && tree[i].r <= r)
        return tree[i].res;
    pushDown(i);
    int res = 0;
    if (tree[i << 1].r >= l)
        res += getSum(i << 1, l, r);
    if (tree[i << 1 | 1].l <= r)
        res += getSum(i << 1 | 1, l, r);
    return res;
}

void init()
{
    transmit();
    cin >> n >> q;
    for (int i = 0; i < 32; i++)
    {
        ull res = 0;
        for (int j = 0; j <= n; j++)
        {
            a[i][j] = res % 2019;
            res = (res + f[i]) % MOD;
        }
    }
    build(1, 1, n);
}

int main()
{
#ifdef LOCAL
    FILE* stream;
    freopen_s(&stream, "in.txt", "r", stdin);
#endif // LOCAL
    IOS
    init();
    for (int i = 0; i < q; i++)
    {
        cin >> l >> r;
        int s = getSum(1, l, r);
        cout << s << endl;
        int t = s % 5;
        mul(1, l, r, t);
    }
}
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

1785
5741
10423
24915
1647
2154
1872
8559
12936
60048
52916
79974
61897
50541
16819
15646
48044
30156
14581
6906
17346
45805
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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值