April Fools Day Contest 2016 (656A,656B,656C,656D,656E,656F)

来源:互联网 发布:sql function函数 编辑:程序博客网 时间:2024/06/04 19:17

Da Vinci Powers

题目链接:

http://codeforces.com/problemset/problem/656/A

解题思路:

This problem asked to figure out an integer sequence from two samples and problem title. It turned out to be surprisingly hard, a lot harder than I anticipated. A quick search through OEIS shows that while there are a lot of sequences which have these two numbers in them, only one is related to Leonardo da Vinci (and if you're looking for da Vinci, there are only two sequences overall). http://oeis.org/A221180 is an erroneous series of powers of 2, written down by da Vinci in his diaries and available as part of "Codex Madrid I".

AC代码:

#include <bits/stdc++.h>using namespace std;typedef long long ll;ll a[] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8092, 16184, 32368, 64736, 129472, 258944, 517888, 1035776, 2071552, 4143104, 8286208, 16572416, 33144832, 66289664, 132579328, 265158656, 530317312, 1060634624, 2121269248, 4242538496LL, 8485076992LL, 16970153984LL, 33940307968LL};int main(){    int n;    while(~scanf("%d",&n)){        printf("%lld\n",a[n]);    }    return 0;}

Scrambled

题目链接:

http://codeforces.com/problemset/problem/656/B

解题思路:

Just one word: typoglycemia. The urban legend (unsupported by any known research) claims that people can easily read text even if letters in each word are scrambled, as long as the first and the last letters stay in place. Looked like a fun thing to verify on a small (and strongly biased) sample of competitive programmers. Judging from the results, it's really not much of an obstacle :-)

When un-scrambled, the lengthy statement becomes a story about two roommates who are trying to figure out a fair way to split dishes washing duty. (Fun fact: this problem idea is actually inspired not by any real-life events but by a theater play.) "You agree on two arrays of integers M and R, number upcoming days (including the current one) with successive integers (the current day is zero), and you wash the dishes on day N if and only if there exists an index i such that N % M[i] = R[i], otherwise your roommate does it... Calculate the percentage of days on which you end up doing the washing."

To find the answer, you can find least common multiple of numbers in array M. The infinite number of days ahead can be split into identical blocks of M days, so the percentage of all days on which you end up with the chore will be the same as the percentage of the first LCM days. Iterate over days 0..LCM-1 and for each day check who gets to do the chore on it (since each element is at most 16, LCM will be at most 720720, and iteration will be sufficiently fast).

A much simpler approach would be just to iterate over a lot of days regardless of LCM and calculate the answer based on them — the absolute error will be small enough for this solution to pass.


AC代码:

#include <bits/stdc++.h>using namespace std;int a[20],b[20];int main() {int n;while(~scanf("%d",&n)){        for(int i = 0; i < n; ++i)            scanf("%d",&a[i]);        for(int i = 0; i < n; ++i)            scanf("%d",&b[i]);        int cnt = 0;        for(int t = 0; t < 720720; ++t) {            bool flag = false;            for(int i = 0; i < n; ++i)                if(t % a[i] == b[i])                    flag = true;            if(flag)                cnt++;        }        printf("%.6lf\n",cnt/720720.0);}return 0;}

Without Text

题目链接:

http://codeforces.com/problemset/problem/656/C

解题思路:

One more statement-less problem (and one more problem which turned out to be harder than I thought), but at least this one has a picture! By this time anyone with a history of participation in my contests should recognize a program regardless of the language in which it is written. In this case it is Sanscript, a visual dataflow language. The image doesn't represent the whole program, just the body of the main loop, but it contains most of the logic.

Individual elements of the program (functions) are fairly well annotated, so it shouldn't be too hard to figure out the overall purpose of the "code". The whole mess of the blocks and arrows calculates sum of indices of uppercase letters in English alphabet (A is 1, B is 2 etc.) minus sum of indices of lowercase letters in alphabet. Non-letter characters are ignored.


AC代码:

#include <bits/stdc++.h>using namespace std;typedef long long ll;char str[100005];int main(){    while(~scanf("%s",str)){        int len = strlen(str);        ll ans = 0;        for(int i = 0; i < len; ++i){            if(str[i] >= 'A' && str[i] <= 'Z')                ans += str[i] - 'A' + 1;            if(str[i] >= 'a' && str[i] <= 'z')                ans -= str[i] - 'a' + 1;        }        printf("%lld\n", ans);    }  return 0;}

Rosetta Problem

题目链接:

http://codeforces.com/problemset/problem/656/D

解题思路:

I love so many unusual programming languages that one language per problem just can't cover all of them. The solution is simple: have several languages per problem! Unfortunately, most people on Codeforces don't share my passion for languages, and this problem, while being trivial to implement, turned out to be the hardest in the whole contest.

Each paragraph of the statement is a short program in one of esoteric languages. If you recognize each one, find an interpreter for it and execute the program, you'll get a piece of statement. Put them together to find out what is it that you need to do to solve the actual problem:

  • Print number (Brainfuck) — http://ideone.com
  • of ones in (Malbolge) — http://www.malbolge.doleczek.pl/
  • base 8 (Piet) — http://www.rapapaing.com/blog/?page_id=6
  • notation of a (Befunge) — http://www.quirkster.com/iano/js/befunge.html

AC代码:

#include <bits/stdc++.h>using namespace std;int main(){int n;while(~scanf("%d",&n)){        int ans = 0;        while(n){            if(n%8 == 1)                ans++;            n /= 8;        }        printf("%d\n",ans);}return 0;}
a = int(raw_input())s = oct(a)res = 0for c in s:if c == '1':res += 1print res

Out of Controls

题目链接:

http://codeforces.com/problemset/problem/656/E

解题思路:

This problem was the first and the last one to tell you clearly and without doubt what you have to do: write a solution to a simple graph problem (likely using a Floyd-Warshall algorithm) without any traditional reserved words for loops and conditional statements.

There is a number of workarounds possible, which strongly depend on the language you use. Three main approaches were:

  • Honestly rewrite the solution in a recursive way.
  • Add some other characters in the middle of each forbidden keyword (/*..*/ in C++ or @ in Python with eval)
  • Use built-in functions like map, each etc.
  • I suspect the few Haskell users were not inconvenienced at all :-)

AC代码:

#include <bits/stdc++.h>using namespace std;#def\ine CAT(a,b) a##b#def\ine F CAT(fo,r)int a[10][10];int main(){int n;scanf("%d",&n);    F(int i = 0; i < n; ++i){        F(int j = 0; j < n; ++j){            scanf("%d",&a[i][j]);        }    }    F(int k = 0; k < n; ++k){        F(int i = 0; i < n; ++i){            F(int j = 0; j < n; ++j){                a[i][j] = min(a[i][j], a[i][k] + a[k][j]);            }        }    }    int ans = 0;    F(int i = 0; i < n; ++i){        F(int j = 0; j < n; ++j){            ans = max(ans, a[i][j]);        }    }    printf("%d\n",ans);    return 0;}

Ace It!

题目链接:

http://codeforces.com/problemset/problem/656/F

解题思路:

One of the most common tricks in April Fool's day contests is OEIS lookup. Most statement-less problems can be solved by thoroughly searching the website. In this problem, you were given OEIS sequence numbers, and if you checked them on the website, you could notice that the answer is the first element of the given sequence. Now it's just a matter of encoding this lookup process, given that Codeforces doesn't allow solutions to visit external websites...

Ok, it's about time to shout "April Fool!" and burst into laughter. Forget about OEIS. This problem was carefully forged to look very much like what I've described (and I hope it did!), but the actual solution is much, much simpler. The input represents a hand of cards in a game of blackjack, and you have to calculate the sum of points for it. A stands for Ace, worth 1 point (the minimal sum of the 6 digits part of the hand is 12, so if Ace is counted as 11, you're guaranteed to go bust), digits 2..9 correspond to matching cards, and pair of digits 10 represents, well, a ten. The reference to "validity" in the statement meant that 1 or 0 will never appear alone, but will always form a valid card.


AC代码:

#include <bits/stdc++.h>using namespace std;char str[10];int main() {    int sum = 0;    while(~scanf("%s",str)){        int len = strlen(str);        for(int i = 0; i < len; ++i){            if(str[i] == '1'){                sum += 10;                ++i;            }else if(str[i] >= '2' && str[i] <= '9'){                sum += str[i] - '0';            }        }        printf("%d\n",sum+1);    }    return 0;}


0 0
原创粉丝点击