The first line of the input contains an integer n (1 ≤ n ≤ 75) — the length of the string. The second line contains a string s, consisting of uppercase English letters. The length of the string is equal to n.

import java.util.*;
public class C_Bear_And_Company_3 {

    static int cv, ck , cs;
    static int[][] freq;
    static ArrayList<Integer>[] pos;
    static int[][][][] dp;

    public static void main(String[] agrs) {
    	Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        char[] c = in.next().toCharArray();
        cv = 0;
        ck = 0;
        cs = 0;
        freq = new int[n + 1][3];
        pos = new ArrayList[3];
        for (int i = 0; i < 3; i++) pos[i] = new ArrayList<>();
        int idx = 0;
        for (char x : c) {
            System.arraycopy(freq[idx], 0, freq[idx + 1], 0, 3);
            idx++;
            if (x == 'V') {
                freq[idx][0]++;
                cv++;
                pos[0].add(idx);
            } else if (x == 'K') {
                freq[idx][1]++;
                ck++;
                pos[1].add(idx);
            } else {
                freq[idx][2]++;
                cs++;
                pos[2].add(idx);
            }
        }
        dp = new int[cv + 1][ck + 1][cs + 1][3];
        for (int[][][] x : dp) for (int[][] y : x) for (int[] z : y) Arrays.fill(z, -1);
        System.out.println(dfs(0, 0, 0, 2));
    }

    static int dfs(int tv, int tk, int ts, int last) {
        if (tv == cv && tk == ck && ts == cs) {
            return 0;
        }
        if (dp[tv][tk][ts][last] != -1) return dp[tv][tk][ts][last];
        int ret = 1 << 29;
        if (tv < cv) {
            int p = pos[0].get(tv);
            int move = Math.max(freq[p][0] - tv, 0) + Math.max(freq[p][1] - tk, 0) + Math.max(freq[p][2] - ts, 0) - 1;
            ret = Math.min(ret, dfs(tv + 1, tk, ts, 0) + move);
        }
        if (tk < ck && last != 0) {
            int p = pos[1].get(tk);
            int move = Math.max(freq[p][0] - tv, 0) + Math.max(freq[p][1] - tk, 0) + Math.max(freq[p][2] - ts, 0) - 1;
            ret = Math.min(ret, dfs(tv, tk + 1, ts, 1) + move);
        }
        if (ts < cs) {
            int p = pos[2].get(ts);
            int move = Math.max(freq[p][0] - tv, 0) + Math.max(freq[p][1] - tk, 0) + Math.max(freq[p][2] - ts, 0) - 1;
            ret = Math.min(ret, dfs(tv, tk, ts + 1, 2) + move);
        }
        return dp[tv][tk][ts][last] = ret;
    }

}

NoteIn the first sample, the initial string is "VKVK". The minimum possible number of moves is 3. One optimal sequence of moves is: Swap two last letters. The string becomes "VKKV". Swap first two letters. The string becomes "KVKV". Swap the second and the third letter. The string becomes "KKVV". Indeed, this string doesn't have a substring "VK".In the second sample, there are two optimal sequences of moves. One is "BVVKV"  →  "VBVKV"  →  "VVBKV". The other is "BVVKV"  →  "BVKVV"  →  "BKVVV".In the fifth sample, no swaps are necessary.

Print one integer, denoting the minimum possible number of moves Limak can do, in order to obtain a string without a substring "VK".

Bear Limak prepares problems for a programming competition. Of course, it would be unprofessional to mention the sponsor name in the statement. Limak takes it seriously and he is going to change some words. To make it still possible to read, he will try to modify each word as little as possible.Limak has a string s that consists of uppercase English letters. In one move he can swap two adjacent letters of the string. For example, he can transform a string "ABBC" into "BABC" or "ABCB" in one move.Limak wants to obtain a string without a substring "VK" (i.e. there should be no letter 'V' immediately followed by letter 'K'). It can be easily proved that it's possible for any initial string s.What is the minimum possible number of moves Limak can do?

Input:
4
VKVK

Output: 3

Input:
5
BVVKV

Output: 2

Input:
7
VVKEVKK

Output: 3

Input:
20
VKVKVVVKVOVKVQKKKVVK

Output: 8

Input:
5
LIMAK

Output: 0

Input:
1
V

Output: 0

Input:
1
K

Output: 0

Input:
1
Z

Output: 0

Input:
17
VAKVAKLIMAKVVVKKK

Output: 4

Input:
10
VVKAVZVAAZ

Output: 1

Input:
17
QZVRZKDKMZZAKKZVA

Output: 0

Input:
51
AVVVVVVVVVVKKKKKKKKKKVVVVVVVVVVVVVVVKKKKKKKKKKKKKKK

Output: 135

Input:
75
VFZVZRVZAZJAKAZKAVVKZKVHZZZZAVAAKKAADKNAKRFKAAAZKZVAKAAAJAVKYAAZAKAVKASZAAK

Output: 3

Input:
75
VVKAVKKVAKVXCKKZKKAVVVAKKKKVVKSKVVWVLEVVHVXKKKVKVJKVVVZVVKKKVVKVVVKKKVVKZKV

Output: 19

Input:
2
VK

Output: 1

Input:
2
KV

Output: 0

Input:
3
VKK

Output: 2

Input:
3
KVV

Output: 0

Input:
75
VKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKV

Output: 703

Input:
75
VKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKOOOKVKV

Output: 175

Input:
6
VVVKKK

Output: 9

Input:
7
VVVKKKO

Output: 3

Input:
12
VKVKVKVKVKVK

Output: 21

Input:
5
VKOVK

Output: 2

Input:
3
KKV

Output: 0

Input:
6
VVOKKK

Output: 0

Input:
15
VOKVOKVVKKKKKKK

Output: 4

Input:
10
KKZKKVKZKV

Output: 1

Input:
15
VKKHKKKKZVKKVKV

Output: 4

Input:
22
VKKVKVKKVKVKZKKVKVAKKK

Output: 14

Input:
46
VVFVKKVAKVKKVGVKKKKZKKKKKKKAKKZKVVVVKKZVVKFVKK

Output: 9

Input:
50
KKAKVVNAVVVVKKVKKZVKKKKVKFTVVKKVVVVVZVLKKKKKKVKVVV

Output: 11

Input:
75
VKVVKVKKKVVZKVZKVKVKVVKIAVKVVVKKKVDKVKKVKAKKAKNAKVZKAAVVAKUKVKKVKKVZVAKKKVV

Output: 27

Input:
75
AJAKVZASUKAYZFSZRPAAVAGZKFZZHZZZKKKVLQAAVAHQHAZCVEZAAZZAAZIAAAZKKAAUKROVKAK

Output: 1

Input:
75
KAVVZVKKVVKVKVLVVKKKVVAKVVKEVAVVKKVVKVDVVKKVKKVZKKAKKKVKVZAVVKKKZVVDKVVAKZV

Output: 20

Input:
75
VKKVKKAKKKVVVVVZKKKKVKAVKKAZKKKKVKVVKVVKVVKCKKVVVVVZKKVKKKVKKKVVKVKVKOVVZKK

Output: 26

Input:
74
VVVKVKKKAZVVVKKKKKVVVVKKVVVKKVAKVVVVVVKVKVKVVMVVKVVVKVKKVVVVVKVKKKVVVXKVVK

Output: 66

Input:
74
VJVKVUKVVVVVVKVLVKKVVKZVNZVKKVVVAVVVKKAKZKZVAZVVKVKKZKKVNAVAKVKKCVVVKKVKVV

Output: 19

Input:
75
ZXPZMAKZZZZZZAZXAZAAPOAFAZUZZAZABQZZAZZBZAAAZZFANYAAZZZZAZHZARACAAZAZDPCAVZ

Output: 0

Input:
75
VZVVVZAUVZZTZZCTJZAVZVSVAAACVAHZVVAFZSVVAZAZVXVKVZVZVVZTAZREOVZZEVAVBAVAAAF

Output: 1

Input:
75
AZKZWAOZZLTZIZTAYKOALAAKKKZAASKAAZFHVZKZAAZUKAKZZBIAZZWAZZZZZPZZZRAZZZAZJZA

Output: 0

Input:
52
VAVBVCVDVEVFVGVHVIVJVKVLVMVNVOVPVQVRVSVTVUVVVWVXVYVZ

Output: 1

Input:
52
AKBKCKDKEKFKGKHKIKJKKKLKMKNKOKPKQKRKSKTKUKVKWKXKYKZK

Output: 1

Input:
64
VVKKVAVBVCVDVEVFVGVHVIVJVKVLVMVNVOVPVQVRVSVTVUVVVWVXVYVZVVVKKKKK

Output: 7

Input:
64
VVKKAKBKCKDKEKFKGKHKIKJKKKLKMKNKOKPKQKRKSKTKUKVKWKXKYKZKVVVKKKKK

Output: 7

Input:
75
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK

Output: 1406

Input:
75
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK

Output: 1406

Input:
72
AVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK

Output: 35

Input:
73
AVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKB

Output: 32

Input:
72
AVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKB

Output: 30

Input:
67
VVVVVVVVVVVVVVVVVVVVKKKKKKKKKKKKKKKXVVVVVVVVVVVVVVVVVVVVVVKKKKKKKKK

Output: 213

Input:
57
VVVVKKKKKKAAVVVVVVVVKKKKKKKVVVVVVVVVKKKKKKKKKKKKKKKKKKKKO

Output: 34

Input:
13
VVVVKKAVVKVKK

Output: 10

Input:
65
VVVVKKAVVKVKKVVVVKKAVVKVKKVVVVKKAVVKVKKVVVVKKAVVKVKKVVVVKKAVVKVKK

Output: 50

Input:
67
VVVVKKAVVKVKKVVVVKKAVVKVKKAOVVVVKKAVVKVKKVVVVKKAVVKVKKVVVVKKAVVKVKK

Output: 44

Input:
52
ZVKVVKKKVKKZKZVKVVKKKVKKZKZVKVVKKKVKKZKZVKVVKKKVKKZK

Output: 28

Input:
63
KKKVVVKAAKVVVTVVVKAUVKKKVKVKKVKVKVVKVKKVKVKKKQVKVVVKVKKVKKKKKKZ

Output: 43

Input:
75
VVKVKKKVKVVKKKKKVVKKKKVVVKVKKKAVAKKKVVKVKEVVVVVVVVKKKKKVVVVVKVVVKKKVVKVVKVV

Output: 114

Input:
75
VVVVVKVKVVKKEVVVVVAKVKKZKVVPKKZKAVKVAKVMZKZVUVKKIVVZVVVKVKZVVVVKKVKVZZVOVKV

Output: 23

Input:
75
VAKKVKVKKZVVZAVKKVKVZKKVKVVKKAVKKKVVZVKVKVKKKKVVVVKKVZKVVKKKVAKKZVKKVKVVKVK

Output: 36

Input:
75
VVKVKKVZAVVKHKRAVKAKVKKVKKAAVKVVNZVKKKVVKMAVVKKWKKVVKVHKKVKVZVVKZZKVKVIKZVK

Output: 22

Input:
75
KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK

Output: 0

Input:
75
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV

Output: 0

Input:
75
VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVK

Output: 74

Input:
75
KVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV

Output: 0

Input:
38
ZKKKVVVVVVVVVVKKKKKEVVKKVVVKKKVVVVKKKK

Output: 40

Input:
74
ZKKKVVVVVVVVVVKKKKKEVVKKVVVKKKVVVVKKKKVVVVVVVVVVVVVVVVVVKKKKKKKKKKKKKKKKKK

Output: 98

Input:
71
ZKKKVVVVVVVKKKKKEVVKKVVVKKKVVVVKKKKVVVVVVVVKKKKKKKKKKKVVVVVVVVVVKKKKKKK

Output: 153

Input:
68
KKVVVVVVVVVVKKKKKKKKKKKKKKKKKKKKVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVKKKKKV

Output: 400

Input:
75
KVVKCVKVVVVKVVVKVKVAVVMVVVVVKKVVVKVVVVVKKVVVVVKVVKVVVKKKKKVKKVKAVVVVVVVVVVK

Output: 71

Input:
74
KKKZKVKKKKVKKKKVKVZKKKZKKKKKZKVKKZZKKBVKKVAKVKVKZVVKKKKKKKKKVKKVVKKVVKKKVK

Output: 45

Input:
75
KVKVVKVKVKVVVVVKVKKKVKVVKVVKVVKKKKEKVVVKKKVVKVVVVVVVKKVKKVVVKAKVVKKVVVVVKUV

Output: 103

Input:
75
KKVVAVVVVKVKAVVAKVKVKVVVVKKKKKAZVKVKVKJVVVAKVVKKKVVVVZVAVVVZKVZAKVVVVVVVAKK

Output: 18

[{'input': '10\r\nKKZKKVKZKV\r\n', 'output': ['1']}, {'input': '1\r\nK\r\n', 'output': ['0']}, {'input': '57\r\nVVVVKKKKKKAAVVVVVVVVKKKKKKKVVVVVVVVVKKKKKKKKKKKKKKKKKKKKO\r\n', 'output': ['34']}, {'input': '71\r\nZKKKVVVVVVVKKKKKEVVKKVVVKKKVVVVKKKKVVVVVVVVKKKKKKKKKKKVVVVVVVVVVKKKKKKK\r\n', 'output': ['153']}, {'input': '74\r\nZKKKVVVVVVVVVVKKKKKEVVKKVVVKKKVVVVKKKKVVVVVVVVVVVVVVVVVVKKKKKKKKKKKKKKKKKK\r\n', 'output': ['98']}]

[{'input': '75\r\nZXPZMAKZZZZZZAZXAZAAPOAFAZUZZAZABQZZAZZBZAAAZZFANYAAZZZZAZHZARACAAZAZDPCAVZ\r\n', 'output': ['0']}, {'input': '10\r\nVVKAVZVAAZ\r\n', 'output': ['1']}, {'input': '65\r\nVVVVKKAVVKVKKVVVVKKAVVKVKKVVVVKKAVVKVKKVVVVKKAVVKVKKVVVVKKAVVKVKK\r\n', 'output': ['50']}, {'input': '75\r\nVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\r\n', 'output': ['0']}, {'input': '52\r\nAKBKCKDKEKFKGKHKIKJKKKLKMKNKOKPKQKRKSKTKUKVKWKXKYKZK\r\n', 'output': ['1']}]

[{'input': '50\r\nKKAKVVNAVVVVKKVKKZVKKKKVKFTVVKKVVVVVZVLKKKKKKVKVVV\r\n', 'output': ['11']}, {'input': '17\r\nQZVRZKDKMZZAKKZVA\r\n', 'output': ['0']}, {'input': '75\r\nAJAKVZASUKAYZFSZRPAAVAGZKFZZHZZZKKKVLQAAVAHQHAZCVEZAAZZAAZIAAAZKKAAUKROVKAK\r\n', 'output': ['1']}, {'input': '74\r\nVJVKVUKVVVVVVKVLVKKVVKZVNZVKKVVVAVVVKKAKZKZVAZVVKVKKZKKVNAVAKVKKCVVVKKVKVV\r\n', 'output': ['19']}, {'input': '72\r\nAVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKB\r\n', 'output': ['30']}]

[{'input': '75\r\nVVVVVKVKVVKKEVVVVVAKVKKZKVVPKKZKAVKVAKVMZKZVUVKKIVVZVVVKVKZVVVVKKVKVZZVOVKV\r\n', 'output': ['23']}, {'input': '2\r\nKV\r\n', 'output': ['0']}, {'input': '5\r\nLIMAK\r\n', 'output': ['0']}, {'input': '75\r\nKVVKCVKVVVVKVVVKVKVAVVMVVVVVKKVVVKVVVVVKKVVVVVKVVKVVVKKKKKVKKVKAVVVVVVVVVVK\r\n', 'output': ['71']}, {'input': '68\r\nKKVVVVVVVVVVKKKKKKKKKKKKKKKKKKKKVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVKKKKKV\r\n', 'output': ['400']}]

[{'input': '75\r\nVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKVKV\r\n', 'output': ['703']}, {'input': '64\r\nVVKKVAVBVCVDVEVFVGVHVIVJVKVLVMVNVOVPVQVRVSVTVUVVVWVXVYVZVVVKKKKK\r\n', 'output': ['7']}, {'input': '51\r\nAVVVVVVVVVVKKKKKKKKKKVVVVVVVVVVVVVVVKKKKKKKKKKKKKKK\r\n', 'output': ['135']}, {'input': '72\r\nAVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKK\r\n', 'output': ['35']}, {'input': '52\r\nZVKVVKKKVKKZKZVKVVKKKVKKZKZVKVVKKKVKKZKZVKVVKKKVKKZK\r\n', 'output': ['28']}]


The first line contains a non-empty string $$$s$$$ with length at most $$$50$$$ characters, containing lowercase English letters only.

import java.util.HashSet;
import java.util.Scanner;

public class Nafis {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        String str = sc.next();
        int sz = str.length();
        HashSet<Character> freq = new HashSet<>();
        for (int i = 0; i <= sz / 2; i++) {
            if (str.charAt(i) == str.charAt(sz - i - 1)) {
                freq.add(str.charAt(i));
                continue;
            } else {
                System.out.println(sz);
                return;
            }
        }

        System.out.println(freq.size() == 1 ? 0 : sz - 1);

    }
}

Note"mew" is not a palindrome, so the longest substring of it that is not a palindrome, is the string "mew" itself. Thus, the answer for the first example is $$$3$$$.The string "uffuw" is one of the longest non-palindrome substrings (of length $$$5$$$) of the string "wuffuw", so the answer for the second example is $$$5$$$.All substrings of the string "qqqqqqqq" consist of equal characters so they are palindromes. This way, there are no non-palindrome substrings. Thus, the answer for the third example is $$$0$$$.

If there is such a substring in $$$s$$$ that is not a palindrome, print the maximum length of such a substring. Otherwise print $$$0$$$. Note that there can be multiple longest substrings that are not palindromes, but their length is unique.

A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, the strings "kek", "abacaba", "r" and "papicipap" are palindromes, while the strings "abb" and "iq" are not.A substring $$$s[l \ldots r]$$$ ($$$1 \leq l \leq r \leq |s|$$$) of a string $$$s = s_{1}s_{2} \ldots s_{|s|}$$$ is the string $$$s_{l}s_{l + 1} \ldots s_{r}$$$.Anna does not like palindromes, so she makes her friends call her Ann. She also changes all the words she reads in a similar way. Namely, each word $$$s$$$ is changed into its longest substring that is not a palindrome. If all the substrings of $$$s$$$ are palindromes, she skips the word at all.Some time ago Ann read the word $$$s$$$. What is the word she changed it into?

Input: mew

Input: wuffuw

Input: qqqqqqqq

Input: ijvji

Input: iiiiiii

Input: wobervhvvkihcuyjtmqhaaigvvgiaahqmtjyuchikvvhvrebow

Output: 49

Input: wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww

Output: 0

Input: wobervhvvkihcuyjtmqhaaigvahheoqleromusrartldojsjvy

Output: 50

Input: ijvxljt

Input: fyhcncnchyf

Input: ffffffffffff

Output: 0

Input: fyhcncfsepqj

Output: 12

Input: ybejrrlbcinttnicblrrjeby

Output: 23

Input: yyyyyyyyyyyyyyyyyyyyyyyyy

Output: 0

Input: ybejrrlbcintahovgjddrqatv

Output: 25

Input: oftmhcmclgyqaojljoaqyglcmchmtfo

Output: 30

Input: oooooooooooooooooooooooooooooooo

Output: 0

Input: oftmhcmclgyqaojllbotztajglsmcilv

Output: 32

Input: gxandbtgpbknxvnkjaajknvxnkbpgtbdnaxg

Output: 35

Input: gggggggggggggggggggggggggggggggggggg

Output: 0

Input: gxandbtgpbknxvnkjaygommzqitqzjfalfkk

Output: 36

Input: fcliblymyqckxvieotjooojtoeivxkcqymylbilcf

Output: 40

Input: fffffffffffffffffffffffffffffffffffffffffff

Output: 0

Input: fcliblymyqckxvieotjootiqwtyznhhvuhbaixwqnsy

Output: 43

Input: rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr

Output: 0

Input: rajccqwqnqmshmerpvjyfepxwpxyldzpzhctqjnstxyfmlhiy

Output: 49

Input: a

Input: abca

Input: aaaaabaaaaa

Input: aba

Input: asaa

Input: aabaa

Input: aabbaa

Input: abcdaaa

Input: aaholaa

Input: abcdefghijka

Output: 12

Input: aaadcba

Input: aaaabaaaa

Input: abaa

Input: abcbaa

Input: ab

Input: l

Input: aaaabcaaaa

Input: abbaaaaaabba

Output: 11

Input: abaaa

Input: baa

Input: aaaaaaabbba

Input: ccbcc

Input: bbbaaab

Input: abaaaaaaaa

Input: abaaba

Input: aabsdfaaaa

Input: aaaba

Input: aaabaaa

Input: baaabbb

Input: ccbbabbcc

Input: cabc

Input: aabcd

Input: abcdea

Input: bbabb

Input: aaaaabababaaaaa

Output: 14

Input: bbabbb

Input: aababd

Input: abaaaa

Input: aaaaaaaabbba

Output: 12

Input: aabca

Input: aaabccbaaa

Input: aaaaaaaaaaaaaaaaaaaab

Output: 21

Input: babb

Input: abcaa

Input: qwqq

Input: aaaaaaaaaaabbbbbbbbbbbbbbbaaaaaaaaaaaaaaaaaaaaaa

Output: 48

Input: aaab

Input: aaaaaabaaaaa

Output: 12

Input: wwuww

Input: aaaaabcbaaaaa

Output: 12

Input: aaabbbaaa

Input: aabcbaa

Input: abccdefccba

Input: aabbcbbaa

Input: aaaabbaaaa

Input: aabcda

Input: abbca

Input: aaaaaabbaaa

Input: sssssspssssss

Output: 12

Input: sdnmsdcs

Input: aaabbbccbbbaaa

Output: 13

Input: cbdbdc

Input: abb

Input: abcdefaaaa

Input: abbbaaa

Input: v

Input: abccbba

Input: axyza

Input: abcdefgaaaa

Input: aaabcdaaa

Input: aaaacaaaa

Input: aaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaa

Output: 42

Input: abbbaa

Input: abcdee

Input: oom

Input: aabcaa

Input: abba

Input: aaca

Input: aacbca

Input: ababa

Input: abcda

Input: cccaaccc

Input: aaabcda

Input: aa

Input: aabaaaa

Input: abbaaaa

Input: aaabcbaaa

Input: aabba

Input: xyxx

Input: aaaaaaaaaaaabc

Output: 14

Input: bbaaaabb

Input: aaabaa

Input: sssssabsssss

Output: 12

Input: bbbaaaabbb

Input: abbbbaaaa

Input: wwufuww

Input: oowoo

Input: cccaccc

Input: aaa

Input: bbbcc

Input: abcdef

Input: abbba

Input: aab

Input: aaba

Input: azbyaaa

Input: oooooiooooo

Input: aabbbbbaaaaaa

Output: 13

[{'input': 'aaabcda\r\n', 'output': ['7']}, {'input': 'abbaaaaaabba\r\n', 'output': ['11']}, {'input': 'oftmhcmclgyqaojljoaqyglcmchmtfo\r\n', 'output': ['30']}, {'input': 'aabca\r\n', 'output': ['5']}, {'input': 'abcda\r\n', 'output': ['5']}]

[{'input': 'abcdefgaaaa\r\n', 'output': ['11']}, {'input': 'abcaa\r\n', 'output': ['5']}, {'input': 'aabbbbbaaaaaa\r\n', 'output': ['13']}, {'input': 'aabaa\r\n', 'output': ['4']}, {'input': 'yyyyyyyyyyyyyyyyyyyyyyyyy\r\n', 'output': ['0']}]

[{'input': 'ababa\r\n', 'output': ['4']}, {'input': 'cabc\r\n', 'output': ['4']}, {'input': 'abbbaaa\r\n', 'output': ['7']}, {'input': 'sssssspssssss\r\n', 'output': ['12']}, {'input': 'abbaaaaaabba\r\n', 'output': ['11']}]

[{'input': 'abaaaaaaaa\r\n', 'output': ['10']}, {'input': 'abba\r\n', 'output': ['3']}, {'input': 'aabcd\r\n', 'output': ['5']}, {'input': 'abbbaaa\r\n', 'output': ['7']}, {'input': 'aacbca\r\n', 'output': ['6']}]

[{'input': 'rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr\r\n', 'output': ['0']}, {'input': 'yyyyyyyyyyyyyyyyyyyyyyyyy\r\n', 'output': ['0']}, {'input': 'aba\r\n', 'output': ['2']}, {'input': 'cabc\r\n', 'output': ['4']}, {'input': 'aaaaabababaaaaa\r\n', 'output': ['14']}]


The only line of input contains a description of Vasya's seat in the format ns, where n (1 ≤ n ≤ 1018) is the index of the row and s is the seat in this row, denoted as letter from 'a' to 'f'. The index of the row and the seat are not separated by a space.

import java.util.*;
import java.math.*;
import java.io.*;
public class FoodOnThePlane {
	public static void main(String args[]) throws Exception {
		BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
		//BufferedReader s = new BufferedReader(new FileReader("*.in"));
		//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("*.out")));
		//StringTokenizer st = new StringTokenizer(s.readLine());
		String y = s.readLine();
		long row = Long.parseLong(y.substring(0, y.length()-1));
		int col = y.charAt(y.length()-1) - 'a';
		long secs = 0;
		if(row == 1)secs = 0;
		else if (row == 2)secs = 7;
		else {
			long remainder = row % 4;
			if(remainder == 1){
				secs = ((row - 1) / 4) * 16;
			} else if (remainder == 3){
				secs = ((row - 3)/4) * 16;
			} else if (remainder == 2){
				secs = ((((row + 2) / 4)-1) * 16)+7;
			} else {
				secs = (((row / 4)-1) * 16)+7;
			}
		}
		if(col == 0){
			secs += 4;
		} else if(col == 1){
			secs += 5;
		} else if(col == 2){
			secs += 6;
		} else if(col == 3){
			secs += 3;
		} else if(col == 4){
			secs += 2;
		} else {
			secs+=1;
		}
		System.out.println(secs);
	}
}

NoteIn the first sample, the first flight attendant serves Vasya first, so Vasya gets his lunch after 1 second.In the second sample, the flight attendants will spend 6 seconds to serve everyone in the rows 1 and 3, then they will move one row forward in 1 second. As they first serve seats located to the right of the aisle in order from window to aisle, Vasya has to wait 3 more seconds. The total is 6 + 1 + 3 = 10.

Print one integer — the number of seconds Vasya has to wait until he gets his lunch.

A new airplane SuperPuperJet has an infinite number of rows, numbered with positive integers starting with 1 from cockpit to tail. There are six seats in each row, denoted with letters from 'a' to 'f'. Seats 'a', 'b' and 'c' are located to the left of an aisle (if one looks in the direction of the cockpit), while seats 'd', 'e' and 'f' are located to the right. Seats 'a' and 'f' are located near the windows, while seats 'c' and 'd' are located near the aisle.    It's lunch time and two flight attendants have just started to serve food. They move from the first rows to the tail, always maintaining a distance of two rows from each other because of the food trolley. Thus, at the beginning the first attendant serves row 1 while the second attendant serves row 3. When both rows are done they move one row forward: the first attendant serves row 2 while the second attendant serves row 4. Then they move three rows forward and the first attendant serves row 5 while the second attendant serves row 7. Then they move one row forward again and so on.Flight attendants work with the same speed: it takes exactly 1 second to serve one passenger and 1 second to move one row forward. Each attendant first serves the passengers on the seats to the right of the aisle and then serves passengers on the seats to the left of the aisle (if one looks in the direction of the cockpit). Moreover, they always serve passengers in order from the window to the aisle. Thus, the first passenger to receive food in each row is located in seat 'f', and the last one — in seat 'c'. Assume that all seats are occupied.Vasya has seat s in row n and wants to know how many seconds will pass before he gets his lunch.


The only line contains integers a, b, x1, y1, x2 and y2 — the parameters of the bad squares, the coordinates of the initial and the final squares correspondingly (2 ≤ a, b ≤ 109 and |x1|,|y1|,|x2|,|y2| ≤ 109). It is guaranteed that the initial and the final square aren't bad.

import java.io.*;
import java.util.*;

public class Main {

    void solve(Scanner in, PrintWriter out) {

        int a = in.nextInt();
        int b = in.nextInt();
        int x1 = in.nextInt();
        int y1 = in.nextInt();
        int x2 = in.nextInt();
        int y2 = in.nextInt();

        int n1 = Math.floorDiv(x1 + y1, 2 * a);
        int m1 = Math.floorDiv(x1 - y1, 2 * b);
        int n2 = Math.floorDiv(x2 + y2, 2 * a);
        int m2 = Math.floorDiv(x2 - y2, 2 * b);

        int res = Math.max(Math.abs(n2 - n1), Math.abs(m2 - m1));

        out.print(res);
    }

    void run() {
        try (Scanner in = new Scanner(System.in);
             PrintWriter out = new PrintWriter(System.out)) {

            solve(in, out);
        }
    }

    public static void main(String[] args) {
        new Main().run();
    }
}

Print a single number — the minimum number of bad cells that one will have to visit in order to travel from square (x1; y1) to square (x2; y2).

You are given an infinite checkered field. You should get from a square (x1; y1) to a square (x2; y2). Using the shortest path is not necessary. You can move on the field squares in four directions. That is, when you are positioned in any square, you can move to any other side-neighboring one. A square (x; y) is considered bad, if at least one of the two conditions is fulfilled: |x + y| ≡ 0 (mod 2a), |x - y| ≡ 0 (mod 2b). Your task is to find the minimum number of bad cells one will have to visit on the way from (x1; y1) to (x2; y2).

Input: 2 2 1 0 0 1

Output: 1

Input: 2 2 10 11 0 1

Output: 5

Input: 2 4 3 -1 3 7

Output: 2

Input: 2 2 9 10 -10 -11

Output: 10

Input: 3 2 -11 -10 10 11

Output: 7

Input: 3 2 11 -12 -12 11

Output: 11

Input: 3 3 12 11 -12 -11

Output: 7

Input: 2 3 -12 13 13 -12

Output: 9

Input: 3 4 -8 5 6 -3

Output: 3

Input: 2 3 2 -1 -10 -1

Output: 3

Input: 4 4 3 2 10 -1

Output: 1

Input: 3 2 -8 -9 -14 -1

Output: 4

Input: 4 4 0 -3 11 -4

Output: 1

Input: 5 3 6 3 3 12

Output: 2

Input: 3 5 -4 -7 5 0

Output: 2

Input: 3 5 -20 19 21 16

Output: 7

Input: 5 6 23 -10 -20 -17

Output: 5

Input: 3 2 8 -25 0 25

Output: 15

Input: 7 7 23 28 -20 -27

Output: 7

Input: 7 7 -30 -29 32 31

Output: 9

Input: 5 8 35 -36 -34 33

Output: 9

Input: 2 9 37 34 -38 -37

Output: 36

Input: 10 8 -44 41 43 -38

Output: 11

Input: 8 9 8 -23 31 -46

Output: 3

Input: 11 10 9 -40 37 -56

Output: 2

Input: 11 5 -71 44 -18 -21

Output: 12

Input: 6 13 -37 12 3 60

Output: 8

Input: 14 9 44 45 -50 -9

Output: 6

Input: 14 16 1967781 241814 1873488 -829353

Output: 41624

Input: 8 12 -14763515 -11730382 -1343471 -4020758

Output: 1320604

Input: 18 17 -26078453 -12853708 26705417 -4593122

Output: 1695679

Input: 5 18 41299309 8851928 -40049166 -35564497

Output: 12576490

Input: 7 20 10771554 -46099323 39192337 54007626

Output: 9180553

Input: 21 24 31005425 54491054 -24732944 -61529693

Output: 4089503

Input: 24 27 -57405669 -65437426 56079726 56139299

Output: 4897128

Input: 17 22 72042304 -75756269 -70969649 64115614

Output: 6429178

Input: 31 29 73305636 76203147 -85238444 -86730133

Output: 5185118

Input: 34 19 -95432112 102651275 96089919 -106537520

Output: 10545022

Input: 26 34 -107153659 6976200 34136365 -95904822

Output: 3590751

Input: 38 5 -13548447 534376 64966608 -29272371

Output: 10832180

Input: 42 45 13921918 62207801 80023961 -85820354

Output: 2379224

Input: 15 11 -140506021 21571904 -148280972 64286933

Output: 2294999

Input: 53 50 -120558789 -138770904 4229051 102239338

Output: 3450925

Input: 29 54 16062290 129524399 -84381788 132177911

Output: 1686044

Input: 12 63 100712190 36906101 87205943 82885374

Output: 1353043

Input: 66 39 -170201625 -169447104 166170410 181151513

Output: 5204323

Input: 72 75 182000846 -19533501 -166922752 -142084479

Output: 3274129

Input: 55 22 189761193 -192020216 -153412991 188486816

Output: 16447301

Input: 86 84 -65173069 221707138 155388823 -224274366

Output: 3967520

Input: 77 101 -241379320 -196400933 220541904 214436435

Output: 5667263

Input: 70 110 221139524 -236077945 -236283510 205078897

Output: 4084454

Input: 18 116 231579605 226020224 -214399491 -217631436

Output: 24711966

Input: 133 122 -258888058 250173335 258738451 -242389122

Output: 4140119

Input: 127 88 66407013 205897916 133496817 264883406

Output: 496360

Input: 146 157 261464154 113810381 214579048 -202712885

Output: 1244549

Input: 148 163 -62225702 -294347345 -98578232 214557359

Output: 1672568

Input: 7 179 -249546082 207791883 267735483 49881404

Output: 25669363

Input: 125 204 91089644 83192699 -300075653 54365352

Output: 1679971

Input: 216 218 15106122 259371253 296596165 -45704666

Output: 1345335

Input: 207 226 -194940280 130461973 246251465 260969752

Output: 1380917

Input: 267 263 -291849914 -111930623 344642355 250706518

Output: 1871029

Input: 288 40 338359015 273791206 -341021431 56950660

Output: 5781749

Input: 321 30 46954660 -343679003 -37851471 373573736

Output: 13367648

Input: 356 10 97627462 341324361 -132835544 -334849729

Output: 22285554

Input: 380 397 -340890121 -349529418 396652406 353599055

Output: 1895619

Input: 388 113 366011910 -387447751 -403158698 353327235

Output: 6681175

Input: 465 469 376765675 358805048 -390193085 -375070460

Output: 1613801

Input: 504 116 -408147784 387006943 367365902 -415105789

Output: 6800114

Input: 509 565 14560229 -77153392 -340426524 82224911

Output: 455190

Input: 605 297 -251700323 -366763764 -445828791 325081312

Output: 1491538

Input: 689 635 344525358 -321493413 12979458 -353392841

Output: 263749

Input: 664 408 -151206136 -299481355 -385233545 310492602

Output: 1034315

Input: 783 827 -98613981 316213558 -275430891 455234090

Output: 190954

Input: 899 549 -249681750 38465319 105189786 -64009701

Output: 416527

Input: 868 969 245648369 212586392 258298826 -389155385

Output: 339339

Input: 1005 557 -451917708 -32771965 501646713 -357583032

Output: 1147554

Input: 1123 1126 438419485 487688122 -477080698 -185247601

Output: 707229

Input: 1174 901 522498777 -499217148 77740787 519316970

Output: 812037

Input: 1425 1444 516172942 520776621 -319341286 -488388923

Output: 647256

Input: 1576 15 -503228573 -531048974 531411118 557082183

Output: 1783049

Input: 1147 1627 473801348 -494462579 -514604760 486124951

Output: 605100

Input: 1811 1038 526157767 549399960 -479125660 -508887739

Output: 569733

Input: 2033 1908 -480144210 482795119 496763189 -594064604

Output: 538199

Input: 86 1341 -197343715 13981506 -529124963 208152056

Output: 800062

Input: 2455 2436 -335351804 -50788097 286734045 222304974

Output: 182317

Input: 2571 2243 474188235 -306739018 48936920 -83297677

Output: 144603

Input: 1558 2911 -239080974 -489789417 369291826 -67795521

Output: 330670

Input: 2795 3024 418200485 -575735266 101404272 -10209857

Output: 145887

Input: 3341 3479 481143880 -383576301 -584637231 166949262

Output: 232295

Input: 3868 1251 -639544998 21536679 -480078735 -457166436

Output: 255064

Input: 4260 4286 -559966975 430515446 630949753 -403746792

Output: 236255

Input: 4685 84 597126772 174658367 -667031403 657366658

Output: 10398014

Input: 5099 3763 239091250 -689089763 -331708609 690647436

Output: 259173

Input: 5431 5421 218916782 582895951 714645533 -634539842

Output: 158012

Input: 5989 6249 -605686335 -602992500 586207791 624769222

Output: 202009

Input: 4238 464 631928630 -699088687 -665579317 658247096

Output: 2860823

Input: 7368 7243 646513016 723552175 -631585348 -678824351

Output: 181900

Input: 6929 8303 -718092932 630511765 717136401 -678221530

Output: 165239

Input: 551 8823 -644698584 720097649 -746775493 -719362914

Output: 1398855

Input: 2036 9146 46737913 478540414 -603176411 -34978692

Output: 285715

Input: 10000 10002 96487781 -692179874 182133670 357089051

Output: 56746

Input: 4209 7951 232804958 -326325341 -138865076 516216059

Output: 76356

Input: 10005 10008 -234169778 -592210597 -126329886 -812018105

Output: 16370

Input: 8387 10012 -275798799 489020846 127010938 154401541

Output: 36828

Input: 10058 9799 -25054219 -611037250 172201377 486371190

Output: 64360

Input: 10088 6166 -735339950 -111273129 787180186 -439981865

Output: 150116

Input: 10311 10242 764996339 626041956 -740573838 -97126465

Output: 108076

Input: 10067 8186 -736794579 -820525762 -407728461 839527984

Output: 98794

Input: 10721 11225 -767745746 709747051 443545879 -717667636

Output: 117537

Input: 13225 984 -760662977 -854994174 786299019 825465374

Output: 122020

Input: 14699 14675 792934253 -867739654 -737526630 840318203

Output: 110341

Input: 20967 19929 821529452 892087465 -867106029 -836044344

Output: 81480

Input: 43649 46022 -793221994 750708255 871188328 -901390875

Output: 36031

Input: 25706 3236 867426580 143799455 254112907 -287546356

Output: 28116

Input: 222075 201776 -663198106 -381459887 -29690718 -65372649

Output: 2138

Input: 526654 264582 -19827600 -757880279 -903623062 -934193021

Output: 1337

Input: 34483 1001201 -483230679 -24466088 827887504 293189155

Output: 23617

Input: 840853 1638188 -425749679 502946202 -953467908 557484181

Output: 281

Input: 4237214 4640696 -612169083 -326390834 887479529 304518522

Output: 251

Input: 2959011 3049607 253816894 -342369389 610124947 440828496

Output: 192

Input: 31288011 27242802 -934902606 343371553 926119543 -195542560

Output: 44

Input: 6152051 53675778 964821583 85960172 -939564894 755134693

Output: 100

Input: 101304499 148554333 -590787464 -890180401 -117457421 997140710

Output: 12

Input: 134699726 208640218 514309071 801051734 276512437 -803859310

Output: 6

Input: 472555248 417950652 -897989583 -805741694 915661619 800897620

Output: 3

Input: 299386785 573704302 956852511 -973861202 -816995136 989470727

Output: 3

Input: 1000000000 1000000000 871940474 991768763 -914352281 -886310260

Output: 1

Input: 781751245 1000000000 -848188940 813653557 978830633 -825182414

Output: 1

Input: 999999999 1000000000 1000000000 -999999999 -1000000000 999999999

Output: 1

Input: 999999 100000 12345 54321 6789 9876

Output: 0


The only line of the input contains one integer n (1 ≤ n ≤ 1018) — the prediction on the number of people who will buy the game.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long res = (n / 2) + (n / 3) - (n / 6) + (n / 42)
                  +(n / 5) + (n / 7) + (n/ 30) + (n / 70) + (n / 105)
                  -(n / 10) - (n / 15) - (n / 14) - (n / 21) - (n / 35) - (n / 210);
        System.out.println(n - res);
    }
}

Output one integer showing how many numbers from 1 to n are not divisible by any number from 2 to 10.

IT City company developing computer games decided to upgrade its way to reward its employees. Now it looks the following way. After a new game release users start buying it actively, and the company tracks the number of sales with precision to each transaction. Every time when the next number of sales is not divisible by any number from 2 to 10 every developer of this game gets a small bonus.A game designer Petya knows that the company is just about to release a new game that was partly developed by him. On the basis of his experience he predicts that n people will buy the game during the first month. Now Petya wants to determine how many times he will get the bonus. Help him to know it.


The single line of the input contains three space-separated integers a, b and c (1 ≤ a, b, c ≤ 106) — the valence numbers of the given atoms.

import java.util.Scanner;

public class SimpleMoecules {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner input = new Scanner(System.in);
		int a = input.nextInt();
		int b = input.nextInt();
		int c = input.nextInt();
		if((c+b-a)%2==0){
			int z  = (c+b-a)/2;
			if(z<0){
				System.out.println("Impossible");
				return;
			}
			int y = c-z;
			if(y<0){
				System.out.println("Impossible");
				return;
			}
			int x = a-y;
			if(x<0){
				System.out.println("Impossible");
				return;
			}
			else
				System.out.println(x+" "+z+" "+y);
		}
		else
			System.out.println("Impossible");
	}

}

NoteThe first sample corresponds to the first figure. There are no bonds between atoms 1 and 2 in this case.The second sample corresponds to the second figure. There is one or more bonds between each pair of atoms.The third sample corresponds to the third figure. There is no solution, because an atom cannot form bonds with itself.The configuration in the fourth figure is impossible as each atom must have at least one atomic bond.

If such a molecule can be built, print three space-separated integers — the number of bonds between the 1-st and the 2-nd, the 2-nd and the 3-rd, the 3-rd and the 1-st atoms, correspondingly. If there are multiple solutions, output any of them. If there is no solution, print "Impossible" (without the quotes).

Mad scientist Mike is busy carrying out experiments in chemistry. Today he will attempt to join three atoms into one molecule.A molecule consists of atoms, with some pairs of atoms connected by atomic bonds. Each atom has a valence number — the number of bonds the atom must form with other atoms. An atom can form one or multiple bonds with any other atom, but it cannot form a bond with itself. The number of bonds of an atom in the molecule must be equal to its valence number.  Mike knows valence numbers of the three atoms. Find a molecule that can be built from these atoms according to the stated rules, or determine that it is impossible.

Input: 1 1 2

Output: 0 1 1

Input: 3 4 5

Output: 1 3 2

Input: 4 1 1

Output: Impossible

Input: 1 1 1

Output: Impossible

Input: 1000000 1000000 1000000

Output: 500000 500000 500000

Input: 3 11 8

Output: 3 8 0

Input: 8 5 12

Output: Impossible

Input: 1000000 500000 1

Output: Impossible

Input: 1000000 500000 2

Output: Impossible

Input: 2 2 2

Output: 1 1 1

Input: 3 3 3

Output: Impossible

Input: 4 4 4

Output: 2 2 2

Input: 2 4 2

Output: 2 2 0

Input: 10 5 14

Output: Impossible

Input: 10 5 15

Output: 0 5 10

Input: 10 4 16

Output: Impossible

Input: 3 3 6

Output: 0 3 3

Input: 9 95 90

Output: 7 88 2

Input: 3 5 8

Output: 0 5 3

Input: 5 8 13

Output: 0 8 5

Input: 6 1 5

Output: 1 0 5

Input: 59 54 56

Output: Impossible

Input: 246 137 940

Output: Impossible

Input: 7357 3578 9123

Output: 906 2672 6451

Input: 93952 49553 83405

Output: 30050 19503 63902

Input: 688348 726472 442198

Output: 486311 240161 202037

Input: 602752 645534 784262

Output: 232012 413522 370740

Input: 741349 48244 642678

Output: Impossible

Input: 655754 418251 468390

Output: Impossible

Input: 310703 820961 326806

Output: Impossible

Input: 1 1 3

Output: Impossible

Input: 5 1 4

Output: 1 0 4


The only line contains an integer n (2 ≤ n ≤ 1012), the number of vertices in the graph.

// http://codeforces.com/contest/959/problem/E

import java.io.*;
import java.util.InputMismatchException;

public class CF959E {

    public static void main(String[] args) throws IOException {
        InputReader in = new InputReader(System.in);
        OutputWriter out = new OutputWriter(System.out);
        final long N = in.readLong() - 1;
        long res = 0;
        long i = 1;
        for (i = 1; i <= N; i = i << 1) {
            res = res + ((N - i) / (i << 1) + 1) * i;
        }
        out.print(res);
        closeStreams(out, in);
    }

    private static void closeStreams(OutputWriter out, InputReader in) throws IOException {
        out.flush();
        out.close();
        in.close();
    }

    static class InputReader {

        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9')
                    throw new InputMismatchException();
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public double readDouble() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            double res = 0;
            while (!isSpaceChar(c) && c != '.') {
                if (c == 'e' || c == 'E') {
                    return res * Math.pow(10, readInt());
                }
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            }
            if (c == '.') {
                c = read();
                double m = 1;
                while (!isSpaceChar(c)) {
                    if (c == 'e' || c == 'E') {
                        return res * Math.pow(10, readInt());
                    }
                    if (c < '0' || c > '9') {
                        throw new InputMismatchException();
                    }
                    m /= 10;
                    res += (c - '0') * m;
                    c = read();
                }
            }
            return res * sgn;
        }

        public long readLong() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public String readString() {
            int c = read();
            while (isSpaceChar(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public boolean isEndOfLine(int c) {
            if (filter != null) {
                return filter.isEndOfLine(c);
            }
            return c == '\n' || c == '\r' || c == -1;
        }

        public String next() {
            return readString();
        }

        public void close() throws IOException {
            this.stream.close();
        }

        public interface SpaceCharFilter {
            boolean isSpaceChar(int ch);

            boolean isEndOfLine(int ch);
        }

    }

    static class IOUtils {

        public static int[] readIntArray(InputReader in, int elementCount) {
            return readIntArray(in, elementCount, 0);
        }

        public static int[] readIntArray(InputReader in, int elementCount, int startOffset) {
            int[] array = new int[elementCount + startOffset];
            for (int i = 0; i < elementCount; i++)
                array[i + startOffset] = in.readInt();
            return array;
        }

        public static long[] readLongArray(InputReader in, int elementCount) {
            return readLongArray(in, elementCount, 0);
        }

        public static long[] readLongArray(InputReader in, int elementCount, int startOffset) {
            long[] array = new long[elementCount + startOffset];
            for (int i = 0; i < elementCount; i++)
                array[i + startOffset] = in.readLong();
            return array;
        }

    }

    static class OutputWriter {

        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void print(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                if (i != 0)
                    writer.print(' ');
                writer.print(objects[i]);
            }
        }

        public void printLine(Object... objects) {
            print(objects);
            writer.println();
        }

        public void close() {
            writer.close();
        }

        public void flush() {
            writer.flush();
        }

    }
}

NoteIn the first sample:  The weight of the minimum spanning tree is 1+2+1=4.

The only line contains an integer x, the weight of the graph's minimum spanning tree.

Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem that combines both. He has a complete graph consisting of n vertices numbered from 0 to n - 1. For all 0 ≤ u < v < n, vertex u and vertex v are connected with an undirected edge that has weight  (where  is the bitwise-xor operation). Can you find the weight of the minimum spanning tree of that graph?You can read about complete graphs in https://en.wikipedia.org/wiki/Complete_graphYou can read about the minimum spanning tree in https://en.wikipedia.org/wiki/Minimum_spanning_treeThe weight of the minimum spanning tree is the sum of the weights on the edges included in it.


The first and only line of the input contains a single integer n (3 ≤ n ≤ 54321) - the number of vertices of the regular polygon drawn by Ari.

import java.util.Scanner;

public class cls_div2_328_B {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        long n = scan.nextInt();

        scan.close();

        long result = (n - 2) + ((n - 3) * (n - 2));

        System.out.println(result);
    }
}

NoteOne of the possible solutions for the first sample is shown on the picture above.

Print the minimum number of jumps Ada should make to collect all the walnuts. Note, that she doesn't need to leave the polygon after.

Ari the monster always wakes up very early with the first ray of the sun and the first thing she does is feeding her squirrel.Ari draws a regular convex polygon on the floor and numbers it's vertices 1, 2, ..., n in clockwise order. Then starting from the vertex 1 she draws a ray in the direction of each other vertex. The ray stops when it reaches a vertex or intersects with another ray drawn before. Ari repeats this process for vertex 2, 3, ..., n (in this particular order). And then she puts a walnut in each region inside the polygon.  Ada the squirrel wants to collect all the walnuts, but she is not allowed to step on the lines drawn by Ari. That means Ada have to perform a small jump if she wants to go from one region to another. Ada can jump from one region P to another region Q if and only if P and Q share a side or a corner.Assuming that Ada starts from outside of the picture, what is the minimum number of jumps she has to perform in order to collect all the walnuts?


The first line of the input contains integer number $$$n$$$ ($$$2 \le n \le 100$$$) — the length of string $$$s$$$. The second line of the input contains the string $$$s$$$ consisting of $$$n$$$ capital Latin letters.

import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		String s=sc.next();
		char c[]=s.toCharArray();
		char aa='1',bb='1';
		int max=-1;
		for(int i=0;i<c.length-1;i++){
			char a=c[i],b=c[i+1];
			int con=1;
			for(int j=i+1;j<c.length-1;j++){
				if(c[j]==c[i]&&c[j+1]==c[i+1]){
					con++;
				}
			}
			if(con>max){
				max=con;
				aa=a;bb=b;
			}else if(con==max&&(a<aa||(a==aa&&b<bb))){
					aa=a;bb=b;
			}
		}
		System.out.println(aa+""+bb);
	}
}

NoteIn the first example "BA" is also valid answer.In the second example the only two-gram "ZZ" can be printed because it contained in the string "ZZZAA" two times.

Print the only line containing exactly two capital Latin letters — any two-gram contained in the given string $$$s$$$ as a substring (i.e. two consecutive characters of the string) maximal number of times.

Two-gram is an ordered pair (i.e. string of length two) of capital Latin letters. For example, "AZ", "AA", "ZA" — three distinct two-grams.You are given a string $$$s$$$ consisting of $$$n$$$ capital Latin letters. Your task is to find any two-gram contained in the given string as a substring (i.e. two consecutive characters of the string) maximal number of times. For example, for string $$$s$$$ = "BBAABBBA" the answer is two-gram "BB", which contained in $$$s$$$ three times. In other words, find any most frequent two-gram.Note that occurrences of the two-gram can overlap with each other.

Input:
7
ABACABA

Output: BA

Output: AB

Input:
5
ZZZAA

Output: ZZ

Input:
26
QWERTYUIOPASDFGHJKLZXCVBNM

Output: KL

Output: QW

Output: WE

Output: AS

Input:
2
QA

Output: QA

Input:
2
WW

Output: WW

Input:
11
GGRRAATTZZZ

Output: ZZ

Input:
50
NYQAHBYYOXLTRYQDMVENEMAQNBAKGLGQOLXNAIFNQTOCLNNQIA

Output: NQ

Output: YQ

Input:
100
URXCAIZFIBNJTPCZHBQIBCILLPXZCFGMKKZMNPLCYGAVJVIBMCZEBSJWPSCPQDYCTTKPOKIJRSKIZPDGCHVOUTMPNECYORSFZFNC

Output: IB

Input:
100
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

Output: AA

Input:
10
SQSQSQSQTG

Output: SQ

Input:
5
AZAZA

Output: ZA

Output: AZ

Input:
15
MIRZOYANOVECLOX

Output: AN

Output: IR

Output: MI

Output: NO

Input:
9
EGORLETOV

Output: GO

Output: EG

Output: TO

Input:
8
PUTINVOR

Output: PU

Output: IN

Output: UT

Output: NV

Input:
7
KADUROV

Output: KA

Output: AD

Input:
6
AZAZAZ

Output: AZ

Input:
3
LOL

Output: OL

Output: LO

Input:
3
KEK

Output: EK

Output: KE

Input:
5
FUFEL

Output: UF

Output: EL

Output: FU

Input:
9
MIKEPIDOR

Output: EP

Output: MI

Output: DO

Output: IK

Input:
9
AAAAAAAAA

Output: AA

Input:
23
AABBBAAACCCCCAAADDDDDDD

Output: DD

[{'input': '100\r\nURXCAIZFIBNJTPCZHBQIBCILLPXZCFGMKKZMNPLCYGAVJVIBMCZEBSJWPSCPQDYCTTKPOKIJRSKIZPDGCHVOUTMPNECYORSFZFNC\r\n', 'output': ['IB']}, {'input': '9\r\nAAAAAAAAA\r\n', 'output': ['AA']}, {'input': '23\r\nAABBBAAACCCCCAAADDDDDDD\r\n', 'output': ['DD']}, {'input': '7\r\nABACABA\r\n', 'output': ['BA', 'AB']}, {'input': '9\r\nEGORLETOV\r\n', 'output': ['GO', 'EG', 'TO']}]

[{'input': '3\r\nLOL\r\n', 'output': ['OL', 'LO']}, {'input': '9\r\nMIKEPIDOR\r\n', 'output': ['EP', 'MI', 'DO', 'IK']}, {'input': '2\r\nWW\r\n', 'output': ['WW']}, {'input': '11\r\nGGRRAATTZZZ\r\n', 'output': ['ZZ']}, {'input': '100\r\nURXCAIZFIBNJTPCZHBQIBCILLPXZCFGMKKZMNPLCYGAVJVIBMCZEBSJWPSCPQDYCTTKPOKIJRSKIZPDGCHVOUTMPNECYORSFZFNC\r\n', 'output': ['IB']}]

[{'input': '7\r\nKADUROV\r\n', 'output': ['KA', 'AD']}, {'input': '9\r\nEGORLETOV\r\n', 'output': ['GO', 'EG', 'TO']}, {'input': '50\r\nNYQAHBYYOXLTRYQDMVENEMAQNBAKGLGQOLXNAIFNQTOCLNNQIA\r\n', 'output': ['NQ', 'YQ']}, {'input': '5\r\nAZAZA\r\n', 'output': ['ZA', 'AZ']}, {'input': '15\r\nMIRZOYANOVECLOX\r\n', 'output': ['AN', 'IR', 'MI', 'NO']}]

[{'input': '11\r\nGGRRAATTZZZ\r\n', 'output': ['ZZ']}, {'input': '9\r\nAAAAAAAAA\r\n', 'output': ['AA']}, {'input': '2\r\nQA\r\n', 'output': ['QA']}, {'input': '9\r\nMIKEPIDOR\r\n', 'output': ['EP', 'MI', 'DO', 'IK']}, {'input': '5\r\nFUFEL\r\n', 'output': ['UF', 'EL', 'FU']}]

[{'input': '2\r\nWW\r\n', 'output': ['WW']}, {'input': '50\r\nNYQAHBYYOXLTRYQDMVENEMAQNBAKGLGQOLXNAIFNQTOCLNNQIA\r\n', 'output': ['NQ', 'YQ']}, {'input': '10\r\nSQSQSQSQTG\r\n', 'output': ['SQ']}, {'input': '5\r\nAZAZA\r\n', 'output': ['ZA', 'AZ']}, {'input': '5\r\nFUFEL\r\n', 'output': ['UF', 'EL', 'FU']}]


The first line contains two integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ 100) — the number of seconds the cutlet should be cooked on each side and number of periods of time in which Arkady can flip it. The next k lines contain descriptions of these intervals. Each line contains two integers li and ri (0 ≤ li ≤ ri ≤ 2·n), meaning that Arkady can flip the cutlet in any moment starting from li seconds after the beginning of cooking and finishing at ri seconds after beginning of cooking. In particular, if li = ri then Arkady can flip the cutlet only in the moment li = ri. It's guaranteed that li > ri - 1 for all 2 ≤ i ≤ k.

import java.io.*;
import java.util.*;
public class CF939_D2_F {
	public static void main(String[] args)throws Throwable {
		MyScanner sc=new MyScanner();
		PrintWriter pw=new PrintWriter(System.out);

		int n=sc.nextInt()*2;
		int k=sc.nextInt();
		int [] l=new int [k+1];
		int [] r=new int [k+1];
		SparseTable [][] seg=new SparseTable [2][2];
		for(int i=0;i<k;i++){
			l[i]=sc.nextInt();
			r[i]=sc.nextInt();
		}
		l[k]=n;
		for(int j=0;j<2;j++){
			int [] a=new int [n/2+2];
			Arrays.fill(a, inf);
			a[n/2]=0;
			seg[1][j]=new SparseTable(a);
		}
		int p=0;
		for(int i=k-1;i>=0;i--){
			for(int j=0;j<2;j++){
				int [] a=new int [n/2+2];
				Arrays.fill(a, inf);
				for(int c=0;c<=l[i] && c<a.length;c++){
					int best=seg[1-p][j].query(c+l[i+1]-l[i], c+l[i+1]-l[i]);
					best=Math.min(best, 1+seg[1-p][1-j].query(l[i+1]-(c+r[i]-l[i]), l[i+1]-(c)));
					if(r[i]>l[i])
						best=Math.min(best, 2+seg[1-p][j].query(c+l[i+1]-r[i], c+l[i+1]-l[i]-1));
					a[c]=best;
				}
				seg[p][j]=new SparseTable(a);
			}
			p^=1;
		}
		p^=1;
		int ans=Math.min(seg[p][0].query(l[0], l[0]), seg[p][1].query(l[0], l[0]));
		if(ans>2*k)
			pw.println("Hungry");
		else{
			pw.println("Full");
			pw.println(ans);
		}
		pw.flush();
		pw.close();
	}

	static int inf=(int)1e7;

	static class SparseTable {
		int [] a;
		int [][] st;
		//st[i][j] --> minimum of sub array starting at index i and of length 2^j
		SparseTable(int [] a){
			int n=a.length;
			this.a=a;
			int k=(int)Math.floor(Math.log(n)/Math.log(2))+1;
			st=new int [n][k];
			for(int i=0;i<n;i++)
				st[i][0]=i;
			//(1<<j)===(2^j)
			for(int j=1;(1<<j)<=n;j++)
				for(int i=0;i+(1<<j)-1<n;i++)
					if(a[st[i][j-1]] < a[st[i+(1<<(j-1))][j-1]])
						st[i][j]=st[i][j-1]; //
					else
						st[i][j]=st[i+(1<<(j-1))][j-1];
		}
		int query(int i,int j){
			if(i>=a.length)
				return inf;
			j=Math.min(j, a.length-1);
			int k=(int)Math.floor(Math.log(j-i+1)/Math.log(2));
			return Math.min(a[st[i][k]], a[st[j-(1<<k)+1][k]]);
		}
	}

	static class MyScanner {
		BufferedReader br;
		StringTokenizer st;
		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
		String next() {while (st == null || !st.hasMoreElements()) {
			try {st = new StringTokenizer(br.readLine());}
			catch (IOException e) {e.printStackTrace();}}
		return st.nextToken();}
		int nextInt() {return Integer.parseInt(next());}
		long nextLong() {return Long.parseLong(next());}
		double nextDouble() {return Double.parseDouble(next());}
		String nextLine(){String str = "";
		try {str = br.readLine();}
		catch (IOException e) {e.printStackTrace();}
		return str;}
	}
}

NoteIn the first example Arkady should flip the cutlet in time moment 3 seconds after he starts cooking and in time moment 13 seconds after he starts cooking.In the second example, Arkady can flip the cutlet at 10 seconds after he starts cooking.

Output "Hungry" if Arkady won't be able to fry the cutlet for exactly n seconds on one side and exactly n seconds on the other side. Otherwise, output "Full" in the first line, and the minimum number of times he should flip the cutlet in the second line.

Arkady wants to have a dinner. He has just returned from a shop where he has bought a semifinished cutlet. He only needs to fry it. The cutlet should be fried for 2n seconds, in particular, it should be fried for n seconds on one side and n seconds on the other side. Arkady has already got a frying pan and turn on fire, but understood that maybe he won't be able to flip the cutlet exactly after n seconds after the beginning of cooking.Arkady is too busy with sorting sticker packs in his favorite messenger and can flip the cutlet only in some periods of time. Namely, there are k periods of time in which he can do it, the i-th of them is an interval of time from li seconds after he starts cooking till ri seconds, inclusive. Arkady decided that it's not required to flip the cutlet exactly in the middle of cooking, instead, he will flip it several times in such a way that the cutlet will be fried exactly n seconds on one side and n seconds on the other side in total.Help Arkady and find out if it's possible for him to cook the cutlet, if he is able to flip the cutlet only in given periods of time; and if yes, find the minimum number of flips he needs to cook the cutlet.

Input:
10 2
3 5
11 13

Output:
Full
2

Input:
10 3
3 5
9 10
11 13

Output:
Full
1

Input:
20 1
3 19

Output: Hungry

Input:
10 1
0 20

Output:
Full
1

Input:
10 1
0 1

Output: Hungry

Input:
10 1
10 10

Output:
Full
1

Input:
10 2
4 4
14 14

Output:
Full
2

Input:
1 1
0 0

Output: Hungry

Input:
10 5
3 3
5 5
8 8
13 13
16 16

Output:
Full
2

Input:
10 7
0 0
2 6
8 10
12 12
14 14
17 17
19 19

Output:
Full
1

Input:
100 10
18 18
30 30
37 37
59 59
83 83
90 90
141 141
149 149
173 173
189 189

Output:
Full
3

Input:
100000 3
0 50000
99999 99999
199998 199998

Output:
Full
3

Input:
100000 17
7247 18957
56758 64403
79823 83648
83649 83715
83732 84946
84947 84963
84964 84968
84970 84978
84982 84991
84992 87130
172421 176513
176514 176596
176629 176689
176692 177213
197692 199830
199831 199993
199997 200000

Output:
Full
3

Input:
100000 20
1425 1425
14050 14050
17375 17375
17609 17609
22704 22704
25922 25922
37894 37894
92308 92308
94002 94002
99619 99619
103208 103208
110194 110194
114468 114468
141214 141214
145980 145980
159553 159553
168441 168441
169633 169633
182674 182674
195738 195738

Output:
Full
8

Input:
100000 3
54962 59962
98273 103273
174042 179042

Output:
Full
1

Input:
100000 20
5000 9999
14999 19998
24998 29997
34997 39996
44996 49995
54995 59994
64994 69993
74993 79992
84992 89991
94991 99990
104990 109989
114989 119988
124988 129987
134987 139986
144986 149985
154985 159984
164984 169983
174983 179982
184982 189981
194981 199980

Output:
Full
2

Input:
100 19
1 1
14 14
16 16
36 36
45 45
51 51
67 67
77 77
90 90
106 106
116 116
129 129
142 142
153 153
168 169
180 180
183 183
185 185
191 191

Output:
Full
2

Input:
1000 10
1 1
122 122
502 502
687 687
731 731
737 737
825 825
878 878
1159 1159
1396 1396

Output: Hungry

Input:
1000 4
184 196
726 737
1114 1131
1571 1581

Output:
Full
4

Input:
1000 6
292 304
1135 1147
1338 1350
1472 1491
1720 1732
1773 1790

Output:
Full
4

Input:
1000 5
509 528
540 551
1332 1347
1732 1743
1777 1787

Output:
Full
3

Input:
100000 1
0 200000

Output:
Full
1

Input:
100000 1
100000 100000

Output:
Full
1

Input:
100000 2
234 234
99766 99766

Output: Hungry

Input:
100000 2
0 99999
100001 200000

Output:
Full
2

Input:
511 18
1 1
2 2
4 4
6 6
10 10
14 14
22 22
30 30
46 46
62 62
94 94
126 126
190 190
254 254
382 382
510 510
766 766
1022 1022

Output:
Full
9

Input:
1000 20
225 225
429 429
560 560
632 632
650 650
704 704
768 768
797 797
983 983
991 991
1046 1046
1082 1082
1233 1233
1366 1366
1394 1394
1456 1456
1459 1459
1519 1519
1967 1967
1996 1996

Output:
Full
4

Input:
10000 10
479 479
1024 1024
4388 4388
4810 4810
6557 6557
9697 9697
11393 11393
12124 12124
14600 14600
17536 17536

Output:
Full
7

Input:
10000 20
746 746
1145 1145
1897 1897
4254 4254
6893 6893
7434 7434
8130 8130
9755 9755
10033 10033
10636 10636
11342 11342
11651 11651
12005 12005
14567 14567
15196 15196
15947 15947
16385 16385
17862 17862
18540 18540
18948 18948

Output:
Full
5

Input:
10000 12
1407 1407
1878 1878
4636 4636
5055 5055
5640 5640
6379 6379
6490 6490
10303 10303
13028 13028
13578 13578
18040 18040
19477 19477

Output:
Full
8

Input:
55 20
1 1
2 2
4 4
6 6
9 9
12 12
16 16
20 20
25 25
30 30
36 36
42 42
49 49
56 56
64 64
72 72
81 81
90 90
100 100
110 110

Output:
Full
2

Input:
6 6
3 3
5 5
7 7
8 8
9 9
12 12

Output:
Full
2

Input:
100000 4
0 40000
41000 80000
99999 99999
199998 199998

Output:
Full
3

Input:
100000 12
1 1751
23999 25007
33798 37031
37117 37426
37428 37436
37437 40132
48648 51062
51071 51743
51763 54643
116077 119442
190627 195558
197662 200000

Output:
Full
3

Input:
100000 14
213 1640
6778 14112
62548 68221
68495 68864
68887 68889
68890 68894
68896 68988
69034 71515
73645 77764
80059 81085
81086 81589
136294 151585
194157 199448
199559 200000

Output:
Full
3

Input:
100000 16
1 7
9 307
405 5574
50346 54067
54069 54100
54101 55097
56093 61752
77951 78580
78585 80749
85191 87424
87485 87490
87491 87694
87715 94544
136369 138773
140012 143346
195045 200000

Output:
Full
3

Input:
100000 2
60000 81999
120000 140000

Output:
Full
3

Input:
100000 12
65418 84245
86341 90510
135508 139243
139287 139389
139393 139437
139440 147819
198670 199954
199955 199963
199968 199979
199980 199985
199986 199997
199999 200000

Output:
Full
3

Input:
100000 11
42866 45922
45923 49957
63729 84014
115856 125872
125988 126003
126004 129147
131201 134555
183782 189949
189955 189967
189968 197363
198291 200000

Output:
Full
2

Input:
100000 8
69804 76492
76493 78217
129407 137816
137817 139388
142035 152201
153150 162227
196326 199996
200000 200000

Output:
Full
3

Input:
100000 18
16 46
47 154
445 526
537 571
572 573
574 580
582 5922
70364 73612
73625 80571
81628 88168
122927 127021
127027 127056
127204 127409
127410 134203
145658 155259
155270 163684
198635 199999
200000 200000

Output:
Full
3

Input:
100000 15
10387 11995
12012 12188
12297 14393
14589 15140
17771 26488
68905 72975
73509 73881
73886 73886
73887 79513
143598 147981
150145 152841
189148 199265
199597 199724
199772 199994
199999 200000

Output:
Full
3

Input:
100000 12
589 2312
2349 12326
12499 12759
12796 21228
70394 77570
77571 86238
133314 135096
135104 135113
135118 135128
135135 137324
190272 199989
199998 200000

Output:
Full
4

Input:
100000 14
3182 5382
5847 10785
26776 36809
36961 39608
65919 72524
73806 75651
79173 81114
81115 84538
112469 113763
113767 113771
113777 113790
113792 119192
193181 198259
199859 200000

Output:
Full
3

Input:
100000 18
3 535
551 7905
74333 87542
124358 135027
135108 142254
142265 143895
144091 145169
145255 145273
145275 145275
145279 145295
145302 145336
145337 145348
145350 145429
145430 145431
145441 145459
145460 147266
198447 199999
200000 200000

Output:
Full
3

Input:
100000 8
244 293
379 886
68058 75221
102015 112569
140672 146088
146090 146284
146289 149770
197995 200000

Output:
Full
3

Input:
100000 5
18547 19547
24249 25249
58262 59262
102965 103965
109453 110453

Output:
Full
5

Input:
100000 9
5071 6797
6916 13337
64413 72188
72231 72441
72458 74946
122835 133275
133562 134079
134098 141894
195543 200000

Output:
Full
4

Input:
100000 6
8828 9828
81857 82857
88071 89071
94010 95010
141844 142844
165669 166669

Output:
Full
6

Input:
100000 7
5645 6645
30563 31563
75140 76140
107764 108764
108910 109910
162122 163122
169774 170774

Output:
Full
3

Input:
100000 7
17993 18993
30906 31906
49354 50354
60696 61696
106638 107638
188177 189177
190333 191333

Output:
Full
3

Input:
100000 18
299 2359
2646 3120
3122 3123
3124 4562
5401 5753
5754 10619
72022 81017
81018 81019
81020 81020
81021 81573
82730 83638
83643 83648
83663 83668
83669 83673
83678 83681
83686 91779
156345 158432
194512 200000

Output:
Full
4

Input:
100000 6
397 1397
15892 16892
35746 36746
94193 95193
166848 167848
185228 186228

Output:
Full
5

Input:
100000 16
41569 49839
49854 54485
59507 68882
68884 69855
69997 72083
105481 108926
108927 108944
108969 109043
109105 109306
110096 110365
110573 114375
180359 187643
191157 196987
197083 197113
197140 197892
199113 200000

Output:
Full
3

Input:
1000 2
909 961
1820 1859

Output:
Full
3

Input:
1000 5
123 174
716 789
1284 1360
1415 1443
1623 1648

Output:
Full
3

Input:
1000 5
381 426
1092 1122
1462 1481
1708 1756
1799 1847

Output:
Full
3

Input:
1000 4
241 259
1127 1154
1219 1239
1739 1758

Output:
Full
4

Input:
1000 5
388 407
488 504
640 658
856 875
1060 1074

Output:
Full
9

Input:
1000 5
182 199
444 460
628 640
693 708
1107 1119

Output:
Full
7

Input:
1000 2
771 837
1015 1049

Output:
Full
3

Input:
1000 3
581 617
802 825
1040 1080

Output:
Full
5

Input:
100000 7
27522 27693
47266 47410
58768 58929
64532 64665
141173 141356
150364 150551
183020 183160

Output:
Full
9


The input contains a single line consisting of $$$2$$$ integers $$$N$$$ and $$$M$$$ ($$$1 \le N \le 10^{18}$$$, $$$2 \le M \le 100$$$).

import java.util.*;
public class issam{
    static class consecutive{
        long mod;
        long[][] matrix;
        long nbWays(long n,int m,long mod){
            this.mod = mod;
            if(n<(long)m) return 1;
            if(n==(long)m) return 2;
            if(m==1) return nbPow(2,n);
            matrix = new long[m][m];
            for(int i=1;i<m;i++) matrix[i][i-1] = 1;
            matrix[0][0] = 1;
            matrix[0][m-1] = 1;
            matrix = pow(matrix,n-(long)m+1);
            long ans = 0;
            for(int i=0;i<m;i++){
                ans += matrix[0][i];
                ans %= mod;
            }
            return ans;
        }

        long nbPow(long a,long n){
            if(n==1) return a;
            if(n%2==0){
                long ans = nbPow(a,n/2);
                return (ans*ans)%mod;
            }
            return (a*nbPow(a,n-1))%mod;
        }

        long[][] pow(long matrix[][], long m) {
            if (m == 1)
                return matrix;
            if (m % 2 == 0) {
                long mat[][] = pow(matrix, m / 2);
                return multiply(mat, mat);
            }
            return multiply(matrix, pow(matrix, m - 1));
        }

        long[][] multiply(long matrix1[][], long matrix2[][]) {
            long ans[][] = new long[matrix1.length][matrix2[0].length];
            for (int i = 0; i < matrix1.length; i++) {
                for (int j = 0; j < matrix2[0].length; j++) {
                    for (int k = 0; k < matrix1[0].length; k++) {
                        ans[i][j] += (matrix1[i][k] * matrix2[k][j]) % mod;
                        ans[i][j] = ans[i][j] % mod;
                    }
                }
            }
            return ans;
        }

    }
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        int m = sc.nextInt();
        consecutive c = new consecutive();
        long mod = 1000;
        mod = mod*mod*mod+7;
        long res = c.nbWays(n,m,mod);
        System.out.println(res);
    }
}

NoteIn the first example each magic gem can split into $$$2$$$ normal gems, and we know that the total amount of gems are $$$4$$$.Let $$$1$$$ denote a magic gem, and $$$0$$$ denote a normal gem.The total configurations you can have is:   $$$1 1 1 1$$$ (None of the gems split);  $$$0 0 1 1$$$ (First magic gem splits into $$$2$$$ normal gems);  $$$1 0 0 1$$$ (Second magic gem splits into $$$2$$$ normal gems);  $$$1 1 0 0$$$ (Third magic gem splits into $$$2$$$ normal gems);  $$$0 0 0 0$$$ (First and second magic gems split into total $$$4$$$ normal gems). Hence, answer is $$$5$$$.

Print one integer, the total number of configurations of the resulting set of gems, given that the total amount of space taken is $$$N$$$ units. Print the answer modulo $$$1000000007$$$ ($$$10^9+7$$$).

Reziba has many magic gems. Each magic gem can be split into $$$M$$$ normal gems. The amount of space each magic (and normal) gem takes is $$$1$$$ unit. A normal gem cannot be split.Reziba wants to choose a set of magic gems and split some of them, so the total space occupied by the resulting set of gems is $$$N$$$ units. If a magic gem is chosen and split, it takes $$$M$$$ units of space (since it is split into $$$M$$$ gems); if a magic gem is not split, it takes $$$1$$$ unit.How many different configurations of the resulting set of gems can Reziba have, such that the total amount of space taken is $$$N$$$ units? Print the answer modulo $$$1000000007$$$ ($$$10^9+7$$$). Two configurations are considered different if the number of magic gems Reziba takes to form them differs, or the indices of gems Reziba has to split differ.

Input: 4 2

Output: 5

Input: 3 2

Output: 3

Input: 1000000000000000000 2

Output: 680057396

Input: 1000000000000000000 3

Output: 615472476

Input: 1000000000000000000 5

Output: 285726867

Input: 1000000000000000000 42

Output: 683499162

Input: 1000000000000000000 69

Output: 864131130

Input: 1000000000000000000 99

Output: 847914677

Input: 1000000000000000000 100

Output: 161502719

Input: 1 2

Output: 1

Input: 1 3

Output: 1

Input: 1 4

Output: 1

Input: 1 5

Output: 1

Input: 2 2

Output: 2

Input: 2 3

Output: 1

Input: 2 4

Output: 1

Input: 2 5

Output: 1

Input: 5 2

Output: 8

Input: 5 3

Output: 4

Input: 5 4

Output: 3

Input: 5 5

Output: 2

Input: 6 2

Output: 13

Input: 6 3

Output: 6

Input: 6 4

Output: 4

Input: 6 5

Output: 3

Input: 7 2

Output: 21

Input: 7 3

Output: 9

Input: 7 4

Output: 5

Input: 7 5

Output: 4

Input: 99 100

Output: 1

Input: 99 99

Output: 2

Input: 69 30

Output: 96

Input: 69 2

Output: 489376391

Input: 25 2

Output: 121393

Input: 576460752303423487 100

Output: 163029191

Input: 144115188075855871 100

Output: 156647062

Input: 288230376151711743 100

Output: 626842151


The single line contains a single integer n (1 ≤ n ≤ 105).

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Sparsh Sanchorawala
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        CInterestingGame solver = new CInterestingGame();
        solver.solve(1, in, out);
        out.close();
    }

    static class CInterestingGame {
        public void solve(int testNumber, InputReader s, PrintWriter w) {
            int n = s.nextInt();
            ArrayList<Pair>[] pair = new ArrayList[n + 1];
            for (int i = 1; i <= n; i++)
                pair[i] = new ArrayList<>();
            for (int k = 2; k <= n; k++)
                for (int a = 1; (long) (2 * a + k - 1) * k / 2 <= n; a++)
                    pair[(2 * a + k - 1) * k / 2].add(new Pair(a, k));
            int[] grundy = new int[n + 1];
            int[] val = new int[n + 1];
            Arrays.fill(val, Integer.MAX_VALUE);
            int[] pre = new int[n + 1];
            for (int i = 2; i <= n; i++) {
                HashSet<Integer> hs = new HashSet<>();
                for (Pair p : pair[i]) {
                    hs.add(pre[p.a + p.n - 1] ^ pre[p.a - 1]);
                    if ((pre[p.a + p.n - 1] ^ pre[p.a - 1]) == 0)
                        val[i] = Math.min(p.n, val[i]);
                }
                while (hs.contains(grundy[i]))
                    grundy[i]++;
                pre[i] = grundy[i] ^ pre[i - 1];
            }
            w.println(val[n] != Integer.MAX_VALUE ? val[n] : -1);
        }

        class Pair {
            int a;
            int n;

            Pair(int a, int n) {
                this.a = a;
                this.n = n;
            }

        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        public int read() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) {
                    return -1;
                }
            }
            return buf[curChar++];
        }

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

        public static boolean isWhitespace(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}

If Serozha wins, print k, which represents the minimal number of piles into which he can split the initial one during the first move in order to win the game. If Gena wins, print "-1" (without the quotes).

Two best friends Serozha and Gena play a game.Initially there is one pile consisting of n stones on the table. During one move one pile should be taken and divided into an arbitrary number of piles consisting of a1 > a2 > ... > ak > 0 stones. The piles should meet the condition a1 - a2 = a2 - a3 = ... = ak - 1 - ak = 1. Naturally, the number of piles k should be no less than two.The friends play in turns. The player who cannot make a move loses. Serozha makes the first move. Who will win if both players play in the optimal way?


The only line contains six integers a1, b1, a2, b2, L, R (0 < a1, a2 ≤ 2·109,  - 2·109 ≤ b1, b2, L, R ≤ 2·109, L ≤ R).

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.math.BigInteger;
import java.util.Random;
import java.util.StringTokenizer;

public class D {
    public static void main(String[] args) {
        FastScanner sc = new FastScanner();

        long a1 = sc.nextLong();
        long b1 = sc.nextLong();
        long a2 = sc.nextLong();
        long b2 = sc.nextLong();
        long left = sc.nextLong();
        long right = sc.nextLong();

        long lcm = lcm(a1, a2);
        left = Math.max(left, Math.max(b1, b2));

        // a1*x + b1 = a2*y + b2
        // a1*x - a2*y = b1 - b2
        long[] x_y_gcd = solveDiophantine(a1, -a2, b2 - b1);
        if (x_y_gcd == null) {
            System.out.println(0);
        } else {
            BigInteger a1Bi = BigInteger.valueOf(a1);
            BigInteger b1Bi = BigInteger.valueOf(b1);
            BigInteger leftBi = BigInteger.valueOf(left);
            BigInteger rightBi = BigInteger.valueOf(right);
            BigInteger lcmBi = BigInteger.valueOf(lcm);

            BigInteger firstEq = a1Bi.multiply(BigInteger.valueOf(x_y_gcd[0])).add(b1Bi);
            BigInteger diff = leftBi.subtract(firstEq).divide(lcmBi).subtract(BigInteger.ONE);
            firstEq = firstEq.add(diff.multiply(lcmBi));
            while (firstEq.compareTo(leftBi) < 0) {
                firstEq = firstEq.add(lcmBi);
            }
            if (rightBi.compareTo(firstEq) < 0) {
                System.out.println(0);
            } else {
                long count = Math.max(rightBi.subtract(firstEq).divide(lcmBi).longValue() + 1, 0);
                System.out.println(count);
            }
        }
    }

    /**
     * Finds a single solution {x, y} to the LDE a*x + b*y = c.
     * Outputs an array of the form {x, y, d}, where d = GCD(a,b).
     */
    private static long[] solveDiophantine(long a, long b, long c) {
        long[] e = extEuclid(a, b);
        long k = c / e[2];

        //c not divisible by the GCD(a,b) -> no solution
        if (c - k * e[2] != 0)
            return null;

        long[] output = {e[0] * k, e[1] * k, e[2]};
        return output;
    }

    /**
     * Extended Euclidean Algorithm finds {x, y, d}
     * where d=GCD(a,b) and x and y satisfy a*x + b*y = d
     * The output is an array of the form {x, y, d}.
     */
    private static long[] extEuclid(long a, long b) {
        long s0 = 1, s1 = 0, sTemp;
        long t0 = 0, t1 = 1, tTemp;
        long r0 = a, r1 = b, rTemp;
        long q;

        while (r1 != 0) {
            q = r0 / r1;

            rTemp = r1;
            r1 = r0 - q * r1;
            r0 = rTemp;

            sTemp = s1;
            s1 = s0 - q * s1;
            s0 = sTemp;

            tTemp = t1;
            t1 = t0 - q * t1;
            t0 = tTemp;
        }

        long[] output = {s0, t0, r0};
        return output;
    }

    static long lcm(long a, long b) {
        return a / gcd(a, b) * b;
    }

    static long gcd(long a, long b) {
        if (b == 0) return a;
        return gcd(b, a % b);
    }

    public static class FastScanner {
        BufferedReader br;
        StringTokenizer st;

        public FastScanner(Reader in) {
            br = new BufferedReader(in);
        }

        public FastScanner() {
            this(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String readNextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        int[] readIntArray(int n) {
            int[] a = new int[n];
            for (int idx = 0; idx < n; idx++) {
                a[idx] = nextInt();
            }
            return a;
        }

        long[] readLongArray(int n) {
            long[] a = new long[n];
            for (int idx = 0; idx < n; idx++) {
                a[idx] = nextLong();
            }
            return a;
        }
    }
}

You are given two arithmetic progressions: a1k + b1 and a2l + b2. Find the number of integers x such that L ≤ x ≤ R and x = a1k' + b1 = a2l' + b2, for some integers k', l' ≥ 0.

Input: 2 0 3 3 5 21

Output: 3

Input: 2 4 3 0 6 17

Output: 2

Input: 2 0 4 2 -39 -37

Output: 0

Input: 1 9 3 11 49 109

Output: 20

Input: 3 81 5 72 -1761 501

Output: 28

Input: 8 -89 20 67 8771 35222

Output: 661

Input: 1 -221 894 86403 -687111 141371

Output: 62

Input: 1 -1074 271 17741 -2062230 1866217

Output: 6821

Input: 3 2408 819 119198 -8585197 7878219

Output: 9474

Input: 1 341 8581 3946733 -59420141 33253737

Output: 3416

Input: 1 10497 19135 2995296 -301164547 -180830773

Output: 0

Input: 8 40306 2753 1809818 254464419 340812028

Output: 3921

Input: 2 21697 9076 1042855 -319348358 236269755

Output: 25918

Input: 4 2963 394 577593 125523962 628140505

Output: 637839

Input: 75 61736 200 200511 160330870 609945842

Output: 749358

Input: 34 64314 836 5976 591751179 605203191

Output: 946

Input: 1 30929 25249 95822203 -1076436442 705164517

Output: 24134

Input: 3 -1208 459 933808 603490653 734283665

Output: 284952

Input: 1 35769 16801 47397023 -82531776 1860450454

Output: 107914

Input: 1 -3078 36929 51253687 -754589746 -53412627

Output: 0

Input: 1 -32720 3649 7805027 408032642 925337350

Output: 141766

Input: 1 -2000000000 1 -2000000000 -2000000000 2000000000

Output: 4000000001

Input: 1 -2000000000 2 -2000000000 -2000000000 2000000000

Output: 2000000001

Input: 3 -2000000000 2 -2000000000 -2000000000 2000000000

Output: 666666667

Input: 999999999 999999998 1000000000 999999999 1 10000

Output: 0

Input: 1 -2000000000 1 2000000000 1 10

Output: 0

Input: 1 -2000000000 2 2000000000 -2000000000 2000000000

Output: 1

Input: 2 0 2 1 0 1000000000

Output: 0

Input: 1000000000 0 1 0 0 2000000000

Output: 3

Input: 4 0 4 1 5 100

Output: 0

Input: 1000000000 1 999999999 0 1 100000000

Output: 0

Input: 1 30929 1 1 1 1

Output: 0

Input: 1 1 1 1 -2000000000 2000000000

Output: 2000000000

Input: 4 0 4 1 0 100

Output: 0

Input: 1 -2000000000 1 2000000000 5 5

Output: 0

Input: 51 -1981067352 71 -414801558 -737219217 1160601982

Output: 435075

Input: 2 -1500000000 4 -1499999999 1600000000 1700000000

Output: 0

Input: 135 -1526277729 32 1308747737 895574 1593602399

Output: 65938

Input: 1098197640 6 994625382 6 -474895292 -101082478

Output: 0

Input: 12 -696575903 571708420 236073275 2 14

Output: 0

Input: 1 -9 2 -10 -10 -9

Output: 0

Input: 2 -11 2 -9 -11 -9

Output: 1

Input: 40 54 15 74 -180834723 1373530127

Output: 11446084

Input: 2 57 1 56 -1773410854 414679043

Output: 207339494

Input: 9 12 1 40 624782492 883541397

Output: 28750990

Input: 4 -1000000000 2 4 100 1000

Output: 226

Input: 66 90 48 84 -1709970247 1229724777

Output: 2329024

Input: 1000000000 1 2000000000 0 -2000000000 200000000

Output: 0

Input: 2 0 2 1 -1000000000 1000000000

Output: 0

Input: 2 -1000000000 2 -999999999 -1000000000 1000000000

Output: 0

Input: 26 1885082760 30 -1612707510 -1113844607 1168679422

Output: 0

Input: 76 -19386 86 -6257 164862270 1443198941

Output: 0

Input: 5 -2000000000 5 1000000000 1000000000 2000000000

Output: 200000001

Input: 505086589 -4 1288924334 -4 -5 -4

Output: 1

Input: 91 -193581878 2 1698062870 -819102473 1893630769

Output: 1074549

Input: 8 11047 45 12730 -45077355 1727233357

Output: 4797835

Input: 35 8673 6 -19687 -111709844 1321584980

Output: 6293220

Input: 71 1212885043 55 1502412287 970234397 1952605611

Output: 115287

Input: 274497829 -12 9 -445460655 -5 4

Output: 0

Input: 1509527550 3 7 -134101853 2 7

Output: 1

Input: 43 -1478944506 45 494850401 634267177 1723176461

Output: 562743

Input: 25 479638866 50 -874479027 -2000000000 2000000000

Output: 0

Input: 11 -10 1 -878946597 -11127643 271407906

Output: 24673447

Input: 15 -738862158 12 -3 -3 12

Output: 1

Input: 70 -835526513 23 687193329 -1461506792 1969698938

Output: 796587

Input: 124 1413 15321 312133 3424 1443242

Output: 0

Input: 75 -13580 14 4508 -67634192 1808916097

Output: 1722773

Input: 915583842 -15 991339476 -12 -15 -5

Output: 0

Input: 85 -18257 47 -7345 -76967244 1349252598

Output: 337737

Input: 178 331734603 162 -73813367 -577552570 1005832995

Output: 46754

Input: 8 -17768 34 963 -2000000000 2000000000

Output: 0

Input: 26 1885082760 30 -1612707510 -2000000000 2000000000

Output: 294660

Input: 4 -1999999999 6 -1999999998 -999999999 1999999999

Output: 0

Input: 121826 1323 1327 304172 -1521910750 860413213

Output: 5

Input: 36281 170 1917 927519 -1767064448 -177975414

Output: 0

Input: 37189 -436 464 797102 -1433652908 1847752465

Output: 107

Input: 81427 -688 1720 -221771 -77602716 1593447723

Output: 11

Input: 11 -1609620737 1315657088 -7 -162162918 287749240

Output: 0

Input: 1480269313 -1048624081 1314841531 -8 295288505 358226461

Output: 0

Input: 13 -15 19 -2 -334847526 1334632952

Output: 5403373

Input: 1254161381 -7 821244830 -7 -698761303 941496965

Output: 1

Input: 1269100557 -5 6 -5 -12 -6

Output: 0

Input: 847666888 -6 1327933031 -6 -5 -2

Output: 0

Input: 1465846675 1002489474 9 -1250811979 1030017372 1391560043

Output: 0

Input: 8 -1915865359 867648990 9 -5 -4

Output: 0

Input: 3 -1164702220 906446587 -1868913852 222249893 1493113759

Output: 0

Input: 15 -8 17 3 -393290856 231975525

Output: 909708

Input: 734963978 0 17 0 -12 -5

Output: 0

Input: 1090004357 5 1124063714 -840327001 -448110704 128367602

Output: 0

Input: 18 -1071025614 1096150070 0 -6 0

Output: 1

Input: 451525105 -8 1256335024 -8 -718788747 928640626

Output: 1

Input: 4 3 5 -1292190012 -97547955 250011754

Output: 12500588

Input: 14 -7 14 -1488383431 -1044342357 842171605

Output: 0

Input: 1384140089 5 16 -1661922737 442287491 1568124284

Output: 0

Input: 16 -11 14 -1466771835 -1192555694 -2257860

Output: 0

Input: 1676164235 -1589020998 1924931103 1189158232 6 12

Output: 0

Input: 15 16 12 -5 11 23

Output: 0

Input: 16 -16 5 20 -9 7

Output: 0

Input: 4 -9 1 -2 -13 -1

Output: 1

Input: 18 -17 9 -17 -29 17

Output: 2

Input: 735463638 620656007 878587644 536507630 -1556948056 1714374073

Output: 0

Input: 1789433851 -633540112 1286318222 -1728151682 1438333624 1538194890

Output: 0

Input: 15 -1264610276 1157160166 -336457087 -496892962 759120142

Output: 0

Input: 831644204 422087925 17 -1288230412 -1090082747 1271113499

Output: 1

Input: 17 -13 223959272 -1081245422 -1756575771 38924201

Output: 1

Input: 1228969457 -1826233120 11 -1063855654 -819177202 1039858319

Output: 0

Input: 1186536442 -1691684240 17 -1 -702600351 1121394816

Output: 1

Input: 1132421757 -1481846636 515765656 -12 -622203577 552143596

Output: 0

Input: 18 -1123473160 1826212361 -10 -12 1

Output: 1

Input: 1197045662 7 15 -1445473718 -1406137199 800415943

Output: 1

Input: 18 565032929 13 735553852 107748471 1945959489

Output: 5172673

Input: 1734271904 1 19 -1826828681 0 4

Output: 1

Input: 1614979757 -1237127436 12 75067457 -933537920 451911806

Output: 1

Input: 8 -335942902 1179386720 -723257398 -13 -12

Output: 0

Input: 989432982 2 9 366779468 -1427636085 985664909

Output: 0

Input: 7 -1390956935 1404528667 -4 -15 0

Output: 1

Input: 1370475975 841789607 733784598 467967887 -7 15

Output: 0

Input: 6 -7 9 -1 -10 1

Output: 1

Input: 960716652 1417038753 1222139305 -4 -1570098546 -931528535

Output: 0

Input: 1744394473 5 1523286739 629247513 -6 1

Output: 0

Input: 2627 -4960 2627 -4960 -4960 4960

Output: 4

Input: 6 -364562196 7 -803430276 0 11

Output: 0

Input: 1955378240 -837482305 1743607821 -1623988108 -653286850 178227154

Output: 0

Input: 9 -1642366642 1499382371 -6 -822052389 1405478033

Output: 0

Input: 9 -1 8 -1 -711474975 237571596

Output: 3299606

Input: 1497677869 -1313800455 11 12 -1157529918 1754001465

Output: 1

Input: 11 -80049925 1600186381 -1454831688 -1384227392 1621203975

Output: 0

Input: 1042015302 -56794440 1727095321 -1037110962 -9 11

Output: 0

Input: 13 0 1419591662 -1360930956 343359607 1283114457

Output: 0

Input: 752411560 -6 857048450 -405514986 -5 0

Output: 0

Input: 12 2 18 2 -6 3

Output: 1

Input: 11 -1 15 -1 -13 2

Output: 1

Input: 1446642133 -7 9 -1719422944 -916435667 36154654

Output: 1

Input: 1689390799 501112014 13 -1621132473 398367938 709483101

Output: 0

Input: 1932547151 -725726769 782679113 -10 -184530763 498112212

Output: 0


The first line contains two space-separated numbers a1 and b1 — the sides of the board. Next two lines contain numbers a2, b2, a3 and b3 — the sides of the paintings. All numbers ai, bi in the input are integers and fit into the range from 1 to 1000.

import java.util.*;

public class CF560BGetaldIntoArts {
    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);
        boolean b = true;

        int a1 = s.nextInt();
        int b1 = s.nextInt();
        int a2 = s.nextInt();
        int b2 = s.nextInt();
        int a3 = s.nextInt();
        int b3 = s.nextInt();

        if(a1 >= Math.max(a2,a3) && b2+b3<=b1) b = true;

        else if(a1 >= Math.max(a2,b3) && b2+a3<=b1) b = true;

        else if(a1 >= Math.max(a3,b2) && b3+a2<=b1) b = true;

        else if(a1 >= Math.max(b2,b3) && a2+a3<=b1) b = true;
        else b = false;

        if(b1 >= Math.max(a2,a3) && b2+b3<=a1) b = true;

        else if(b1 >= Math.max(a2,b3) && b2+a3<=a1) b = true;

        else if(b1>=Math.max(a3,b2) && b3+a2<=a1) b = true;

        else if(b1>=Math.max(b2,b3) && a2+a3<=a1) b = true;

        if(b == false){
            System.out.println("NO");
        }
        else System.out.println("YES");

    }
}

NoteThat's how we can place the pictures in the first test:And that's how we can do it in the third one.

If the paintings can be placed on the wall, print "YES" (without the quotes), and if they cannot, print "NO" (without the quotes).

Gerald bought two very rare paintings at the Sotheby's auction and he now wants to hang them on the wall. For that he bought a special board to attach it to the wall and place the paintings on the board. The board has shape of an a1 × b1 rectangle, the paintings have shape of a a2 × b2 and a3 × b3 rectangles.Since the paintings are painted in the style of abstract art, it does not matter exactly how they will be rotated, but still, one side of both the board, and each of the paintings must be parallel to the floor. The paintings can touch each other and the edges of the board, but can not overlap or go beyond the edge of the board. Gerald asks whether it is possible to place the paintings on the board, or is the board he bought not large enough?

Input:
3 2
1 3
2 1

Output: YES

Input:
5 5
3 3
3 3

Output: NO

Input:
4 2
2 3
1 2

Output: YES

Input:
3 3
1 1
1 1

Output: YES

Input:
1000 1000
999 999
1 1000

Output: YES

Input:
7 7
5 5
2 4

Output: YES

Input:
3 3
2 2
2 2

Output: NO

Input:
2 9
5 1
3 2

Output: YES

Input:
9 9
3 8
5 2

Output: YES

Input:
10 10
10 5
4 3

Output: YES

Input:
10 6
10 1
5 7

Output: YES

Input:
6 10
6 3
6 2

Output: YES

Input:
7 10
7 5
1 7

Output: YES

Input:
10 10
7 4
3 5

Output: YES

Input:
4 10
1 1
9 3

Output: YES

Input:
8 7
1 7
3 2

Output: YES

Input:
5 10
5 2
3 5

Output: YES

Input:
9 9
9 7
2 9

Output: YES

Input:
8 10
3 8
7 4

Output: YES

Input:
10 10
6 6
4 9

Output: YES

Input:
8 9
7 6
2 3

Output: YES

Input:
10 10
9 10
6 1

Output: YES

Input:
90 100
52 76
6 47

Output: YES

Input:
84 99
82 54
73 45

Output: YES

Input:
100 62
93 3
100 35

Output: YES

Input:
93 98
75 32
63 7

Output: YES

Input:
86 100
2 29
71 69

Output: YES

Input:
96 100
76 21
78 79

Output: YES

Input:
99 100
95 68
85 32

Output: YES

Input:
97 100
95 40
70 60

Output: YES

Input:
100 100
6 45
97 54

Output: YES

Input:
99 100
99 72
68 1

Output: YES

Input:
88 100
54 82
86 45

Output: YES

Input:
91 100
61 40
60 88

Output: YES

Input:
100 100
36 32
98 68

Output: YES

Input:
78 86
63 8
9 4

Output: YES

Input:
72 93
38 5
67 64

Output: YES

Input:
484 1000
465 2
9 535

Output: YES

Input:
808 1000
583 676
527 416

Output: YES

Input:
965 1000
606 895
533 394

Output: YES

Input:
824 503
247 595
151 570

Output: YES

Input:
970 999
457 305
542 597

Output: YES

Input:
332 834
312 23
505 272

Output: YES

Input:
886 724
830 439
102 594

Output: YES

Input:
958 1000
326 461
836 674

Output: YES

Input:
903 694
104 488
567 898

Output: YES

Input:
800 1000
614 163
385 608

Output: YES

Input:
926 1000
813 190
187 615

Output: YES

Input:
541 1000
325 596
403 56

Output: YES

Input:
881 961
139 471
323 731

Output: YES

Input:
993 1000
201 307
692 758

Output: YES

Input:
954 576
324 433
247 911

Output: YES

Input:
7 3
7 8
1 5

Output: NO

Input:
5 9
2 7
8 10

Output: NO

Input:
10 4
4 3
5 10

Output: NO

Input:
2 7
8 3
2 7

Output: NO

Input:
1 4
7 2
3 2

Output: NO

Input:
5 8
5 1
10 5

Output: NO

Input:
3 5
3 6
10 7

Output: NO

Input:
6 2
6 6
1 2

Output: NO

Input:
10 3
6 6
4 7

Output: NO

Input:
9 10
4 8
5 6

Output: YES

Input:
3 8
3 2
8 7

Output: NO

Input:
3 3
3 4
3 6

Output: NO

Input:
6 10
1 8
3 2

Output: YES

Input:
8 1
7 5
3 9

Output: NO

Input:
9 7
5 2
4 1

Output: YES

Input:
100 30
42 99
78 16

Output: NO

Input:
64 76
5 13
54 57

Output: YES

Input:
85 19
80 18
76 70

Output: NO

Input:
57 74
99 70
86 29

Output: NO

Input:
22 21
73 65
92 35

Output: NO

Input:
90 75
38 2
100 61

Output: NO

Input:
62 70
48 12
75 51

Output: NO

Input:
23 17
34 71
98 34

Output: NO

Input:
95 72
65 31
89 50

Output: NO

Input:
68 19
39 35
95 65

Output: NO

Input:
28 65
66 27
5 72

Output: NO

Input:
100 16
41 76
24 15

Output: NO

Input:
21 63
28 73
60 72

Output: NO

Input:
85 18
37 84
35 62

Output: NO

Input:
58 64
98 30
61 52

Output: NO

Input:
32 891
573 351
648 892

Output: NO

Input:
796 846
602 302
600 698

Output: NO

Input:
665 289
608 360
275 640

Output: NO

Input:
237 595
318 161
302 838

Output: NO

Input:
162 742
465 429
571 29

Output: NO

Input:
222 889
491 923
76 195

Output: NO

Input:
794 140
166 622
378 905

Output: NO

Input:
663 287
193 212
615 787

Output: NO

Input:
427 433
621 441
868 558

Output: NO

Input:
1000 388
332 49
735 699

Output: NO

Input:
868 535
409 690
761 104

Output: YES

Input:
632 786
710 208
436 290

Output: YES

Input:
501 932
463 636
363 918

Output: NO

Input:
73 79
626 483
924 517

Output: NO

Input:
190 34
653 163
634 314

Output: NO

Input:
2 4
1 3
1 4

Output: YES

Input:
3 10
1 1
1 11

Output: NO

Input:
5 4
3 3
2 6

Output: NO

Input:
3 4
1 6
2 3

Output: NO


The first line contains two integers a and c (0 ≤ a, c ≤ 109). Both numbers are written in decimal notation.

// ~/BAU/ACM-ICPC/Teams/A++/BlackBurn95
// ~/sudo apt-get Accpeted

import java.io.*;
import java.util.*;
import java.math.*;
import static java.lang.Math.*;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Double.parseDouble;
import static java.lang.String.*;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder out = new StringBuilder();
        StringTokenizer tk;

        tk = new StringTokenizer(in.readLine());
        BigInteger a = new BigInteger(tk.nextToken()),c = new BigInteger(tk.nextToken());

        String A = a.toString(3),C = c.toString(3) ;

        if(A.length() < C.length()) {
            String tmp = "";
            for(int i=0; i<C.length()-A.length(); i++)
                tmp += "0";
            A = tmp+A;
        } else if(A.length() > C.length()) {
            String tmp = "";
            for(int i=0; i<A.length()-C.length(); i++)
                tmp += "0";
            C = tmp+C;
        }

        char [] B = new char[A.length()];
        for(int i=0; i<A.length(); i++)
            B[i] = (char)((C.charAt(i)-'0'-(A.charAt(i)-'0')+3)%3 + '0');

        BigInteger b = new BigInteger(valueOf(B),3);

        System.out.println(b.toString(10));
    }

}

Print the single integer b, such that a tor b = c. If there are several possible numbers b, print the smallest one. You should print the number in decimal notation.

Little Petya very much likes computers. Recently he has received a new "Ternatron IV" as a gift from his mother. Unlike other modern computers, "Ternatron IV" operates with ternary and not binary logic. Petya immediately wondered how the xor operation is performed on this computer (and whether there is anything like it).It turned out that the operation does exist (however, it is called tor) and it works like this. Suppose that we need to calculate the value of the expression a tor b. Both numbers a and b are written in the ternary notation one under the other one (b under a). If they have a different number of digits, then leading zeroes are added to the shorter number until the lengths are the same. Then the numbers are summed together digit by digit. The result of summing each two digits is calculated modulo 3. Note that there is no carry between digits (i. e. during this operation the digits aren't transferred). For example: 1410 tor 5010 = 01123 tor 12123 = 10213 = 3410.Petya wrote numbers a and c on a piece of paper. Help him find such number b, that a tor b = c. If there are several such numbers, print the smallest one.

Input: 14 34

Output: 50

Input: 50 34

Output: 14

Input: 387420489 225159023

Output: 1000000001

Input: 5 5

Output: 0

Input: 23476 23875625

Output: 23860906

Input: 11111 10101010

Output: 10116146

Input: 1 23865354

Output: 23865356

Input: 0 0

Output: 0

Input: 2376234 0

Output: 4732515

Input: 1 0

Output: 2

Input: 581130733 0

Output: 1162261466

Input: 581131733 1

Output: 1162260467

Input: 0 1000000000

Output: 1000000000

Input: 1000000000 0

Output: 693711461

Input: 1000000000 100000000

Output: 650219958

Input: 956747697 9487

Output: 736688812

Input: 229485033 8860

Output: 308580772

Input: 5341 813849430

Output: 813850920

Input: 227927516 956217829

Output: 872370713

Input: 390 380875228

Output: 380874919

Input: 336391083 911759145

Output: 1135529718

Input: 154618752 504073566

Output: 753527130

Input: 6436017 645491133

Output: 639142839

Input: 4232 755480607

Output: 755485882

Input: 19079106 69880743

Output: 56293527

Input: 460318555 440850074

Output: 25179124

Input: 227651149 379776728

Output: 168088492

Input: 621847819 8794

Output: 1114556841

Input: 827112516 566664600

Output: 908742057

Input: 460311350 820538776

Output: 404875070

Input: 276659168 241268656

Output: 358409486

Input: 9925 9952

Output: 27

Input: 830218526 438129941

Output: 784719357

Input: 630005197 848951646

Output: 754665575

Input: 123256190 174927955

Output: 243699845

Input: 937475611 769913258

Output: 994719535

Input: 561666539 29904379

Output: 1152454076

Input: 551731805 8515539

Output: 1049769112

Input: 6560 96330685

Output: 96330968

Input: 337894292 55

Output: 243175169

Input: 479225038 396637601

Output: 47143216

Input: 111174087 482024380

Output: 430083082

Input: 785233275 1523

Output: 393767834

Input: 47229813 6200

Output: 89081162

Input: 264662333 6952

Output: 141903557

Input: 523162963 922976263

Output: 414184806

Input: 6347 7416

Output: 10549

Input: 278014879 3453211

Output: 171855414

Input: 991084922 66

Output: 690049933

Input: 929361351 7373

Output: 679915097

Input: 532643581 213098335

Output: 842718489

Input: 69272798 718909239

Output: 668771236

Input: 440760623 316634331

Output: 1052493562

Input: 9001 9662

Output: 1390

Input: 417584836 896784933

Output: 481392203

Input: 640735701 335933492

Output: 992169746

Input: 5440 6647

Output: 10711

Input: 3545 6259

Output: 3536

Input: 847932562 1405

Output: 488901051

Input: 359103580 852

Output: 201115550

Input: 406369748 625641695

Output: 221459919

Input: 345157805 719310676

Output: 504894191

Input: 9150 823789822

Output: 823781437

Input: 8727 702561605

Output: 702556127

Input: 931392186 677650263

Output: 923604336

Input: 976954722 548418041

Output: 862925051

Input: 168971531 697371009

Output: 588009082

Input: 5849 7211

Output: 10146

Input: 934045591 4156

Output: 661009836

Input: 427471963 436868749

Output: 67345761

Input: 702754885 762686553

Output: 81198815

Input: 897312963 177161062

Output: 620860447

Input: 268520356 1999

Output: 135088146

Input: 635318406 289972012

Output: 950864476

Input: 237819544 904440360

Output: 857959352

Input: 44788825 4485

Output: 89397617

Input: 7376 994270908

Output: 994283218

Input: 893244884 654169485

Output: 1095395095

Input: 960725158 342144655

Output: 548529624

Input: 460645829 46697832

Output: 792961330

Input: 8389 172682371

Output: 172696203

Input: 294567098 631452590

Output: 745235571

Input: 5573 8790

Output: 13021

Input: 285938679 907528096

Output: 1068058915

Input: 774578699 101087409

Output: 940495066

Input: 153749013 598457896

Output: 444892699

Input: 364059865 346004232

Output: 40934348

Input: 237924125 573400957

Output: 507664538

Input: 987310001 827978268

Output: 275919178

Input: 922263603 387506683

Output: 1064907553

Input: 5712 384487208

Output: 384482225

Input: 9099 3208

Output: 14035

Input: 948688087 38251290

Output: 768385433

Input: 260153932 138945442

Output: 271056231

Input: 497129325 766959165

Output: 276817557

Input: 783390583 7679

Output: 399664540

Input: 657244587 28654748

Output: 921153434

Input: 455705795 757666961

Output: 303798597

Input: 815932189 211656771

Output: 562850021

Input: 511307975 307916669

Output: 1137612240

Input: 274842194 1000000000

Output: 1162261466