zoukankan      html  css  js  c++  java
  • Codeforces Round #324 (Div. 2) A, B, C, D, E

    A. Olesya and Rodion

    题目链接:

      codeforces 584A. Olesya and Rodion

    题目描述:

      给出n, t, 问是否存在一个n位数能被t整除,能的话输出这个数,否则输出-1.  题目太水,加上题目描述是为了确保此题是此题,此题非彼题(看的是同一个题目)

    解题思路:

      当n=1,t=10时输出-1,其他时候先输出一个t,然后其他剩余的位置用0填补。

    题目代码:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 int main ()
     5 {
     6     int n, t;
     7     while (scanf ("%d %d", &n, &t) != EOF)
     8     {
     9         if (n == 1 && t == 10)
    10             {
    11                 printf ("-1
    ");
    12                 continue;
    13             }
    14         if (t == 10)
    15             n --;
    16         printf ("%d", t);
    17         while (-- n)
    18             printf ("0");
    19         printf ("
    ");
    20     }
    21     return 0;
    22 }
    View Code

    B. Kolya and Tanya

    题目链接:

      codeforces 584 B. Kolya and Tanya

    题目描述:

      有一个圆环,圆环上有3*n个点,三个点组成一个正三角形。每个点上可以放1, 2 or 3个金币,n个正三角形里的金币总数不能同时等于6。问有多少种摆放方案?

    解题思路:

      每个正三角形有3^3种摆放方式,其中7种方式不合法,那么当有n个正三角形的时候,方案数就为27^n - 7^n.

    题目代码:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 typedef __int64 LL;
     5 const LL mod = 1000000007;
     6 int main ()
     7 {
     8     LL n, a, b;
     9     scanf("%I64d", &n);
    10     a = 27; b = 7;
    11     while (-- n)
    12     {
    13         a = (a * 27) % mod;
    14         b = (b * 7) % mod;
    15     }
    16     printf ("%I64d
    ", (a - b + mod) % mod);
    17     return 0;
    18 }
    View Code

    C. Marina and Vasya

    题目链接:

      codeforces 584 C. Marina and Vasya

    题目描述:

      给出长度都为n的字符串a, b, 输出一个新构造出的串s,存在s[i] == a[i] 的数目与 s[j] == b[j]的数目都等于k。不能构造出这样的新串s,输出-1.

    解题思路:

      怎么构造都可以,题目也是比较水的。扎扎的构造方式是:先把s串置成空,然后把a[i] == b[i]的位置选择前k位放置到s[i]里面。a[i] == b[i]的数目k'小于k的话,找前 k - k' 个 s[i] 为空的位置,使得a[i] == s[i], b字符串同理,然后把s串中当前还是空的位置,赋为!a也!b的字母。如果构造不成功就输出-1即可。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 
     4 typedef __int64 LL;
     5 const LL maxn = 100010;
     6 
     7 int main ()
     8 {
     9     int n, t;
    10     char a1[maxn], a2[maxn], ans[maxn];
    11 
    12     while (scanf ("%d %d", &n, &t) != EOF)
    13     {
    14         scanf ("%s %s", a1, a2);
    15         memset (ans, 0, sizeof(ans));
    16         t = n - t;
    17 
    18         for (int i=0; i<n; i++)
    19         {
    20             if (!t)
    21                 break;
    22             if (a1[i] == a2[i])
    23             {
    24                 ans[i] = a1[i];
    25                 t --;
    26             }
    27         }
    28         int resa1 = t;
    29         for (int i=0; i<n; i++)
    30         {
    31             if ( !resa1 )
    32                 break;
    33             if (!ans[i])
    34             {
    35                 ans[i] = a1[i];
    36                 resa1 --;
    37             }
    38         }
    39 
    40         int resa2 = t;
    41         for (int i=0; i<n; i++)
    42         {
    43             if ( !resa2 )
    44                 break;
    45             if (!ans[i])
    46             {
    47                 ans[i] = a2[i];
    48                 resa2 --;
    49             }
    50         }
    51 
    52         for (int i=0; i<n; i++)
    53             if ( !ans[i] )
    54             {
    55                 if (a1[i]!='a' && a2[i]!='a')
    56                     ans[i] = 'a';
    57                 else if (a1[i]!='b' && a2[i]!='b')
    58                     ans[i] ='b';
    59                 else
    60                     ans[i] = 'c';
    61             }
    62 
    63         if (resa1 || resa2)
    64             printf("-1
    ");
    65         else
    66             puts (ans);
    67 
    68     }
    69     return 0;
    70 }
    View Code

    D. Dima and Lisa

    题目链接:

      codeforces 584 D. Dima and Lisa

    题目描述:

      一个数,问是否可以由小于等于3个素数相加得出?可以输出素数的值,否则输出-1.

    解题思路:

      额!这个题目是我胡搞的,我先找出一个小于n但是距离n最近的素数x。由于n和x都是奇数,辣么n-x肯定是偶数咯,然后就拆分n-x,如果n-x可以拆成两个素数就输出这些素数,否则输出-1。写一发,测试实力过了,然后提交,心好虚!但是竟然过了,对的没错就是过了。事后听大家讨论题目好像有什么定理,然后找之。

      哥德巴赫猜想:任何大于2的偶数都可以用两个素数和来表示。扩展:任何大于5的奇数都可以用三个素数的和来表示。(n-x肯定可以表示为两个素数的和或者是2咯)

    题目代码:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 500;
     4 int mark[maxn], isprime[maxn];
     5 
     6 void init ()
     7 {
     8     int i, j;
     9     mark[0] = mark[1] = 1;
    10     for (i=2; i<=maxn; i++)
    11             for (j=i+i; j<maxn; j+=i)
    12                 mark[j] = 1;
    13 }
    14 
    15 int main ()
    16 {
    17     int n;
    18     init();
    19 
    20     while (scanf ("%d", &n) != EOF)
    21     {
    22         int i, a, b, c, m;
    23 
    24         for (a=n; ; a--)
    25         {
    26             if (a == n - 1)
    27                 continue;
    28             m = (int)sqrt (a);
    29 
    30             for (i=2; i<=m; i++)
    31                 if (a%i == 0)
    32                     break;
    33 
    34             if (i > m)
    35                 break;
    36 
    37         }
    38         if (a == n)
    39             printf ("%d
    %d
    ", 1, n);
    40         else if (a == n - 2)
    41             printf ("%d
    %d %d
    ", 2, 2, n-2);
    42         else
    43         {
    44             int m = n - a;
    45             for (i=2; i<=m-i; i++)
    46             {
    47                 if (!mark[i] && !mark[m-i])
    48                 {
    49                     c = i;
    50                     b = m - i;
    51                     break;
    52                 }
    53             }
    54 
    55             printf("%d
    %d %d %d
    ", 3, c, b, a);
    56         }
    57     }
    58     return 0;
    59 }
    View Code

    E. Anton and Ira

    题目链接:

      codeforces 584 E. Anton and Ira

    题目描述:

      给出两个长度都为n的序列a, b。两个序列中的数字都是[1, n]。现在可以交换a中的任意两个元素(a[i], a[j]),交换价值为|i - j|,问最小的交换价值为多少?可以使得strcmp(a, b) == 0.

    解题思路:

      其实这个题目就是需要向前换与需要向后换的数互换,为了保证最优,交换(a[i], a[j])的时候posb[a[i]]<=j && posb[a[j]]>=i.

      交换的次数还是很多的,结构体数组保存一直WA 14,最后突然醒悟,发现数组开小了........下次遇到不确定大小的数组保证开STL。

     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 #include <algorithm>
     5 using namespace std;
     6 
     7 const int maxn = 2010;
     8 int a[maxn], b[maxn], posb[maxn];
     9 struct node
    10 {
    11     int x, y;
    12     node (int a = 0, int b = 0):x(a),y(b) {}
    13 } p[10000000];
    14 
    15 int main ()
    16 {
    17     int n;
    18     while (scanf ("%d", &n) != EOF)
    19     {
    20         for (int i=1; i<=n; i++)
    21             scanf ("%d", &a[i]);
    22         for (int i=1; i<=n; i++)
    23         {
    24             scanf ("%d", &b[i]);
    25             posb[b[i]] = i;
    26         }
    27 
    28         int res, nu;
    29         res = nu = 0;
    30         for (int i=1; i<=n; i++)
    31         {
    32             int x = i;
    33             for (int j=i-1; j>0; j--)
    34             {
    35                 if (posb[a[j]]>=x && posb[a[x]]<=j)
    36                 {
    37                     swap (a[x], a[j]);
    38                     res += abs (x - j);
    39                     p[nu ++] = node(x, j);
    40                     x = j;
    41                 }
    42             }
    43         }
    44 
    45         printf ("%d
    %d
    ", res, nu);
    46         for (int i=0; i<nu; i++)
    47                 printf ("%d %d
    ", p[i].x, p[i].y);
    48     }
    49     return 0;
    50 }
    View Code
    本文为博主原创文章,未经博主允许不得转载。
  • 相关阅读:
    array and ram
    char as int
    pointer of 2d array and address
    Install SAP HANA EXPRESS on Google Cloud Platform
    Ubuntu remount hard drive
    Compile OpenSSL with Visual Studio 2019
    Install Jupyter notebook and tensorflow on Ubuntu 18.04
    Build OpenCV text(OCR) module on windows with Visual Studio 2019
    Reinstall VirtualBox 6.0 on Ubuntu 18.04
    Pitfall in std::vector<cv::Mat>
  • 原文地址:https://www.cnblogs.com/alihenaixiao/p/4864282.html
Copyright © 2011-2022 走看看