#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    char buf = '\0';
    unsigned long int result = 0;
    unsigned long int variants = 0;
    int symbols[100][26] = {{0}};
//    FILE *f = 0;
    int N = 0,M = 0,n=0,m=0,i=0;

//    f = fopen("input.txt","rt");

    scanf("%d %d\n",&N,&M);

    for (n=0;n<N;n++) {
        for (m=0;m<M;m++) {
            buf = getc(stdin);
            symbols[m][buf-'A'] = 1;
        }
        getc(stdin);
    }

    result = 1;
    for (m=0;m<M;m++) {
        variants = 0;
        for (i=0;i<26;i++) {
            variants += symbols[m][i];
        }
        result *= variants;
        result %= 1000000007;
    }
    printf("%li",result);

    return 0;
}

One day little Vasya found mom's pocket book. The book had n names of her friends and unusually enough, each name was exactly m letters long. Let's number the names from 1 to n in the order in which they are written.As mom wasn't home, Vasya decided to play with names: he chose three integers i, j, k (1 ≤ i < j ≤ n, 1 ≤ k ≤ m), then he took names number i and j and swapped their prefixes of length k. For example, if we take names "CBDAD" and "AABRD" and swap their prefixes with the length of 3, the result will be names "AABAD" and "CBDRD".You wonder how many different names Vasya can write instead of name number 1, if Vasya is allowed to perform any number of the described actions. As Vasya performs each action, he chooses numbers i, j, k independently from the previous moves and his choice is based entirely on his will. The sought number can be very large, so you should only find it modulo 1000000007 (109 + 7).

The first input line contains two integers n and m (1 ≤ n, m ≤ 100) — the number of names and the length of each name, correspondingly. Then n lines contain names, each name consists of exactly m uppercase Latin letters.

Print the single number — the number of different names that could end up in position number 1 in the pocket book after the applying the procedures described above. Print the number modulo 1000000007 (109 + 7).

NoteIn the first sample Vasya can get the following names in the position number 1: "AAB", "AAA", "BAA" and "BAB".

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    char buf = '\0';
    unsigned long long int result = 0;
    unsigned long long int variants = 0;
    int symbols[100][26] = {{0}};
//    FILE *f = 0;
    int N = 0,M = 0,n=0,m=0,i=0;

//    f = fopen("input.txt","rt");

    scanf("%d %d\n",&N,&M);

    for (n=0;n<N;n++) {
        for (m=0;m<M;m++) {
            buf = getc(stdin);
            symbols[m][buf-'A'] = 1;
        }
        getc(stdin);
    }

    result = 1;
    for (m=0;m<M;m++) {
        variants = 0;
        for (i=0;i<26;i++) {
            variants += symbols[m][i];
        }
        result *= variants;
        result %= 1000000007;
    }
    printf("%I64d",result);

    return 0;
}

[{'input': '2 3\r\nAAB\r\nBAA\r\n', 'output': ['4\r\n']}, {'input': '4 5\r\nABABA\r\nBCGDG\r\nAAAAA\r\nYABSA\r\n', 'output': ['216\r\n']}, {'input': '1 1\r\nE\r\n', 'output': ['1\r\n']}, {'input': '2 2\r\nNS\r\nPD\r\n', 'output': ['4\r\n']}, {'input': '3 4\r\nPJKD\r\nNFJX\r\nFGFK\r\n', 'output': ['81\r\n']}, {'input': '4 5\r\nSXFMY\r\nATHLM\r\nKDDQW\r\nZWGDS\r\n', 'output': ['1024\r\n']}, {'input': '20 14\r\nJNFKBBBJYZHWQE\r\nLBOKZCPFNKDBJY\r\nXKNWGHQHIOXUPF\r\nDDNRUKVUGHWMXW\r\nMTIZFNAAFEAPHX\r\nIXBQOOHEULZYHU\r\nMRCSREUEOOMUUN\r\nHJTSQWKUFYZDQU\r\nGMCMUZCOPRVEIQ\r\nXBKKGGJECOBLTH\r\nXXHTLXCNJZJUAF\r\nVLJRKXXXWMTPKZ\r\nPTYMNPTBBCWKAD\r\nQYJGOBUBHMEDYE\r\nGTKUUVVNKAHTUI\r\nZNKXYZPCYLBZFP\r\nQCBLJTRMBDWNNE\r\nTDOKJOBKEOVNLZ\r\nFKZUITYAFJOQIM\r\nUWQNSGLXEEIRWF\r\n', 'output': ['515139391\r\n']}, {'input': '5 14\r\nAQRXUQQNSKZPGC\r\nDTTKSPFGGVCLPT\r\nVLZQWWESCHDTAZ\r\nCOKOWDWDRUOMHP\r\nXDTRBIZTTCIDGS\r\n', 'output': ['124999979\r\n']}, {'input': '9 23\r\nOILBYKHRGMPENVFNHLSIUOW\r\nLPJFHTUQUINAALRDGLSQUXR\r\nLYYJJEBNZATAFQWTDZSPUNZ\r\nHSJPIQKKWWERJZIEMLCZUKI\r\nOJYIEYDGPFWRHCMISJCCUEM\r\nLMGKZVFYIVDRTIHBWPCNUTG\r\nUBGGNCITVHAIPKXCLTSAULQ\r\nOWSAWUOXQDBSXXBHTLSXUVD\r\nUGQTIZQPBGMASRQPVPSFUWK\r\n', 'output': ['454717784\r\n']}, {'input': '25 4\r\nLVKG\r\nMICU\r\nZHKW\r\nLFGG\r\nOWQO\r\nLCQG\r\nLVXU\r\nOUKB\r\nLNQX\r\nZJTO\r\nOOQX\r\nLVQP\r\nMFQB\r\nMRQV\r\nOIQH\r\nOPXX\r\nXFKU\r\nFCQB\r\nZPKH\r\nLVCH\r\nNFCU\r\nOVQW\r\nOZKU\r\nLFHX\r\nLPXO\r\n', 'output': ['5733\r\n']}, {'input': '30 10\r\nUTNTGOKZYJ\r\nQHOUHNYZVW\r\nLTVGHJRZVW\r\nMZHYHOLZYJ\r\nERYEUEPZYE\r\nUZDBFTURYJ\r\nRVSMQTIZGW\r\nWDJQHMIRYY\r\nKCORHQPZYE\r\nRRPLFOZZVY\r\nJTXMFNNNYJ\r\nMVTGGOZZVV\r\nEHAFFNUZVF\r\nLBRNWJZNYE\r\nJVMOHTPZYJ\r\nWTARFJLZVV\r\nLVJCWOURVW\r\nLCLQFJYRVV\r\nQVBVGNJRYF\r\nNTZGHOLRYE\r\nMGQKHOUPYJ\r\nRRSSBXPZYJ\r\nRYCRGTLZYJ\r\nJRDEGNKRVW\r\nRZKFGHYRVG\r\nMDJBFNIZYG\r\nMPLWHXIZYE\r\nSRZMHMURVE\r\nMTEBBMRZYJ\r\nJPJIFOLZYM\r\n', 'output': ['919913906\r\n']}, {'input': '40 7\r\nPNTVVER\r\nPAHTQDR\r\nRXMJVAS\r\nVIQNLYC\r\nILPUSVX\r\nYJOXQDJ\r\nSEFODTO\r\nOTJMREL\r\nLIQRZGD\r\nLBJJPOR\r\nRUTYHQO\r\nRIWEPBD\r\nKQUMFIB\r\nISTRRYH\r\nXBTOTGK\r\nRFQODEY\r\nHDSTZTP\r\nYCXFAGL\r\nAREGRFU\r\nLELZUYU\r\nGVABDKH\r\nFJAMMME\r\nACVULXE\r\nJHVPJAS\r\nAAQNMBX\r\nJJGUCXG\r\nOQATILQ\r\nNEOSHJM\r\nHFLWOFM\r\nICYEQHY\r\nFACGLYP\r\nPLLXJEQ\r\nDCHXYPB\r\nAGDDZJJ\r\nLSQRXTN\r\nHDQZXIY\r\nNAHDDWW\r\nQCMXRQN\r\nFDUDSZO\r\nHKBEVTW\r\n', 'output': ['206575993\r\n']}, {'input': '2 2\r\nAA\r\nBB\r\n', 'output': ['4\r\n']}, {'input': '1 10\r\nAAAAAAAAAA\r\n', 'output': ['1\r\n']}, {'input': '2 8\r\nAAAAAAAA\r\nBBBBBBBB\r\n', 'output': ['256\r\n']}, {'input': '10 10\r\nAAAAAAAAAA\r\nBBBBBBBBBB\r\nCCCCCCCCCC\r\nDDDDDDDDDD\r\nAAAAAAAAAA\r\nBBBBBBBBBB\r\nCCCCCCCCCC\r\nDDDDDDDDDD\r\nAAAAAAAAAA\r\nBBBBBBBBBB\r\n', 'output': ['1048576\r\n']}, {'input': '1 20\r\nAAAAAAAAAAAAAAAAAAAA\r\n', 'output': ['1\r\n']}, {'input': '20 1\r\nA\r\nB\r\nC\r\nD\r\nE\r\nF\r\nG\r\nA\r\nB\r\nC\r\nD\r\nE\r\nF\r\nG\r\nA\r\nB\r\nC\r\nD\r\nE\r\nF\r\n', 'output': ['7\r\n']}, {'input': '5 60\r\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\r\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\r\nCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC\r\nDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD\r\nEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE\r\n', 'output': ['449874206\r\n']}, {'input': '50 4\r\nAAAA\r\nBBBB\r\nCCCC\r\nDDDD\r\nEEEE\r\nFFFF\r\nGGGG\r\nHHHH\r\nIIII\r\nJJJJ\r\nAAAA\r\nBBBB\r\nCCCC\r\nDDDD\r\nEEEE\r\nFFFF\r\nGGGG\r\nHHHH\r\nIIII\r\nJJJJ\r\nAAAA\r\nBBBB\r\nCCCC\r\nDDDD\r\nEEEE\r\nFFFF\r\nGGGG\r\nHHHH\r\nIIII\r\nJJJJ\r\nAAAA\r\nBBBB\r\nCCCC\r\nDDDD\r\nEEEE\r\nFFFF\r\nGGGG\r\nHHHH\r\nIIII\r\nJJJJ\r\nAAAA\r\nBBBB\r\nCCCC\r\nDDDD\r\nEEEE\r\nFFFF\r\nGGGG\r\nHHHH\r\nIIII\r\nJJJJ\r\n', 'output': ['10000\r\n']}, {'input': '1 100\r\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\r\n', 'output': ['1\r\n']}, {'input': '100 1\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\nA\r\n', 'output': ['1\r\n']}, {'input': '100 1\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\nA\r\nB\r\n', 'output': ['2\r\n']}, {'input': '100 1\r\nA\r\nB\r\nC\r\nD\r\nE\r\nF\r\nG\r\nH\r\nI\r\nJ\r\nK\r\nL\r\nM\r\nN\r\nA\r\nB\r\nC\r\nD\r\nE\r\nF\r\nG\r\nH\r\nI\r\nJ\r\nK\r\nL\r\nM\r\nN\r\nA\r\nB\r\nC\r\nD\r\nE\r\nF\r\nG\r\nH\r\nI\r\nJ\r\nK\r\nL\r\nM\r\nN\r\nA\r\nB\r\nC\r\nD\r\nE\r\nF\r\nG\r\nH\r\nI\r\nJ\r\nK\r\nL\r\nM\r\nN\r\nA\r\nB\r\nC\r\nD\r\nE\r\nF\r\nG\r\nH\r\nI\r\nJ\r\nK\r\nL\r\nM\r\nN\r\nA\r\nB\r\nC\r\nD\r\nE\r\nF\r\nG\r\nH\r\nI\r\nJ\r\nK\r\nL\r\nM\r\nN\r\nA\r\nB\r\nC\r\nD\r\nE\r\nF\r\nG\r\nH\r\nI\r\nJ\r\nK\r\nL\r\nM\r\nN\r\nA\r\nB\r\n', 'output': ['14\r\n']}, {'input': '100 1\r\nA\r\nB\r\nC\r\nD\r\nE\r\nF\r\nG\r\nH\r\nI\r\nJ\r\nK\r\nL\r\nM\r\nN\r\nO\r\nP\r\nQ\r\nR\r\nS\r\nT\r\nU\r\nV\r\nW\r\nX\r\nY\r\nZ\r\nA\r\nB\r\nC\r\nD\r\nE\r\nF\r\nG\r\nH\r\nI\r\nJ\r\nK\r\nL\r\nM\r\nN\r\nO\r\nP\r\nQ\r\nR\r\nS\r\nT\r\nU\r\nV\r\nW\r\nX\r\nY\r\nZ\r\nA\r\nB\r\nC\r\nD\r\nE\r\nF\r\nG\r\nH\r\nI\r\nJ\r\nK\r\nL\r\nM\r\nN\r\nO\r\nP\r\nQ\r\nR\r\nS\r\nT\r\nU\r\nV\r\nW\r\nX\r\nY\r\nZ\r\nA\r\nB\r\nC\r\nD\r\nE\r\nF\r\nG\r\nH\r\nI\r\nJ\r\nK\r\nL\r\nM\r\nN\r\nO\r\nP\r\nQ\r\nR\r\nS\r\nT\r\nU\r\nV\r\n', 'output': ['26\r\n']}]


#include <stdio.h>
#include <string.h>

int main(){
	char s[1000], t[1000];
	scanf ("%[^\n]", s);
	getchar();

	scanf ("%[^\n]", t);
	getchar();

	int len = strlen(s);
	int les = strlen(t);

	int ts[200] = {};

	for (int i=0; i<len; i++){
		ts[s[i]]++;
	}

	bool found = true;
	for (int i=0; i<les && found; i++){
		if (t[i] != ' '){
			if (ts[t[i]] > 0) ts[t[i]]--;
			else found = false;
		}
	}

	printf ("%s", (found) ? "YES", "NO");
}

Vasya decided to write an anonymous letter cutting the letters out of a newspaper heading. He knows heading s1 and text s2 that he wants to send. Vasya can use every single heading letter no more than once. Vasya doesn't have to cut the spaces out of the heading — he just leaves some blank space to mark them. Help him; find out if he will manage to compose the needed text.

The first line contains a newspaper heading s1. The second line contains the letter text s2. s1 и s2 are non-empty lines consisting of spaces, uppercase and lowercase Latin letters, whose lengths do not exceed 200 symbols. The uppercase and lowercase letters should be differentiated. Vasya does not cut spaces out of the heading.

If Vasya can write the given anonymous letter, print YES, otherwise print NO

Instead of dogging Your footsteps it disappears but you dont notice anything
where is your dog

Instead of dogging Your footsteps it disappears but you dont notice anything
Your dog is upstears

Instead of dogging your footsteps it disappears but you dont notice anything
Your dog is upstears

[{'input': 'Instead of dogging Your footsteps it disappears but you dont notice anything\r\nwhere is your dog\r\n', 'output': ['NO\r\n']}, {'input': 'Instead of dogging Your footsteps it disappears but you dont notice anything\r\nYour dog is upstears\r\n', 'output': ['YES\r\n']}, {'input': 'Instead of dogging your footsteps it disappears but you dont notice anything\r\nYour dog is upstears\r\n', 'output': ['NO\r\n']}, {'input': 'abcdefg hijk\r\nk j i h g f e d c b a\r\n', 'output': ['YES\r\n']}, {'input': 'HpOKgo\r\neAtAVB\r\n', 'output': ['NO\r\n']}, {'input': 'GRZGc\r\nLPzD\r\n', 'output': ['NO\r\n']}, {'input': 'GtPXu\r\nd\r\n', 'output': ['NO\r\n']}, {'input': 'FVF\r\nr \r\n', 'output': ['NO\r\n']}, {'input': 'HpOKgo\r\nogK\r\n', 'output': ['YES\r\n']}, {'input': 'GRZGc\r\nZG\r\n', 'output': ['YES\r\n']}, {'input': 'HpOKgoueAtAVBdGffvQheJDejNDHhhwyKJisugiRAH OseK yUwqPPNuThUxTfthqIUeb wS jChGOdFDarNrKRT  MlwKecxWNoKEeD BbiHAruE XMlvKYVsJGPP\r\nAHN  XvoaNwV  AVBKwKjr u      U K wKE D K   Jy KiHsR h d W  Js IHyMPK Br iSqe  E fDA   g H\r\n', 'output': ['YES\r\n']}, {'input': 'GRZGcsLPzDrCSXhhNTaibJqVphhjbcPoZhCDUlzAbDnRWjHvxLKtpGiFWiGbfeDxBwCrdJmJGCGv GebAOinUsFrlqKTILOmxrFjSpEoVGoTdSSstJWVgMLKMPettxHASaQZNdOIObcTxtF qTHWBdNIKwj\r\nWqrxze Ji x q aT GllLrRV jMpGiMDTwwS JDsPGpAZKACmsFCOS CD Sj  bCDgKF  jJxa RddtLFAi VGLHH SecObzG q  hPF \r\n', 'output': ['YES\r\n']}, {'input': 'GtPXuwdAxNhODQbjRslDDKciOALJrCifTjDQurQEBeFUUSZWwCZQPdYwZkYbrduMijFjgodAOrKIuUKwSXageZuOWMIhAMexyLRzFuzuXqBDTEaWMzVdbzhxDGSJC SsIYuYILwpiwwcObEHWpFvHeBkWYNitqYrxqgHReHcKnHbtjcWZuaxPBVPb\r\nTQIKyqFaewOkY lZUOOuxEw EwuKcArxRQGFYkvVWIAe SuanPeHuDjquurJu aSxwgOSw jYMwjxItNUUArQjO BIujAhSwttLWp\r\n', 'output': ['YES\r\n']}, {'input': 'FVFSr unvtXbpKWF vPaAgNaoTqklzVqiGYcUcBIcattzBrRuNSnKUtmdGKbjcE\r\nUzrU K  an GFGR        Wc zt iBa     P c    T K v p     V In   b           B   c\r\n', 'output': ['YES\r\n']}, {'input': 'lSwjnYLYtDNIZjxHiTawdh ntSzggZogcIZTuiTMWVgwyloMtEhqkrOxgIcFvwvsboXUPILPIymFAEXnhApewJXJNtFyZ\r\nAoxe  jWZ u  yImg  o AZ FNI w lpj  tNhT g y  ZYcb rc J    w Dlv\r\n', 'output': ['YES\r\n']}, {'input': 'kvlekcdJqODUKdsJlXkRaileTmdGwUHWWgvgUokQxRzzbpFnswvNKiDnjfOFGvFcnaaiRnBGQmqoPxDHepgYasLhzjDgmvaFfVNEcSPVQCJKAbSyTGpXsAjIHr\r\nGjzUllNaGGKXUdYmDFpqFAKIwvTpjmqnyswWRTnxlBnavAGvavxJemrjvRJc\r\n', 'output': ['YES\r\n']}, {'input': 'kWbvhgvvoYOhwXmgTwOSCDXrtFHhqwvMlCvsuuAUXMmWaYXiqHplFZZemhgkTuvsUtIaUxtyYauBIpjdbyYxjZ ZkaBPzwqPfqF kCqGRmXvWuabnQognnkvdNDtRUsSUvSzgBuxCMBWJifbxWegsknp\r\nBsH   bWHJD n Ca T xq  PRCv  tatn Wjy sm  I q s WCjFqdWe  t W XUs  Do  eb Pfh ii  hTbF  O Fll\r\n', 'output': ['YES\r\n']}, {'input': 'OTmLdkMhmDEOMQMiW ZpzEIjyElHFrNCfFQDp SZyoZaEIUIpyCHfwOUqiSkKtFHggrTBGkqfOxkChPztmPrsHoxVwAdrxbZLKxPXHlMnrkgMgiaHFopiFFiUEtKwCjpJtwdwkbJCgA bxeDIscFdmHQJLAMNhWlrZisQrHQpvbALWTwpf jnx\r\nDbZwrQbydCdkJMCrftiwtPFfpMiwwrfIrKidEChKECxQUBVUEfFirbGWiLkFQkdJiFtkrtkbIAEXCEDkwLpK\r\n', 'output': ['YES\r\n']}, {'input': 'NwcGaIeSkOva\r\naIa\r\n', 'output': ['YES\r\n']}, {'input': 'gSrAcVYgAdbdayzbKGhIzLDjyznLRIJH KyvilAaEddmgkBPCNzpmPNeGEbmmpAyHvUSoPvnaORrPUuafpReEGoDOQsAYnUHYfBqhdcopQfxJuGXgKnbdVMQNhJYkyjiJDKlShqBTtnnDQQzEijOMcYRGMgPGVhfIReYennKBLwDTVvcHMIHMgVpJkvzTrezxqS\r\nHJerIVvRyfrPgAQMTI AqGNO mQDfDwQHKgeeYmuRmozKHILvehMPOJNMRtPTAfvKvsoGKi xHEeKqDAYmQJPUXRJbIbHrgVOMGMTdvYiLui\r\n', 'output': ['YES\r\n']}, {'input': 'ReB hksbHqQXxUgpvoNK bFqmNVCEiOyKdKcAJQRkpeohpfuqZabvrLfmpZOMcfyFBJGZwVMxiUPP pbZZtJjxhEwvrAba\r\nJTCpQnIViIGIdQtLnmkVzmcbBZR CoxAdTtWSYpbOglDFifqIVQ vfGKGtLpxpJHiHSWCMeRcrVOXBGBhoEnVhNTPWGTOErNtSvokcGdgZXbgTEtISUyTwaXUEIlJMmutsdCbiyrPZPJyRdOjnSuAGttLy\r\n', 'output': ['NO\r\n']}, {'input': 'hrLzRegCuDGxTrhDgVvM KowwyYuXGzIpcXdSMgeQVfVOtJZdkhNYSegwFWWoPqcZoeapbQnyCtojgkcyezUNHGGIZrhzsKrvvcrtokIdcnqXXkCNKjrOjrnEAKBNxyDdiMVeyLvXxUYMZQRFdlcdlcxzKTeYzBlmpNiwWbNAAhWkMoGpRxkCuyqkzXdKWwGH\r\nJESKDOfnFdxPvUOCkrgSBEPQHJtJHzuNGstRbTCcchRWJvCcveSEAtwtOmZZiW\r\n', 'output': ['NO\r\n']}, {'input': 'yDBxCtUygQwWqONxQCcuAvVCkMGlqgC zvkfEkwqbhMCQxnkwQIUhucCbVUyOBUcXvTNEGriTBwMDMfdsPZgWRgIUDqM\r\neptVnORTTyixxmWIBpSTEwOXqGZllBgSxPenYCDlFwckJlWsoVwWLAIbPOmFqcKcTcoQqahetl KLfVSyaLVebzsGwPSVbtQAeUdZAaJtfxlCEvvaRhLlVvRJhKat IaB awdqcDlrrhTbRxjEbzGwcdmdavkhcjHjzmwbxAgw\r\n', 'output': ['NO\r\n']}, {'input': 'jlMwnnotSdlQMluKWkJwAeCetcqbIEnKeNyLWoKCGONDRBQOjbkGpUvDlmSFUJ bWhohqmmIUWTlDsvelUArAcZJBipMDwUvRfBsYzMdQnPDPAuBaeJmAxVKwUMJrwMDxNtlrtAowVWqWiwFGtmquZAcrpFsLHCrvMSMMlvQUqypAihQWrFMNoaqfs IBg\r\nNzeWQ bafrmDsYlpNHSGTBBgPl WIcuNhyNaNOEFvL\r\n', 'output': ['NO\r\n']}, {'input': 'zyWvXBcUZqGqjHwZHQryBtFliLYnweXAoMKNpLaunaOlzaauWmLtywsEvWPiwxJapocAFRMjrqWJXYqfKEbBKnzLO\r\npsbi bsXpSeJaCkIuPWfSRADXdIClxcDCowwJzGCDTyAl\r\n', 'output': ['NO\r\n']}, {'input': 'kKhuIwRPLCwPFfcnsyCfBdnsraGeOCcLTfXuGjqFSGPSAeDZJSS bXKFanNqWjpFnvRpWxHJspvisDlADJBioxXNbVoXeUedoPcNEpUyEeYxdJXhGzFAmpAiHotSVwbZQsuWjIVhVaEGgqbZHIoDpiEmjTtFylCwCkWWzUOoUfOHxEZvDwNpXhBWamHn\r\nK VpJjGhNbwCRhcfmNGVjewBFpEmPlIKeTuWiukDtEWpjgqciqglkyNfWrBLbGAKvlNWxaUelJmSlSoakSpRzePvJsshOsTYrMPXdxKpaShjyVIXGhRIAdtiGpNwtiRmGTBZhkJqIMdxMHX RMxCMYcWjcjhtCHyFnCvjjezGbkRDRiVxkbh\r\n', 'output': ['NO\r\n']}, {'input': 'AXssNpFKyQmJcBdBdfkhhMUzfqJVgcLBddkwtnFSzSRUCjiDcdtmkzIGkCKSxWUEGhmHmciktJyGMkgCductyHx\r\nI nYhmJfPnvoKUiXYUBIPIcxNYTtvwPUoXERZvY ahlDpQFNMmVZqEBiYqYlHNqcpSCmhFczBlOAhsYFeqMGfqL EJsDNOgwoJfBzqijKOFcYQ\r\n', 'output': ['NO\r\n']}, {'input': 'lkhrzDZmkdbjzYKPNMRkiwCFoZsMzBQMnxxdKKVJezSBjnLjPpUYtabcPTIaDJeDEobbWHdKOdVfMQwDXzDDcSrwVenDEYpMqfiOQ xSsqApWnAMoyhQXCKFzHvvzvUvkWwmwZrvZz\r\nsUzGspYpRFsHRbRgTQuCBgnFgPkisTUfFNwyEEWWRiweWWgjRkVQxgTwxOzdsOwfrGIH O gCXpzvHzfItuEHaihmugEyymSJIogYwX qAwcwIItidfnzZDhZgQHi eRjMAeVkJHceDZuJkmxGowOsmcGYYvk Ajtgi TxwihvjLViNZjvscTWvsaQUelTSivLShhEl\r\n', 'output': ['NO\r\n']}, {'input': 'BRsVjyNhrqRHVwrJzuzRigEhdpbDmaACSPfed\r\nlWqKTjlrqOCUbgBBZdZDGCeQJDXawPnnDkQdZDgwrEQk\r\n', 'output': ['NO\r\n']}, {'input': 'KRmINuyBYPwiTsdlyiNVuylToysJKmOpcLovAtwGPqrgFJQNAYvuAiyQRkeFMECVZvkDEmTauXlyjAaYRnTJXORMZRnTakBaUzSelMilejySDIZjQjzcOIrwXdvDvpeRIkoBgreyFXIyyIZutjiEBtwrmzQtPVUhvvdEtDMbXjBpoPVjGdM EXTAK JbCnw\r\nXZZqlJvzKKtvdNlzFPDTYxidqlsgufVzyEmO FZuLQ vVQsJESNviUCovCK NwwlbxsmPtOJNmAonCqrOZ bZ LVKAsQGmoLnYjeekvEIECFk\r\n', 'output': ['NO\r\n']}]


#include<stdio.h>
#include<string.h>
int main()
{
	char s[1001];
	int i,k,count=0,j;
	 scanf("%s",&s);
	 scanf("%d",&k);
	if(strlen(s) <k)
	printf("impossible");
	else
    {
      for(i=0;i<=strlen(s);i++)
            for(j=i+1;j<=strlen(s);j++)
             if(s[i]==s[j])
                count++;

      if(i== strlen(s))
        printf("impossible");
        else
        printf("%d",count);

    }
}

Calculate the minimum number of characters you need to change in the string s, so that it contains at least k different letters, or print that it is impossible.String s consists only of lowercase Latin letters, and it is allowed to change characters only to lowercase Latin letters too.

First line of input contains string s, consisting only of lowercase Latin letters (1 ≤ |s| ≤ 1000, |s| denotes the length of s). Second line of input contains integer k (1 ≤ k ≤ 26).

Print single line with a minimum number of necessary changes, or the word «impossible» (without quotes) if it is impossible.

NoteIn the first test case string contains 6 different letters, so we don't need to change anything.In the second test case string contains 4 different letters: {'a', 'h', 'o', 'y'}. To get 5 different letters it is necessary to change one occurrence of 'o' to some letter, which doesn't occur in the string, for example, {'b'}.In the third test case, it is impossible to make 7 different letters because the length of the string is 6.

#include<stdio.h>
#include<string.h>
int main()
{
	char s[1001];
	int i,k,count,j,freq[1001],count1;
	 scanf("%s",&s);
	 scanf("%d",&k);

	if(strlen(s) <k)
	printf("impossible");
	else
    {for(i=0;i<strlen(s);i++)
        freq[i]=-1;

     for(i=0; i<strlen(s); i++)
    {
        count = 1;
        for(j=i+1; j<strlen(s); j++)
        {
            if(s[i]==s[j])
            {
                count++;
                freq[j] = 0;
            }
        }

        if(freq[i] != 0)
        {
            freq[i] = count;
        }
    }

    int d,t;
        for(i=0;i<strlen(s);i++)
        {

             d = i;
        while ( d > 0 && freq[d] < freq[d-1]) {
                        t= freq[d];
                    freq[d]= freq[d-1];
                    freq[d-1] = t;
                d--;
                }

        }
        int size,freq2[1001],n=0;
        //size = sizeof(freq)/sizeof(int);
        for(i=0;i<strlen(s);i++)
            if(freq[i]!=0)
        {
            freq2[n]= freq[i];
            n++;

        }

        if( n<k)
           printf("%d",k-n);
        else
            printf("0");

    }
}

[{'input': 'yandex\r\n6\r\n', 'output': ['0\r\n']}, {'input': 'yahoo\r\n5\r\n', 'output': ['1\r\n']}, {'input': 'google\r\n7\r\n', 'output': ['impossible\r\n']}, {'input': 'a\r\n1\r\n', 'output': ['0\r\n']}, {'input': 'z\r\n2\r\n', 'output': ['impossible\r\n']}, {'input': 'fwgfrwgkuwghfiruhewgirueguhergiqrbvgrgf\r\n26\r\n', 'output': ['14\r\n']}, {'input': 'nfevghreuoghrueighoqghbnebvnejbvnbgneluqe\r\n26\r\n', 'output': ['12\r\n']}, {'input': 'a\r\n3\r\n', 'output': ['impossible\r\n']}, {'input': 'smaxpqplaqqbxuqxalqmbmmgubbpspxhawbxsuqhhegpmmpebqmqpbbeplwaepxmsahuepuhuhwxeqmmlgqubuaxehwuwasgxpqmugbmuawuhwqlswllssueglbxepbmwgs\r\n1\r\n', 'output': ['0\r\n']}, {'input': 'cuguccgcugcugucgggggcgcgucgucugcuuuccccuugccg\r\n4\r\n', 'output': ['1\r\n']}, {'input': 'fcfccfcfccfcfcffcffffffcfccfccfcffccccfcffffccfccfcffcfcccccffcfffcccffcfccfffffcccfccffffffccfccccf\r\n20\r\n', 'output': ['18\r\n']}, {'input': 'swmkwaruyv\r\n5\r\n', 'output': ['0\r\n']}, {'input': 'tnbqpsuhkczmejirvyfdolxwga\r\n22\r\n', 'output': ['0\r\n']}, {'input': 'abcde\r\n3\r\n', 'output': ['0\r\n']}, {'input': 'abb\r\n1\r\n', 'output': ['0\r\n']}, {'input': 'aaaa\r\n1\r\n', 'output': ['0\r\n']}, {'input': 'abcde\r\n2\r\n', 'output': ['0\r\n']}, {'input': 'yandex\r\n4\r\n', 'output': ['0\r\n']}, {'input': 'aaabbbccc\r\n1\r\n', 'output': ['0\r\n']}, {'input': 'abcd\r\n2\r\n', 'output': ['0\r\n']}, {'input': 'asdfgh\r\n2\r\n', 'output': ['0\r\n']}, {'input': 'aab\r\n1\r\n', 'output': ['0\r\n']}, {'input': 'mynameissako\r\n5\r\n', 'output': ['0\r\n']}, {'input': 'abcde\r\n1\r\n', 'output': ['0\r\n']}, {'input': 'abcd\r\n3\r\n', 'output': ['0\r\n']}, {'input': 'abcdef\r\n2\r\n', 'output': ['0\r\n']}, {'input': 'abcdefg\r\n4\r\n', 'output': ['0\r\n']}, {'input': 'abc\r\n1\r\n', 'output': ['0\r\n']}, {'input': 'asdafjsgljdllgjdgkl\r\n5\r\n', 'output': ['0\r\n']}, {'input': 'yaay\r\n3\r\n', 'output': ['1\r\n']}, {'input': 'yaay\r\n4\r\n', 'output': ['2\r\n']}, {'input': 'zzzzzz\r\n2\r\n', 'output': ['1\r\n']}]


"Hey, it's homework time" — thought Polycarpus and of course he started with his favourite subject, IT. Polycarpus managed to solve all tasks but for the last one in 20 minutes. However, as he failed to solve the last task after some considerable time, the boy asked you to help him.The sequence of n integers is called a permutation if it contains all integers from 1 to n exactly once.You are given an arbitrary sequence a1, a2, ..., an containing n integers. Each integer is not less than 1 and not greater than 5000. Determine what minimum number of elements Polycarpus needs to change to get a permutation (he should not delete or add numbers). In a single change he can modify any single sequence element (i. e. replace it with another integer).

The first line of the input data contains an integer n (1 ≤ n ≤ 5000) which represents how many numbers are in the sequence. The second line contains a sequence of integers ai (1 ≤ ai ≤ 5000, 1 ≤ i ≤ n).

Print the only number — the minimum number of changes needed to get the permutation.

NoteThe first sample contains the permutation, which is why no replacements are required.In the second sample it is enough to replace the first element with the number 1 and that will make the sequence the needed permutation.In the third sample we can replace the second element with number 4 and the fourth element with number 2.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char v[100000], a1[100], a2[100], nv[100000];
    scanf("%s", &v);
    strcpy(nv, v);
    strrev(nv);
    scanf("%s", &a1);
    scanf("%s", &a2);
    if(strstr(v, a1) != NULL && strstr(v, a2) != NULL && strlen(v)>=(strlen(a1)+strlen(a2))){
        if(strstr(v, a1)>=strstr(v,a2) && strstr(nv, a1)>=strstr(nv, a2))
            printf("both");
        else if(strstr(v, a1)>strstr(v,a2))
            printf("backward");
        else if(strstr(v, a1)<strstr(v,a2))
            printf("forward");
        else printf("fantasy");
    }
    else printf("fantasy");
    return 0;
}

Peter likes to travel by train. He likes it so much that on the train he falls asleep. Once in summer Peter was going by train from city A to city B, and as usual, was sleeping. Then he woke up, started to look through the window and noticed that every railway station has a flag of a particular colour.The boy started to memorize the order of the flags' colours that he had seen. But soon he fell asleep again. Unfortunately, he didn't sleep long, he woke up and went on memorizing the colours. Then he fell asleep again, and that time he slept till the end of the journey.At the station he told his parents about what he was doing, and wrote two sequences of the colours that he had seen before and after his sleep, respectively.Peter's parents know that their son likes to fantasize. They give you the list of the flags' colours at the stations that the train passes sequentially on the way from A to B, and ask you to find out if Peter could see those sequences on the way from A to B, or from B to A. Remember, please, that Peter had two periods of wakefulness.Peter's parents put lowercase Latin letters for colours. The same letter stands for the same colour, different letters — for different colours.

The input data contains three lines. The first line contains a non-empty string, whose length does not exceed 105, the string consists of lowercase Latin letters — the flags' colours at the stations on the way from A to B. On the way from B to A the train passes the same stations, but in reverse order.  The second line contains the sequence, written by Peter during the first period of wakefulness. The third line contains the sequence, written during the second period of wakefulness. Both sequences are non-empty, consist of lowercase Latin letters, and the length of each does not exceed 100 letters. Each of the sequences is written in chronological order.

Output one of the four words without inverted commas:    «forward» — if Peter could see such sequences only on the way from A to B;  «backward» — if Peter could see such sequences on the way from B to A;  «both» — if Peter could see such sequences both on the way from A to B, and on the way from B to A;  «fantasy» — if Peter could not see such sequences.

NoteIt is assumed that the train moves all the time, so one flag cannot be seen twice. There are no flags at stations A and B.

#include<stdio.h>
#include <string.h>

int main(){
    char sequance[100003];
   scanf("%s", sequance);
   char a[103];
   char b[103];
   int n1=0;
   int n2=0;
   int n3=0;
   int n4=0;
   scanf("%s", a);
   scanf("%s", b);
   int current=0;
   int current2=0;
   int lenseq=strlen(sequance);
   int lena=strlen(a);
   int lenb=strlen(b);
//   if (sequance[1000]=='a'){

//       printf("%d lenseq\n", lenseq);
//       printf("%d lena\n", lena);
//       printf("%d lenb\n", lenb);
//   }
   for (int j=0;j<=lenseq-lena;j++){
       if (sequance[j]==a[0]) {
           n1=1;
           for (int i=1;i<lena;i++){
               if(sequance[j+i]==a[i]){
                   n1++;
               } else break;
           }
       }
       if (n1==lena) {current= j+lena;break;}
   }

   for (int j=current;j<=lenseq-lenb;j++){
       if (sequance[j]==b[0]) {
           n2=1;
           for (int i=1;i<lenb;i++){
               if(sequance[j+i]==b[i]){
                   n2++;
               } else break;
           }
       }
        if (n2==lenb) {break;}
   }

   for (int j=lenseq-1;j>=lena-1; j--){
       if (sequance[j]==a[0]) {
          n3=1;
           for (int i=1;i<lena;i++){

               if(sequance[j-i]==a[i]){
                   n3++;
               } else break;
           }
       }
       if (n3==lena) {current2=j-lena;break;}
   }

   for (int j=current2;j>=0; j--){
       if (sequance[j]==b[0]) {
           n4=1;
           for (int i=1;i<lenb;i++){
               if(sequance[j-i]==b[i]){
                   n4 ++;

               } else break;
           }
       }
       if (n4==lenb) {break;}
   }

   if (((n1!=lena || n2!=lenb) && (n3!=lena ||n4!=lenb ) )|| lena+lenb>lenseq) {
       printf("fantasy");
   }
   else if ( (n1==lena && n2==lenb) && (n3!=lena || n4!=lenb )) {
        printf("forward");
   }
   else if ( (n1!=lena || n2!=lenb) && (n3==lena && n4==lenb )) {
        printf("backward");
   }
   else if ( (n1==lena || n2==lenb) && (n3==lena && n4==lenb )) {
        printf("both");
   }
}

[{'input': 'atob\r\na\r\nb\r\n', 'output': ['forward\r\n']}, {'input': 'aaacaaa\r\naca\r\naa\r\n', 'output': ['both\r\n']}, {'input': 'aaa\r\naa\r\naa\r\n', 'output': ['fantasy\r\n']}, {'input': 'astalavista\r\nastla\r\nlavista\r\n', 'output': ['fantasy\r\n']}, {'input': 'abacabadabacaba\r\nabacaba\r\nabacaba\r\n', 'output': ['both\r\n']}, {'input': 'a\r\na\r\na\r\n', 'output': ['fantasy\r\n']}, {'input': 'ab\r\nb\r\na\r\n', 'output': ['backward\r\n']}, {'input': 'aaa\r\naaaa\r\naaaa\r\n', 'output': ['fantasy\r\n']}, {'input': 'bbabbbbababbaabaabaa\r\nabb\r\nbaab\r\n', 'output': ['forward\r\n']}, {'input': 'bbbbbbbbbbbbbbbbbbbbbbbbb\r\nbbbb\r\nbbbbb\r\n', 'output': ['both\r\n']}, {'input': 'babaabababaaaababaabababaabababababababbababbbabbaabababaababbaabbababaababaaabababaabbaababaaababaa\r\nabaabababaa\r\nabaabbaa\r\n', 'output': ['forward\r\n']}, {'input': 'bbbbbbbbbbbbbbbbbbbbbbbbb\r\nbbbb\r\nbbbbb\r\n', 'output': ['both\r\n']}, {'input': 'aababaaababaabbaabababaaababaabababbaabbabaabababaabbabbbababbababababababaabababaababaaaabababaabab\r\nabaabababaa\r\nabaabbaa\r\n', 'output': ['backward\r\n']}, {'input': 'aaaa\r\naaa\r\naa\r\n', 'output': ['fantasy\r\n']}, {'input': 'zzzz\r\nzz\r\nzzz\r\n', 'output': ['fantasy\r\n']}, {'input': 'zzzz\r\nzzzz\r\nzzzz\r\n', 'output': ['fantasy\r\n']}, {'input': 'zzzz\r\nzz\r\nzz\r\n', 'output': ['both\r\n']}, {'input': 'aabaa\r\naab\r\nbaa\r\n', 'output': ['fantasy\r\n']}, {'input': 'aabaab\r\naba\r\nab\r\n', 'output': ['forward\r\n']}, {'input': 'aab\r\nb\r\naa\r\n', 'output': ['backward\r\n']}, {'input': 'abacaba\r\naca\r\nba\r\n', 'output': ['both\r\n']}]


#include <stdio.h>
int main ()
{
    int n,i,j,num[2000],a[2000],min=2000,x,z;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&num[i]);
        if(num[i]<min)
            {
                min=num[i];
                z=i;
            }
    }
    if(z==1 || z==n)
    {
        printf("%d\n",min);
        return 0;
    }
    //printf("%d\n",min);
    min=2000,i=1;
    while(i<n)
    {
        if(num[i+1]>num[i+2])
        {
            if(min>num[i+1])
            {
                min=num[i+1];
            }
            i=i+1;
        }
        else{

            if(min>num[i+2])
            {
                min=num[i+2];
            }
            i=i+2;
        }
    }

    printf("%d\n",min);
    return 0;
}

In Berland the opposition is going to arrange mass walking on the boulevard. The boulevard consists of n tiles that are lain in a row and are numbered from 1 to n from right to left. The opposition should start walking on the tile number 1 and the finish on the tile number n. During the walk it is allowed to move from right to left between adjacent tiles in a row, and jump over a tile. More formally, if you are standing on the tile number i (i < n - 1), you can reach the tiles number i + 1 or the tile number i + 2 from it (if you stand on the tile number n - 1, you can only reach tile number n). We can assume that all the opposition movements occur instantaneously.In order to thwart an opposition rally, the Berland bloody regime organized the rain. The tiles on the boulevard are of poor quality and they are rapidly destroyed in the rain. We know that the i-th tile is destroyed after ai days of rain (on day ai tile isn't destroyed yet, and on day ai + 1 it is already destroyed). Of course, no one is allowed to walk on the destroyed tiles! So the walk of the opposition is considered thwarted, if either the tile number 1 is broken, or the tile number n is broken, or it is impossible to reach the tile number n from the tile number 1 if we can walk on undestroyed tiles.The opposition wants to gather more supporters for their walk. Therefore, the more time they have to pack, the better. Help the opposition to calculate how much time they still have and tell us for how many days the walk from the tile number 1 to the tile number n will be possible.

The first line contains integer n (1 ≤ n ≤ 103) — the boulevard's length in tiles. The second line contains n space-separated integers ai — the number of days after which the i-th tile gets destroyed (1 ≤ ai ≤ 103).

Print a single number — the sought number of days.

NoteIn the first sample the second tile gets destroyed after day three, and the only path left is 1 → 3 → 4. After day five there is a two-tile gap between the first and the last tile, you can't jump over it.In the second sample path 1 → 3 → 5 is available up to day five, inclusive. On day six the last tile is destroyed and the walk is thwarted.

#include<stdio.h>
#include<math.h>
int max(int a, int b)
{
	if(a>b)
	return a;
	else
	return b;

}
int main()
{
	int n,a[10004],i,min,max1;
	min=1000000;
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}

	for(i=1;i<=n;i++)
	{
		max1=max(a[i],a[i-1]);
		if(max1<min)
		min=max1;
	}
	if(min>a[0])
	min=a[0];
	if(min>a[n-1])
	min=a[n-1];
	printf("%d",min);
}


Anna and Maria are in charge of the math club for junior students. When the club gathers together, the students behave badly. They've brought lots of shoe laces to the club and got tied with each other. Specifically, each string ties together two students. Besides, if two students are tied, then the lace connects the first student with the second one as well as the second student with the first one.To restore order, Anna and Maria do the following. First, for each student Anna finds out what other students he is tied to. If a student is tied to exactly one other student, Anna reprimands him. Then Maria gathers in a single group all the students who have been just reprimanded. She kicks them out from the club. This group of students immediately leaves the club. These students takes with them the laces that used to tie them. Then again for every student Anna finds out how many other students he is tied to and so on. And they do so until Anna can reprimand at least one student.Determine how many groups of students will be kicked out of the club.

The first line contains two integers n and m — the initial number of students and laces (). The students are numbered from 1 to n, and the laces are numbered from 1 to m. Next m lines each contain two integers a and b — the numbers of students tied by the i-th lace (1 ≤ a, b ≤ n, a ≠ b). It is guaranteed that no two students are tied with more than one lace. No lace ties a student to himself.

Print the single number — the number of groups of students that will be kicked out from the club.

NoteIn the first sample Anna and Maria won't kick out any group of students — in the initial position every student is tied to two other students and Anna won't be able to reprimand anyone.In the second sample four students are tied in a chain and two more are running by themselves. First Anna and Maria kick out the two students from both ends of the chain (1 and 4), then — two other students from the chain (2 and 3). At that the students who are running by themselves will stay in the club.In the third sample Anna and Maria will momentarily kick out all students except for the fourth one and the process stops at that point. The correct answer is one.

#include<stdio.h>
#include<string.h>

int main()
{
	int n,m;
	scanf("%d%d",&n,&m);
	int a[105][2];
	int b[105][105];
	int c[105];
	memset(a,0,sizeof(a));
	memset(b,0,sizeof(b));
	memset(c,0,sizeof(c));
	for(int i=0;i<m;i++)
	{
		scanf("%d%d",&a[i][0],&a[i][1]);
		c[a[i][0]]++;
		c[a[i][1]]++;
		b[a[i][0]][a[i][1]]=1;
		b[a[i][1]][a[i][0]]=1;
	}
	int count = 0;
	int countq = 0;
	while(1)
	{
		countq = 0;
		int aa[105];
		for(int j=1;j<=n;j++)
		{
			if(c[j]==1)
			{
				c[j]--;
				++countq;
				aa[countq]=j;

			}
		}
		for(int i=1;i<=countq;i++)
		{
			for(int j=1;j<=n;j++)
			{
				if(b[aa[i]][j]==1) c[j]--;
			}
		}
		if(countq!=0) count++;
		else break;
	}
	printf("%d",count);
}


#include<stdio.h>
#include<math.h>
int main()
{
    int n,i,j,k,a,b,min,t;
    scanf("%d", &n);
    int ara[n];

    for(i=0;i<n;i++)
    {
        scanf("%d", &ara[i]);
    }
    min = abs(ara[0]-ara[1]);
    a=1,b=2;
    for(i=0;i<n;i++)
    {
        for(j=i+1,k=1;k<n;j++,k++)
        {
            if(j==n)
                j=0;
            t = abs(ara[i]-ara[j]);
            if(t<min)
            {
                min = t;
                a=i+1;
                b=j+1;
            }
        }
    }
    printf("%d %d\n", a, b);
}

n soldiers stand in a circle. For each soldier his height ai is known. A reconnaissance unit can be made of such two neighbouring soldiers, whose heights difference is minimal, i.e. |ai - aj| is minimal. So each of them will be less noticeable with the other. Output any pair of soldiers that can form a reconnaissance unit.

The first line contains integer n (2 ≤ n ≤ 100) — amount of soldiers. Then follow the heights of the soldiers in their order in the circle — n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 1000). The soldier heights are given in clockwise or counterclockwise direction.

Output two integers — indexes of neighbouring soldiers, who should form a reconnaissance unit. If there are many optimum solutions, output any of them. Remember, that the soldiers stand in a circle.

#include<stdio.h>
#include<math.h>
int main()
{
    int n,c,d,t,m,s;
    scanf("%d",&n);
    int a[n];
    for(int i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    m=abs(a[1]-a[0]);
    c=1;
    d=2;
    for(int i=1;i<n-1;i++)
    {
        t=abs(a[i+1]-a[i]);
        if(t<m)
        {
            m=t;
            c=i+1;
            d=i+2;
        }
    }
    s=abs(a[n-1]-a[0]);
    if(s<m)
    {
        printf("%d %d",n,1);
    }
    else
    {
        printf("%d %d",c,d);
    }
    return 0;
}


#include <stdio.h>

int f[4000001];

int getf(int now)
{
   if (f[now]==now)
      return now;
   return f[now]=getf(f[now]);
}

int main()
{
   int i,n,m,s=0;
   scanf("%d%d",&n,&m);
   for (i=1;i<=2*n+2*m;i++)
      f[i]=i;
   for (i=1;i<=n;i++)
      if (i<=m)
         f[getf(i)]=getf(2*n+i);
      else
         f[getf(i)]=getf(n+i-m+1);
   for (i=1;i<=m;i++)
      if (m-i+1<=n)
         f[getf(2*n+i)]=getf(n+m-i+1);
      else
         f[getf(2*n+i)]=getf(3*n+m+i-1);
   for (i=1;i<=n;i++)
   {
      if (n-i+1<=m)
         f[getf(n+i)]=getf(n+2*m+i);
      if (i<=m)
         f[getf(n+i)]=getf(i-m+1);
   }
   for (i=1;i<=m;i++)
   {
      if (i<=n)
         f[getf(2*n+m+i)]=getf(n-i+1);
      if (i+n-1<=m)
         f[getf(2*n+m+i)]=getf(2*n+i+n-1);
   }
   for (i=1;i<=2*n+2*m;i++)
      s+=(f[i]==i);
   printf("%d\n",s);
   return 0;
}

Let's imagine: there is a chess piece billiard ball. Its movements resemble the ones of a bishop chess piece. The only difference is that when a billiard ball hits the board's border, it can reflect from it and continue moving.More formally, first one of four diagonal directions is chosen and the billiard ball moves in that direction. When it reaches the square located on the board's edge, the billiard ball reflects from it; it changes the direction of its movement by 90 degrees and continues moving. Specifically, having reached a corner square, the billiard ball is reflected twice and starts to move the opposite way. While it moves, the billiard ball can make an infinite number of reflections. At any square of its trajectory the billiard ball can stop and on that the move is considered completed.  It is considered that one billiard ball a beats another billiard ball b if a can reach a point where b is located.You are suggested to find the maximal number of billiard balls, that pairwise do not beat each other and that can be positioned on a chessboard n × m in size.

The first line contains two integers n and m (2 ≤ n, m ≤ 106).

Print a single number, the maximum possible number of billiard balls that do not pairwise beat each other. Please do not use the %lld specificator to read or write 64-bit numbers in C++. It is preferred to use cin (also you may use the %I64d specificator).

#include <stdio.h>

int par[4000000];
int f[4000000];

int find(int x)
{
     if (par[x] == x) {
	  return x;
     } else {
	  return par[x] = find(par[x]);
     }
}

void unite(int x, int y)
{
     x = find(x);
     y = find(y);

     if (x == y) return;

     par[x] = y;
}

int main()
{
     int n, m, sum = 0, i, j;

     scanf("%d %d", &n, &m);

     if (n < m) {
	  int tmp = n; n = m; m = tmp;
     }

     for (i = 0; i < 2 * n + 2 * m; i++) par[i] = i;

     for (i = 0; i < n; i++) {
	  if (i + m <= n) {
	       unite(n + i + m - 1, i);
	       unite(i + m - 1, n + i);
	  } else {
	       unite(3 * n + m - i - 1, i);
	       unite(n + 2 * m + i, n + i);
	  }

	  if (i >= m - 1) {
	       unite(n + i - m + 1, i);
	       unite(i - m + 1, n + i);
	  } else {
	       unite(2 * n + i, i);
	       unite(2 * n + m - i - 1, n + i);
	  }
     }

     for (i = 0; i < 2 * n + 2 * m; i++) f[find(i)]++;

     for (i = 0; i < 2 * n + 2 * m; i++) {
	  if (f[i] > 0) sum++;
     }

     printf("%d\n", sum);

     return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

char mp[66][66];
int x,y,z;

int sz, arr[15000][4];
int now_gen[4];

int res[5000], res_size;
int tmp[5000], tmp_size;

void gen(int depth, int st){
  int i;

  if(depth==z){
    rep(i,z) arr[sz][i] = now_gen[i];
    sz++;
    return;
  }

  REP(i,st,26){
    now_gen[depth] = i;
    gen(depth+1, i+1);
  }
}

int smaller(void){
  int i;
  rep(i,res_size){
    if(res[i] < tmp[i]) return 0;
    if(res[i] > tmp[i]) return 1;
  }
  return 0;
}

void renew(void){
  int i;

  if(res_size == tmp_size && smaller()) res_size++;
  if(res_size > tmp_size){
    res_size = tmp_size;
    rep(i,res_size) res[i] = tmp[i];
  }
}

int main(){
  int i,j,k,l,m,n,loop,mi;
  int sx,sy,ex,ey,tx,ty,nx,ny;
  int dx[4]={-1,1,0,0}, dy[4]={0,0,1,-1};
  int usa[30];
  int dist[55][55], chked[55][55];
  int q[3333], q_st, q_size;
  int ok1[3333], ok2[3333], ok1_size, ok2_size;

  while(scanf("%d%d%d",&x,&y,&z)==3){
    rep(i,x) scanf("%s",mp[i]);

    sz = 0;
    gen(0, 0);

    rep(i,x) rep(j,y){
      if(mp[i][j]=='S'){ mp[i][j] = 27; sx = i; sy = j; continue; }
      if(mp[i][j]=='T'){ mp[i][j] = 28; ex = i; ey = j; continue; }
      mp[i][j] -= 'a';
    }

    res_size = 5000;

    rep(loop,sz){
      rep(i,30) usa[i] = 0;
      rep(i,z) usa[arr[loop][i]]=1; usa[28]=1;

      rep(i,x) rep(j,y) dist[i][j] = -1;
      dist[sx][sy] = 0;

      q_st = 0; q_size = 1;
      q[0] = sx*y + sy;

      while(q_size){
        tx = q[q_st]/y; ty = q[q_st]%y;
        q_st++; q_size--;

        rep(k,4){
          nx = tx + dx[k]; ny = ty + dy[k];
          if(nx<0||ny<0 || nx>=x||ny>=y) continue;
          if(!usa[mp[nx][ny]]) continue;
          if(dist[nx][ny]>=0) continue;

          dist[nx][ny] = dist[tx][ty] + 1;
          q[q_st+q_size++] = nx*y + ny;
        }
      }

      if(dist[ex][ey]==-1) continue;
      if(dist[ex][ey]-1 > res_size) continue;

      rep(i,x) rep(j,y) chked[i][j] = 0;
      chked[sx][sy] = 1;

      tmp_size = 0;
      ok1_size = 1; ok1[0] = sx*y + sy;
      while(tmp_size < dist[ex][ey]-1){
        mi = 100;
        rep(i,ok1_size){
          tx = ok1[i]/y; ty = ok1[i]%y;
          rep(k,4){
            nx = tx + dx[k]; ny = ty + dy[k];
            if(nx<0||ny<0 || nx>=x||ny>=y) continue;
            if(dist[nx][ny] != dist[tx][ty]+1) continue;
            if(mi > mp[nx][ny]) mi = mp[nx][ny];
          }
        }

        ok2_size = 0;
        rep(i,ok1_size){
          tx = ok1[i]/y; ty = ok1[i]%y;
          rep(k,4){
            nx = tx + dx[k]; ny = ty + dy[k];
            if(nx<0||ny<0 || nx>=x||ny>=y) continue;
            if(dist[nx][ny] != dist[tx][ty]+1) continue;
            if(chked[nx][ny]) continue;
            if(mi == mp[nx][ny]){
              ok2[ok2_size++] = nx*y + ny;
              chked[nx][ny]=1;
            }
          }
        }

        ok1_size = ok2_size;
        rep(i,ok1_size) ok1[i] = ok2[i];

        tmp[tmp_size++] = mi;
      }

      renew();
    }

    if(res_size==5000){puts("-1"); continue;}
    rep(i,res_size) putchar('a' + res[i]); puts("");
  }

  return 0;
}

You already know that Valery's favorite sport is biathlon. Due to your help, he learned to shoot without missing, and his skills are unmatched at the shooting range. But now a smaller task is to be performed, he should learn to complete the path fastest.The track's map is represented by a rectangle n × m in size divided into squares. Each square is marked with a lowercase Latin letter (which means the type of the plot), with the exception of the starting square (it is marked with a capital Latin letters S) and the terminating square (it is marked with a capital Latin letter T). The time of movement from one square to another is equal to 1 minute. The time of movement within the cell can be neglected. We can move from the cell only to side-adjacent ones, but it is forbidden to go beyond the map edges. Also the following restriction is imposed on the path: it is not allowed to visit more than k different types of squares (squares of one type can be visited an infinite number of times). Squares marked with S and T have no type, so they are not counted. But S must be visited exactly once — at the very beginning, and T must be visited exactly once — at the very end.Your task is to find the path from the square S to the square T that takes minimum time. Among all shortest paths you should choose the lexicographically minimal one. When comparing paths you should lexicographically represent them as a sequence of characters, that is, of plot types.

The first input line contains three integers n, m and k (1 ≤ n, m ≤ 50, n·m ≥ 2, 1 ≤ k ≤ 4). Then n lines contain the map. Each line has the length of exactly m characters and consists of lowercase Latin letters and characters S and T. It is guaranteed that the map contains exactly one character S and exactly one character T. Pretest 12 is one of the maximal tests for this problem.

If there is a path that satisfies the condition, print it as a sequence of letters — the plot types. Otherwise, print "-1" (without quotes). You shouldn't print the character S in the beginning and T in the end. Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one "End of line"-character. Both will be accepted.

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

char mp[66][66];
int x,y,z;

int sz, arr[15000][4];
int now_gen[4];

int res[5000], res_size;
int tmp[5000], tmp_size;

void gen(int depth, int st){
  int i;

  if(depth==z){
    rep(i,z) arr[sz][i] = now_gen[i];
    sz++;
    return;
  }

  REP(i,st,26){
    now_gen[depth] = i;
    gen(depth+1, i+1);
  }
}

int smaller(void){
  int i;
  rep(i,res_size){
    if(res[i] < tmp[i]) return 0;
    if(res[i] > tmp[i]) return 1;
  }
  return 0;
}

void renew(void){
  int i;

  if(res_size == tmp_size && smaller()) res_size++;
  if(res_size > tmp_size){
    res_size = tmp_size;
    rep(i,res_size) res[i] = tmp[i];
  }
}

int main(){
  int i,j,k,l,m,n,loop,mi;
  int sx,sy,ex,ey,tx,ty,nx,ny;
  int dx[4]={-1,1,0,0}, dy[4]={0,0,1,-1};
  int usa[30];
  int dist[55][55], chked[55][55];
  int q[3333], q_st, q_size;
  int ok1[3333], ok2[3333], ok1_size, ok2_size;

  while(scanf("%d%d%d",&x,&y,&z)==3){
    rep(i,x) scanf("%s",mp[i]);

    sz = 0;
    gen(0, 0);

    rep(i,x) rep(j,y){
      if(mp[i][j]=='S'){ mp[i][j] = 27; sx = i; sy = j; continue; }
      if(mp[i][j]=='T'){ mp[i][j] = 28; ex = i; ey = j; continue; }
      mp[i][j] -= 'a';
    }

    res_size = 5000;

    rep(loop,sz){
      rep(i,30) usa[i] = 0;
      rep(i,z) usa[arr[loop][i]]=1; usa[27]=usa[28]=1;

      rep(i,x) rep(j,y) dist[i][j] = -1;
      dist[ex][ey] = 0;

      q_st = 0; q_size = 1;
      q[0] = ex*y + ey;

      while(q_size){
        tx = q[q_st]/y; ty = q[q_st]%y;
        q_st++; q_size--;

        rep(k,4){
          nx = tx + dx[k]; ny = ty + dy[k];
          if(nx<0||ny<0 || nx>=x||ny>=y) continue;
          if(!usa[mp[nx][ny]]) continue;
          if(dist[nx][ny]>=0) continue;

          dist[nx][ny] = dist[tx][ty] + 1;
          q[q_st+q_size++] = nx*y + ny;
        }
      }

      if(dist[sx][sy]==-1) continue;
      if(dist[sx][sy]-1 > res_size) continue;

      rep(i,x) rep(j,y) chked[i][j] = 0;
      chked[sx][sy] = 1;

      tmp_size = 0;
      ok1_size = 1; ok1[0] = sx*y + sy;
      while(tmp_size < dist[sx][sy]-1){
        mi = 100;
        rep(i,ok1_size){
          tx = ok1[i]/y; ty = ok1[i]%y;
          rep(k,4){
            nx = tx + dx[k]; ny = ty + dy[k];
            if(nx<0||ny<0 || nx>=x||ny>=y) continue;
            if(dist[nx][ny] != dist[tx][ty]-1) continue;
            if(mi > mp[nx][ny]) mi = mp[nx][ny];
          }
        }

        ok2_size = 0;
        rep(i,ok1_size){
          tx = ok1[i]/y; ty = ok1[i]%y;
          rep(k,4){
            nx = tx + dx[k]; ny = ty + dy[k];
            if(nx<0||ny<0 || nx>=x||ny>=y) continue;
            if(dist[nx][ny] != dist[tx][ty]-1) continue;
            if(chked[nx][ny]) continue;
            if(mi == mp[nx][ny]){
              ok2[ok2_size++] = nx*y + ny;
              chked[nx][ny]=1;
            }
          }
        }

        ok1_size = ok2_size;
        rep(i,ok1_size) ok1[i] = ok2[i];

        tmp[tmp_size++] = mi;
      }

      renew();
    }

    if(res_size==5000){puts("-1"); continue;}
    rep(i,res_size) putchar('a' + res[i]); puts("");
  }

  return 0;
}

[{'input': '5 3 2\r\nSba\r\nccc\r\naac\r\nccc\r\nabT\r\n', 'output': ['bcccc\r\n']}, {'input': '3 4 1\r\nSxyy\r\nyxxx\r\nyyyT\r\n', 'output': ['xxxx\r\n']}, {'input': '1 3 3\r\nTyS\r\n', 'output': ['y\r\n']}, {'input': '1 4 1\r\nSxyT\r\n', 'output': ['-1\r\n']}, {'input': '1 3 3\r\nSaT\r\n', 'output': ['a\r\n']}, {'input': '3 4 1\r\nSbbT\r\naaaa\r\nabba\r\n', 'output': ['bb\r\n']}, {'input': '3 5 2\r\nSbcaT\r\nacbab\r\nacccb\r\n', 'output': ['aacccaa\r\n']}, {'input': '3 4 1\r\nSbbb\r\naaaT\r\nabbc\r\n', 'output': ['aaa\r\n']}, {'input': '3 4 2\r\nSbbb\r\naabT\r\nabbc\r\n', 'output': ['aab\r\n']}, {'input': '1 2 1\r\nST\r\n', 'output': ['\r\n']}, {'input': '4 5 3\r\nabaaa\r\nbabaT\r\nSabba\r\naaaaa\r\n', 'output': ['aaba\r\n']}, {'input': '6 6 3\r\npkhipk\r\nmlfmak\r\naqmbae\r\ndlbfSj\r\ndpbjcr\r\naTbqbm\r\n', 'output': ['cbqb\r\n']}, {'input': '1 20 3\r\nacbccbbddbffScTadffd\r\n', 'output': ['c\r\n']}, {'input': '1 30 2\r\nbmjcfldkloleiqqiTnmdjpaSckkijf\r\n', 'output': ['-1\r\n']}, {'input': '1 40 1\r\nfaSfgfTcfadcdfagfbccbffbeaaebagbfcfcgdfd\r\n', 'output': ['-1\r\n']}, {'input': '1 50 3\r\nSaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaTaaaaaaaaaaa\r\n', 'output': ['aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\r\n']}, {'input': '5 10 4\r\naaaaaaaaaa\r\naaaaaTaaaa\r\naaaaaaaSaa\r\naaaaaaaaaa\r\naaaaaaaaaa\r\n', 'output': ['aa\r\n']}, {'input': '5 3 4\r\naaT\r\nacc\r\nbbb\r\nbbc\r\ncSb\r\n', 'output': ['bbbc\r\n']}, {'input': '5 5 1\r\ncaTbc\r\ndccac\r\ndacda\r\naacaS\r\ncdcab\r\n', 'output': ['-1\r\n']}, {'input': '10 8 2\r\nbdcdcbfa\r\ndecffcce\r\ndTffdacb\r\neeedcdbb\r\nfdbbbcba\r\nddabfcda\r\nabdbSeed\r\nbdcdcffa\r\ncadbaffa\r\nfcccddad\r\n', 'output': ['bbbbee\r\n']}, {'input': '20 10 3\r\nebebccacdb\r\neeebccddeT\r\neadebecaac\r\nadeeeaccbc\r\nbaccccdaed\r\ndeabceabba\r\ndadbecbaaa\r\neacbbcedcb\r\naeeScdbbab\r\nbabaecaead\r\nbacdbebeae\r\naacbadbeec\r\nacddceecca\r\nacaeaebaba\r\ncdddeaaeae\r\neabddadade\r\nddddaeaeed\r\nbccbaacadd\r\ndccccbabdc\r\necdaebeccc\r\n', 'output': ['bbbcccaccaac\r\n']}, {'input': '15 10 4\r\nsejwprqjku\r\npnjsiopxft\r\nrsplgvwixq\r\nendglkchxl\r\nftihbbexgh\r\nsxtxbbavge\r\njcdkusfnmr\r\nskgsqvflia\r\nkcxmcxjpae\r\namaiwcfile\r\nnjgjSunmwd\r\nldxvahgreu\r\necmrajbjuT\r\nnaioqigols\r\npbwrmxkltj\r\n', 'output': ['aajbju\r\n']}, {'input': '15 3 4\r\nllv\r\nttT\r\nhbo\r\nogc\r\nkfe\r\ngli\r\nfbx\r\nkfp\r\nspm\r\ncxc\r\nndw\r\nSoa\r\npfh\r\nedr\r\nxmv\r\n', 'output': ['-1\r\n']}, {'input': '15 15 3\r\ncbbdccabdcbacbd\r\nbcabdcacadacdbc\r\ncbcddbbcdbddcad\r\nddcabdbbdcabbdc\r\naabadcccTcabdbb\r\ncbacaaacaabdbbd\r\ndbdcbSdabaadbdb\r\ndbbaddcdddaadbb\r\nbbddcdcbaccbbaa\r\nadadadbdbbddccc\r\ncddbbdaddcbbdcc\r\nbbaadcdbbcaacca\r\nadbdcdbbcbddbcd\r\ncdadbcccddcdbda\r\ncbcdaabdcabccbc\r\n', 'output': ['aaca\r\n']}, {'input': '20 20 2\r\nddadfcdeTaeccbedeaec\r\nacafdfdeaffdeabdcefe\r\nabbcbefcdbbbcdebafef\r\nfdafdcccbcdeeaedeffc\r\ndfdaabdefdafabaabcef\r\nfebdcabacaaaabfacbbe\r\nabfcaacadfdbfdbaaefd\r\ndacceeccddccaccdbbce\r\ncacebecabedbddfbfdad\r\ndacbfcabbebfddcedffd\r\ncfcdfacfadcfbcebebaa\r\nddfbebafaccbebeefbac\r\nebfaebacbbebdfcbcbea\r\ndfbaebcfccacfeaccaad\r\nedeedeceebcbfdbcdbbe\r\nafaacccfbdecebfdabed\r\nddbdcedacedadeccaeec\r\necbSeacbdcccbcedafef\r\ncfdbeeffbeeafccfdddb\r\ncefdbdfbabccfdaaadbf\r\n', 'output': ['-1\r\n']}, {'input': '10 10 2\r\nbaaaaaaaaa\r\nbffacffffa\r\nbggaccggga\r\nbbbSccchha\r\nbdddddccia\r\nbjddccccca\r\nbkkdddTaaa\r\nblllddblla\r\nbmmmmdbmma\r\nbbbbbbbbbb\r\n', 'output': ['ccccc\r\n']}, {'input': '10 20 3\r\nbaaaaaaaaaaaaaaaaaaa\r\nbfffffffacfffffffffa\r\nbgggggggaccgggggggga\r\nbbbbbbbbSccchhhhhhha\r\nbiiiiidddddcciiiiiia\r\nbjjjjjjddcccccjjjjja\r\nbkkkkkkkdddTaaaaaaaa\r\nbllllllllddbllllllla\r\nbmmmmmmmmmdbmmmmmmma\r\nbbbbbbbbbbbbbbbbbbbb\r\n', 'output': ['ccccc\r\n']}, {'input': '20 10 4\r\nbaaaaaaaaa\r\nbffacffffa\r\nbggaccggga\r\nbhhaccchha\r\nbiiaccccia\r\nbjjaccccca\r\nbkkakkkkka\r\nbllallllla\r\nbbbSmmmmma\r\nbnnnnnnnna\r\nbooooooooa\r\nbpppppTaaa\r\nbqqqqqbqqa\r\nbrrrrrbrra\r\nbdddddbssa\r\nbtddddbtta\r\nbuudddbuua\r\nbvvvddbvva\r\nbwwwwdbwwa\r\nbbbbbbbbbb\r\n', 'output': ['mmmno\r\n']}, {'input': '20 20 2\r\nbaaaaaaaaaaaaaaaaaaa\r\nbfffffffacfffffffffa\r\nbgggggggaccgggggggga\r\nbhhhhhhhaccchhhhhhha\r\nbiiiiiiiacccciiiiiia\r\nbjjjjjjjacccccjjjjja\r\nbkkkkkkkacccccckkkka\r\nblllllllacccccccllla\r\nbbbbbbbbSccccccccmma\r\nbddddddddddcccccccna\r\nbodddddddcccccccccca\r\nbppddddddddTaaaaaaaa\r\nbqqqdddddddbqqqqqqqa\r\nbrrrrddddddbrrrrrrra\r\nbsssssdddddbsssssssa\r\nbttttttddddbttttttta\r\nbuuuuuuudddbuuuuuuua\r\nbvvvvvvvvddbvvvvvvva\r\nbwwwwwwwwwdbwwwwwwwa\r\nbbbbbbbbbbbbbbbbbbbb\r\n', 'output': ['ccccc\r\n']}, {'input': '1 2 4\r\nST\r\n', 'output': ['\r\n']}, {'input': '3 3 1\r\naaa\r\naaa\r\nTSa\r\n', 'output': ['\r\n']}, {'input': '2 1 1\r\nS\r\nT\r\n', 'output': ['\r\n']}, {'input': '1 10 2\r\nbaaSaaTacb\r\n', 'output': ['aa\r\n']}, {'input': '2 1 4\r\nS\r\nT\r\n', 'output': ['\r\n']}]


#include <stdio.h>
#include <stdlib.h>

#define A	5000
#define N_	(A * 3)
#define INF	0x3f3f3f3f3f3f3f3fLL

long long min(long long a, long long b) { return a < b ? a : b; }

int oo[1 + (N_ - 1) * 2], oj[1 + (N_ - 1) * 2];

int link(int o, int j) {
	static int _ = 1;

	oo[_] = o, oj[_] = j;
	return _++;
}

int count(int n, int p) {
	return n < p ? 0 : n / p + count(n / p, p);
}

int ae[N_], pp[N_], dd[N_], sz[N_], ii[A + 1], n;

void init() {
	static char composite[A + 1];
	static int primes[A + 1];
	int n_, a, b, cnt, i, j;

	for (a = 2; a <= A / a; a++) {
		if (composite[a])
			continue;
		for (b = a * a; b <= A; b += a)
			composite[b] = 1;
	}
	cnt = 0;
	for (a = 2; a <= A; a++)
		if (!composite[a])
			primes[cnt++] = a;
	n_ = 0;
	pp[n_] = -1, dd[n_] = 0;
	ii[1] = n_++;
	for (a = 2; a <= A; a++) {
		int h, d, d_;

		d = 0;
		for (h = 0; h < cnt; h++)
			d += count(a, primes[h]);
		d_ = 0;
		for (h = cnt - 1; h >= 0; h--) {
			d_ += count(a - 1, primes[h]);
			if (a % primes[h] == 0)
				break;
		}
		i = ii[a - 1], j = -1;
		while (1)
			if (dd[i] > d_)
				j = i, i = pp[i];
			else if (dd[i] == d_) {
				pp[n_] = i, dd[n_] = d, n_++;
				ii[a] = n_ - 1;
				break;
			} else {
				int k = n_++;

				pp[k] = i, dd[k] = d_;
				if (j != -1)
					pp[j] = k;
				pp[n_] = k, dd[n_] = d, n_++;
				ii[a] = n_ - 1;
				break;
			}
	}
	for (j = 1; j < n_; j++) {
		i = pp[j];
		ae[i] = link(ae[i], j), ae[j] = link(ae[j], i);
	}
}

long long dfs1(int p, int i) {
	int o;
	long long x;

	x = (long long) sz[i] * dd[i];
	for (o = ae[i]; o; o = oo[o]) {
		int j = oj[o];

		if (j != p) {
			sz[i] += sz[j];
			x += dfs1(i, j);
		}
	}
	return x;
}

long long ans;

void dfs2(int p, int i, long long x) {
	int o;

	ans = min(ans, x);
	for (o = ae[i]; o; o = oo[o]) {
		int j = oj[o];

		if (j != p) {
			int w = abs(dd[i] - dd[j]);

			dfs2(i, j, x - (long long) sz[j] * w + (long long) (n - sz[j]) * w);
		}
	}
}

int main() {
	int i;

	init();
	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		int a;

		scanf("%d", &a);
		sz[ii[a > 0 ? a : 1]]++;
	}
	ans = INF;
	dfs2(-1, 0, dfs1(-1, 0));
	printf("%lld\n", ans);
	return 0;
}

Æsir - CHAOS Æsir - V."Everything has been planned out. No more hidden concerns. The condition of Cytus is also perfect.The time right now...... 00:01:12......It's time."The emotion samples are now sufficient. After almost 3 years, it's time for Ivy to awake her bonded sister, Vanessa.The system inside A.R.C.'s Library core can be considered as an undirected graph with infinite number of processing nodes, numbered with all positive integers ($$$1, 2, 3, \ldots$$$). The node with a number $$$x$$$ ($$$x > 1$$$), is directly connected with a node with number $$$\frac{x}{f(x)}$$$, with $$$f(x)$$$ being the lowest prime divisor of $$$x$$$.Vanessa's mind is divided into $$$n$$$ fragments. Due to more than 500 years of coma, the fragments have been scattered: the $$$i$$$-th fragment is now located at the node with a number $$$k_i!$$$ (a factorial of $$$k_i$$$).To maximize the chance of successful awakening, Ivy decides to place the samples in a node $$$P$$$, so that the total length of paths from each fragment to $$$P$$$ is smallest possible. If there are multiple fragments located at the same node, the path from that node to $$$P$$$ needs to be counted multiple times.In the world of zeros and ones, such a requirement is very simple for Ivy. Not longer than a second later, she has already figured out such a node.But for a mere human like you, is this still possible?For simplicity, please answer the minimal sum of paths' lengths from every fragment to the emotion samples' assembly node $$$P$$$.

The first line contains an integer $$$n$$$ ($$$1 \le n \le 10^6$$$) — number of fragments of Vanessa's mind. The second line contains $$$n$$$ integers: $$$k_1, k_2, \ldots, k_n$$$ ($$$0 \le k_i \le 5000$$$), denoting the nodes where fragments of Vanessa's mind are located: the $$$i$$$-th fragment is at the node with a number $$$k_i!$$$.

Print a single integer, denoting the minimal sum of path from every fragment to the node with the emotion samples (a.k.a. node $$$P$$$). As a reminder, if there are multiple fragments at the same node, the distance from that node to $$$P$$$ needs to be counted multiple times as well.

NoteConsidering the first $$$24$$$ nodes of the system, the node network will look as follows (the nodes $$$1!$$$, $$$2!$$$, $$$3!$$$, $$$4!$$$ are drawn bold):For the first example, Ivy will place the emotion samples at the node $$$1$$$. From here:  The distance from Vanessa's first fragment to the node $$$1$$$ is $$$1$$$.  The distance from Vanessa's second fragment to the node $$$1$$$ is $$$0$$$.  The distance from Vanessa's third fragment to the node $$$1$$$ is $$$4$$$. The total length is $$$5$$$.For the second example, the assembly node will be $$$6$$$. From here:  The distance from Vanessa's first fragment to the node $$$6$$$ is $$$0$$$.  The distance from Vanessa's second fragment to the node $$$6$$$ is $$$2$$$.  The distance from Vanessa's third fragment to the node $$$6$$$ is $$$2$$$.  The distance from Vanessa's fourth fragment to the node $$$6$$$ is again $$$2$$$. The total path length is $$$6$$$.

#include <stdio.h>
#include <stdlib.h>

#define A	5000
#define N_	(A * 3)
#define INF	0x3f3f3f3f3f3f3f3fLL

long long min(long long a, long long b) { return a < b ? a : b; }

int oo[1 + (N_ - 1) * 2], oj[1 + (N_ - 1) * 2];

int link(int o, int j) {
	static int _ = 1;

	oo[_] = o, oj[_] = j;
	return _++;
}

int count(int n, int p) {
	return n < p ? 0 : n / p + count(n / p, p);
}

int ae[N_], pp[N_], dd[N_], sz[N_], ii[A + 1], n;

void init() {
	static char composite[A + 1];
	static int primes[A + 1];
	int n_, a, b, cnt, i, j;

	for (a = 2; a <= A / a; a++) {
		if (composite[a])
			continue;
		for (b = a * a; b <= A; b += a)
			composite[b] = 1;
	}
	cnt = 0;
	for (a = 2; a <= A; a++)
		if (!composite[a])
			primes[cnt++] = a;
	n_ = 0;
	pp[n_] = -1, dd[n_] = 0;
	ii[1] = n_++;
	for (a = 2; a <= A; a++) {
		int h, d, d_;

		d = 0;
		for (h = 0; h < cnt; h++)
			d += count(a, primes[h]);
		d_ = 0;
		for (h = cnt - 1; h >= 0; h--) {
			d_ += count(a - 1, primes[h]);
			if (a % primes[h] == 0)
				break;
		}
		i = ii[a - 1], j = -1;
		while (1)
			if (dd[i] > d_)
				j = i, i = pp[i];
			else if (dd[i] == d_) {
				pp[n_] = i, dd[n_] = d, n_++;
				ii[a] = n_ - 1;
				break;
			} else {
				int k = n_++;

				pp[k] = i, dd[k] = d_;
				if (j != -1)
					pp[j] = k;
				pp[n_] = k, dd[n_] = d, n_++;
				ii[a] = n_ - 1;
				break;
			}
	}
	for (j = 1; j < n_; j++) {
		i = pp[j];
		ae[i] = link(ae[i], j), ae[j] = link(ae[j], i);
	}
}

long long dfs1(int p, int i) {
	int o;
	long long x;

	x = (long long) sz[i] * dd[i];
	for (o = ae[i]; o; o = oo[o]) {
		int j = oj[o];

		if (j != p) {
			x += dfs1(i, j);
			sz[i] += sz[j];
		}
	}
	return x;
}

long long ans;

void dfs2(int p, int i, long long x) {
	int o;

	ans = min(ans, x);
	for (o = ae[i]; o; o = oo[o]) {
		int j = oj[o];

		if (j != p) {
			int w = abs(dd[i] - dd[j]);

			dfs2(i, j, x - (long long) sz[j] * w + (long long) (n - sz[j]) * w);
		}
	}
}

int main() {
	int i;

	init();
	scanf("%d", &n);
	for (i = 0; i < n; i++) {
		int a;

		scanf("%d", &a);
		sz[ii[a > 0 ? a : 1]]++;
	}
	ans = INF;
	dfs2(-1, 0, dfs1(-1, 0));
	printf("%lld\n", ans);
	return 0;
}


Today you are to solve the problem even the famous Hercule Poirot can't cope with! That's why this crime has not yet been solved and this story was never included in Agatha Christie's detective story books. You are not informed on what crime was committed, when and where the corpse was found and other details. We only know that the crime was committed in a house that has n rooms and m doors between the pairs of rooms. The house residents are very suspicious, that's why all the doors can be locked with keys and all the keys are different. According to the provided evidence on Thursday night all the doors in the house were locked, and it is known in what rooms were the residents, and what kind of keys had any one of them. The same is known for the Friday night, when all the doors were also locked. On Friday it was raining heavily, that's why nobody left the house and nobody entered it. During the day the house residents could  open and close doors to the neighboring rooms using the keys at their disposal (every door can be opened and closed from each side);  move freely from a room to a room if a corresponding door is open;  give keys to one another, being in one room.  "Little grey matter" of Hercule Poirot are not capable of coping with such amount of information. Find out if the positions of people and keys on the Thursday night could result in the positions on Friday night, otherwise somebody among the witnesses is surely lying.

The first line contains three preset integers n, m и k (1 ≤ n, m, k ≤ 1000) — the number of rooms, the number of doors and the number of house residents respectively. The next m lines contain pairs of room numbers which join the doors. The rooms are numbered with integers from 1 to n. There cannot be more that one door between the pair of rooms. No door connects a room with itself. The next k lines describe the residents' position on the first night. Every line contains a resident's name (a non-empty line consisting of no more than 10 Latin letters), then after a space follows the room number, then, after a space — the number of keys the resident has. Then follow written space-separated numbers of the doors that can be unlocked by these keys. The doors are numbered with integers from 1 to m in the order in which they are described in the input data. All the residents have different names, uppercase and lowercase letters considered to be different. Every m keys occurs exactly once in the description. Multiple people may be present in one room, some rooms may be empty. The next k lines describe the position of the residents on the second night in the very same format. It is guaranteed that in the second night's description the residents' names remain the same and every m keys occurs exactly once.

Print "YES" (without quotes) if the second arrangement can result from the first one, otherwise, print "NO".

2 1 2
1 2
Dmitry 1 1 1
Natalia 2 0
Natalia 1 1 1
Dmitry 2 0

4 4 3
1 3
1 2
2 3
3 4
Artem 1 1 4
Dmitry 1 1 2
Edvard 4 2 1 3
Artem 2 0
Dmitry 1 0
Edvard 4 4 1 2 3 4


#include <stdio.h>

int min(int a, int b)
{
     if (a < b) {
	  return a;
     } else {
	  return b;
     }
}

int main()
{
     int n, m, k, f = 0, i;

     scanf("%d %d %d", &n, &m, &k);

     for (i = 0; i < k; i++) {
	  int x, y;

	  scanf("%d %d", &x, &y);

	  if (min(min(x, n - x + 1), min(y, m - y + 1)) < 5) f = 1;
     }

     if (f == 1) {
	  puts("YES");
     } else {
	  puts("NO");
     }

     return 0;
}

Volodya and Vlad play the following game. There are k pies at the cells of n  ×  m board. Each turn Volodya moves one pie to the neighbouring (by side) cell. If the pie lies at the border of the board then Volodya can move it outside the board, get the pie and win. After Volodya's move, Vlad bans some edge at the border of the board of length 1 (between two knots of the board) so that Volodya is not able to move the pie outside the board through this edge anymore. The question is: will Volodya win this game? We suppose both players follow the optimal strategy.

First line contains 3 integers, separated by space: 1 ≤ n, m ≤ 100 — dimensions of the board and 0 ≤ k ≤ 100 — the number of pies. Each of the next k lines contains 2 integers, separated by space: 1 ≤ x ≤ n, 1 ≤ y ≤ m — coordinates of the corresponding pie. There could be more than one pie at a cell.

Output only one word: "YES" — if Volodya wins, "NO" — otherwise.

#include <stdio.h>
#include <stdlib.h>

int main()
{
     int m,n,k,x,y,i,r=0;
    scanf("%d %d %d\n",&m,&n,&k);
    if(k<1) printf("NO");
    else{ if(n<5||m<5)
    {
        printf("YES");

    }
    else
        {
    for(i=0;i<k;i++){
    scanf("%d %d",&x,&y);

        if(x>m-5||y>n-5||x<6||y<6)

        {
            r=r+1;

       }}
       if (r==0)
    printf("NO");
    else printf("YES");

    }

    }
    }


#include <stdio.h>
#include <stdlib.h>

#define X	1000000
#define Q	100000

long long cross2(long long x1, long long y1, long long x2, long long y2) {
	return x1 * y2 - x2 * y1;
}

struct P {
	int x, y;
} *pl, *pr;

typedef struct P *T[(X + X + 1) * 4];

long long cross(struct P *o, struct P *p, struct P *q) {
	return cross2(p->x - o->x, p->y - o->y, q->x - o->x, q->y - o->y);
}

struct P *min(struct P *p, struct P *q) {
	return p != NULL && q != NULL ? (p->x < q->x ? p : q) : (p == NULL ? q : p);
}

struct P *max(struct P *p, struct P *q) {
	return p != NULL && q != NULL ? (p->x > q->x ? p : q) : (p == NULL ? q : p);
}

void update(T tl, T tr, int k, int l, int r, int x, struct P *p) {
	int m;

	if (r - l == 1) {
		tl[k] = tr[k] = p;
		return;
	}
	m = (l + r) / 2;
	if (x < m)
		update(tl, tr, k * 2 + 1, l, m, x, p);
	else
		update(tl, tr, k * 2 + 2, m, r, x, p);
	tr[k] = min(tr[k * 2 + 1], tr[k * 2 + 2]);
	tl[k] = max(tl[k * 2 + 1], tl[k * 2 + 2]);
}

void query(T tl, T tr, int k, int l, int r, int x) {
	int m;

	if (r - l == 1) {
		pl = max(pl, tl[k]);
		pr = min(pr, tr[k]);
		return;
	}
	m = (l + r) / 2;
	if (x < m) {
		pr = min(pr, tr[k * 2 + 2]);
		query(tl, tr, k * 2 + 1, l, m, x);
	} else {
		pl = max(pl, tl[k * 2 + 1]);
		query(tl, tr, k * 2 + 2, m, r, x);
	}
}

int query_(T tl_up, T tr_up, T tl_dn, T tr_dn, struct P *p, int insert) {
	struct P *q;
	int inside = 1;

	pl = pr = NULL;
	query(tl_up, tr_up, 0, 0, X + X + 1, p->x);
	if ((pl == pr
				&& (pl == NULL || pl->y < p->y))
			|| (pl != pr
				&& (pl == NULL || pr == NULL || cross(pl, p, pr) < 0))) {
		if (insert) {
			while ((q = pl) && q->x > 0) {
				pl = pr = NULL;
				query(tl_up, tr_up, 0, 0, X + X + 1, q->x - 1);
				if (!pl || cross(pl, q, p) < 0)
					break;
				update(tl_up, tr_up, 0, 0, X + X + 1, q->x, NULL);
			}
			while ((q = pr) && q->x < X + X) {
				pl = pr = NULL;
				query(tl_up, tr_up, 0, 0, X + X + 1, q->x + 1);
				if (!pr || cross(p, q, pr) < 0)
					break;
				update(tl_up, tr_up, 0, 0, X + X + 1, q->x, NULL);
			}
			update(tl_up, tr_up, 0, 0, X + X + 1, p->x, p);
		}
		inside = 0;
	}
	pl = pr = NULL;
	query(tl_dn, tr_dn, 0, 0, X + X + 1, p->x);
	if ((pl == pr
				&& (pl == NULL || pl->y > p->y))
			|| (pl != pr
				&& (pl == NULL || pr == NULL || cross(pl, p, pr) > 0))) {
		if (insert) {
			while ((q = pl) && q->x > 0) {
				pl = pr = NULL;
				query(tl_dn, tr_dn, 0, 0, X + X + 1, q->x - 1);
				if (!pl || cross(pl, q, p) > 0)
					break;
				update(tl_dn, tr_dn, 0, 0, X + X + 1, q->x, NULL);
			}
			while ((q = pr) && q->x < X + X) {
				pl = pr = NULL;
				query(tl_dn, tr_dn, 0, 0, X + X + 1, q->x + 1);
				if (!pr || cross(p, q, pr) > 0)
					break;
				update(tl_dn, tr_dn, 0, 0, X + X + 1, q->x, NULL);
			}
			update(tl_dn, tr_dn, 0, 0, X + X + 1, p->x, p);
		}
		inside = 0;
	}
	return inside;
}

int main() {
	static T tl_up, tr_up, tl_dn, tr_dn;
	int q;

	scanf("%d", &q);
	while (q-- > 0) {
		struct P *p = malloc(sizeof *p);
		int t;

		scanf("%d%d%d", &t, &p->x, &p->y);
		p->x += X;
		if (t == 1)
			query_(tl_up, tr_up, tl_dn, tr_dn, p, 1);
		else
			printf(query_(tl_up, tr_up, tl_dn, tr_dn, p, 0) ? "YES\n" : "NO\n");
	}
	return 0;
}

Once a walrus professor Plato asked his programming students to perform the following practical task. The students had to implement such a data structure that would support a convex hull on some set of points S. The input to the program had q queries of two types: 1. Add a point with coordinates (x, y) into the set S. Note that in this case the convex hull of S could have changed, and could have remained the same. 2. Say whether a point with coordinates (x, y) belongs to an area limited by the convex hull, including the border. All the students coped with the task. What about you?

The first line contains an integer q (4 ≤ q ≤ 105).  Then follow q lines in the following way: "t x y", where t is the query type (1 or 2), and (x, y) are the coordinates of the point ( - 106 ≤ x, y ≤ 106, x and y are integers).  There is at least one query of type 2. It is guaranteed that the three queries of the first type follow first and the points given in the queries form a non-degenerative triangle. Also all the points added in S are distinct.

For each query of the second type print one string containing "YES", if the point lies inside the convex hull or on its border. Otherwise, print "NO".

#include <stdio.h>
#include <stdlib.h>

#define X	1000000
#define Q	100000

long long cross2(long long x1, long long y1, long long x2, long long y2) {
	return x1 * y2 - x2 * y1;
}

struct P {
	int x, y;
} *pl, *pr;

typedef struct P *T[(X + X + 1) * 4];

long long cross(struct P *o, struct P *p, struct P *q) {
	return cross2(p->x - o->x, p->y - o->y, q->x - o->x, q->y - o->y);
}

struct P *min(struct P *p, struct P *q) {
	return p != NULL && q != NULL ? (p->x < q->x ? p : q) : (p == NULL ? q : p);
}

struct P *max(struct P *p, struct P *q) {
	return p != NULL && q != NULL ? (p->x > q->x ? p : q) : (p == NULL ? q : p);
}

void update(T tl, T tr, int k, int l, int r, int x, struct P *p) {
	int m;

	if (r - l == 1) {
		tl[k] = tr[k] = p;
		return;
	}
	m = (l + r) / 2;
	if (x < m)
		update(tl, tr, k * 2 + 1, l, m, x, p);
	else
		update(tl, tr, k * 2 + 2, m, r, x, p);
	tr[k] = min(tr[k * 2 + 1], tr[k * 2 + 2]);
	tl[k] = max(tl[k * 2 + 1], tl[k * 2 + 2]);
}

void query(T tl, T tr, int k, int l, int r, int x) {
	int m;

	if (r - l == 1) {
		pl = max(pl, tl[k]);
		pr = min(pr, tr[k]);
		return;
	}
	m = (l + r) / 2;
	if (x < m) {
		pr = min(pr, tr[k * 2 + 2]);
		query(tl, tr, k * 2 + 1, l, m, x);
	} else {
		pl = max(pl, tl[k * 2 + 1]);
		query(tl, tr, k * 2 + 2, m, r, x);
	}
}

int query_(T tl_up, T tr_up, T tl_dn, T tr_dn, struct P *p, int insert) {
	struct P *q, *pl_, *pr_;
	int inside = 1;

	pl = pr = NULL;
	query(tl_up, tr_up, 0, 0, X + X + 1, p->x);
	pl_ = pl, pr_ = pr;
	if ((pl == pr
				&& (pl == NULL || pl->y < p->y))
			|| (pl != pr
				&& (pl == NULL || pr == NULL || cross(pl, p, pr) < 0))) {
		if (insert) {
			for (q = pl_; q && q->x > 0; q = pl) {
				pl = pr = NULL;
				query(tl_up, tr_up, 0, 0, X + X + 1, q->x - 1);
				if (!pl || cross(pl, q, p) < 0)
					break;
				update(tl_up, tr_up, 0, 0, X + X + 1, q->x, NULL);
			}
			for (q = pr_; q && q->x < X + X; q = pr) {
				pl = pr = NULL;
				query(tl_up, tr_up, 0, 0, X + X + 1, q->x + 1);
				if (!pr || cross(p, q, pr) < 0)
					break;
				update(tl_up, tr_up, 0, 0, X + X + 1, q->x, NULL);
			}
			update(tl_up, tr_up, 0, 0, X + X + 1, p->x, p);
		}
		inside = 0;
	}
	pl = pr = NULL;
	query(tl_dn, tr_dn, 0, 0, X + X + 1, p->x);
	pl_ = pl, pr_ = pr;
	if ((pl == pr
				&& (pl == NULL || pl->y > p->y))
			|| (pl != pr
				&& (pl == NULL || pr == NULL || cross(pl, p, pr) > 0))) {
		if (insert) {
			for (q = pl_; q && q->x > 0; q = pl) {
				pl = pr = NULL;
				query(tl_dn, tr_dn, 0, 0, X + X + 1, q->x - 1);
				if (!pl || cross(pl, q, p) > 0)
					break;
				update(tl_dn, tr_dn, 0, 0, X + X + 1, q->x, NULL);
			}
			for (q = pr_; q && q->x < X + X; q = pr) {
				pl = pr = NULL;
				query(tl_dn, tr_dn, 0, 0, X + X + 1, q->x + 1);
				if (!pr || cross(p, q, pr) > 0)
					break;
				update(tl_dn, tr_dn, 0, 0, X + X + 1, q->x, NULL);
			}
			update(tl_dn, tr_dn, 0, 0, X + X + 1, p->x, p);
		}
		inside = 0;
	}
	return inside;
}

int main() {
	static T tl_up, tr_up, tl_dn, tr_dn;
	int q;

	scanf("%d", &q);
	while (q-- > 0) {
		struct P *p = malloc(sizeof *p);
		int t;

		scanf("%d%d%d", &t, &p->x, &p->y);
		p->x += X;
		if (t == 1)
			query_(tl_up, tr_up, tl_dn, tr_dn, p, 1);
		else
			printf(query_(tl_up, tr_up, tl_dn, tr_dn, p, 0) ? "YES\n" : "NO\n");
	}
	return 0;
}


There are n students in a class working on group projects. The students will divide into groups (some students may be in groups alone), work on their independent pieces, and then discuss the results together. It takes the i-th student ai minutes to finish his/her independent piece.If students work at different paces, it can be frustrating for the faster students and stressful for the slower ones. In particular, the imbalance of a group is defined as the maximum ai in the group minus the minimum ai in the group. Note that a group containing a single student has an imbalance of 0. How many ways are there for the students to divide into groups so that the total imbalance of all groups is at most k?Two divisions are considered distinct if there exists a pair of students who work in the same group in one division but different groups in the other.

The first line contains two space-separated integers n and k (1 ≤ n ≤ 200, 0 ≤ k ≤ 1000) — the number of students and the maximum total imbalance allowed, respectively. The second line contains n space-separated integers ai (1 ≤ ai ≤ 500) — the time it takes the i-th student to complete his/her independent piece of work.

Print a single integer, the number of ways the students can form groups. As the answer may be large, print its value modulo 109 + 7.

NoteIn the first sample, we have three options:   The first and second students form a group, and the third student forms a group. Total imbalance is 2 + 0 = 2.  The first student forms a group, and the second and third students form a group. Total imbalance is 0 + 1 = 1.  All three students form their own groups. Total imbalance is 0. In the third sample, the total imbalance must be 0, so each student must work individually.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef long long LL;

int comp(const void * a, const void * b){
	return *((int*)a)-*((int*)b);
}

int a[200], n, k;
LL dp[2][201][1001];
const LL mod=1000000007;

LL get(int i, int num, int s){
	if(s>k || num<0) return 0;
	return dp[i&1][num][s];
}

int main(){
	int i, j, l, add;
	scanf("%d %d", &n, &k);
	for(i=0;i<n;++i) scanf("%d", a+i);
	for(i=0;i<=k;++i) dp[n&1][0][i]=1;
	for(i=1;i<=n;++i) for(j=0;j<=k;++j) dp[n&1][i][j]=0;
	qsort(a, n, sizeof(int), comp);
	for(i=n-1;i>0;--i){
		for(j=0;j<=n;++j){
			add=j*(a[i]-a[i-1]);
			for(l=0;l<=k;++l){
				dp[i&1][j][l]=(get(i+1, j+1, l+add)+j*get(i+1, j-1, l+add)+
						(j+1)*get(i+1, j, l+add))%mod;
			}
		}
	}
	printf("%I64d\n", (get(1, 1, 0)+get(1, 0, 0))%mod);
	return 0;
}


#include <stdio.h>

#define FOR(index, max_val) for(index = 0; index < max_val; index++)

int array[10000];
int indexes[10000];

int compare1(const void *a, const void *b) {
  return ( array[*(int*)a] - array[*(int*)b] );
}

int main() {
  int i;
  int n, a, b;

  scanf("%d%d%d", &n, &a, &b);
  FOR(i, n) {
    scanf("%d", &array[i]);
    indexes[i] = i;
    if(a < b) array[i] = -array[i];
  }

  if(a != b) {
    qsort(indexes, n, sizeof(int), compare1);
  }
  FOR(i, n) {
    if(i < a) array[indexes[i]] = 1;
    else array[indexes[i]] = 2;
  }

  FOR(i, n) {
    printf("%d", array[i]);
    if(i != n-1) printf(" ");
    else printf("\n");
  }

  return 0;
}

After the educational reform Polycarp studies only two subjects at school, Safety Studies and PE (Physical Education). During the long months of the fourth term, he received n marks in them. When teachers wrote a mark in the journal, they didn't write in what subject the mark was for, they just wrote the mark.Now it's time to show the journal to his strict parents. Polycarp knows that recently at the Parent Meeting the parents were told that he received a Safety Studies marks and b PE marks (a + b = n). Now Polycarp wants to write a subject's name in front of each mark so that:   there are exactly a Safety Studies marks,  there are exactly b PE marks,  the total average score in both subjects is maximum. An average subject grade is the sum of all marks in it, divided by the number of them. Of course, the division is performed in real numbers without rounding up or down. Polycarp aims to maximize the x1 + x2, where x1 is the average score in the first subject (Safety Studies), and x2 is the average score in the second one (Physical Education).

The first line contains an integer n (2 ≤ n ≤ 105), n is the number of marks in Polycarp's Journal. The second line contains two positive integers a, b (1 ≤ a, b ≤ n - 1, a + b = n). The third line contains a sequence of integers t1, t2, ..., tn (1 ≤ ti ≤ 5), they are Polycarp's marks.

Print the sequence of integers f1, f2, ..., fn, where fi (1 ≤ fi ≤ 2) is the number of a subject to which the i-th mark should be attributed. If there are several possible solutions, then print such that the sequence f1, f2, ..., fn is the smallest lexicographically. The sequence p1, p2, ..., pn is lexicographically less than q1, q2, ..., qn if there exists such j (1 ≤ j ≤ n) that pi = qi for all 1 ≤ i < j, аnd pj < qj.

NoteIn the first sample the average score in the first subject is equal to 4, and in the second one — to 4.5. The total average score is 8.5.

#include <stdio.h>

#define FOR(index, max_val) for(index = 0; index < max_val; index++)

int array[100010];
int indexes[100010];
int n;

int compare1(const void *a, const void *b) {
  return (array[*(int*)a] != array[*(int*)b])?(array[*(int*)a] - array[*(int*)b]):(*(int*)a - *(int*)b);
}

int compare2(const void *a, const void *b) {
  return (array[*(int*)a] != array[*(int*)b])?( - array[*(int*)a] + array[*(int*)b]):(*(int*)a - *(int*)b);
}

int main() {
  int i, j;
  int a, b;

  scanf("%d%d%d", &n, &a, &b);
  FOR(i, n) {
    scanf("%d", &array[i]);
    indexes[i] = i;
  }

  if(a > b) {
		qsort(indexes, n, sizeof(int), compare1);
  } else if(a < b) {
		qsort(indexes, n, sizeof(int), compare2);
	}
	//FOR(i, n) printf("%d ", indexes[i]);
	//printf("\n");
	for(i = 0; i < a; i++) array[indexes[i]] = 1;
	for(i = a; i < n; i++) array[indexes[i]] = 2;

  FOR(i, n-1) printf("%d ", array[i]);
  printf("%d\n", array[i]);

  return 0;
}


/*  problem B */

#include<stdio.h>
#include<stdlib.h>

int find(int base[][100], int  key[], int num,int k)
 {

 int i,j,flag=0;

 i=1;
 if(k>=0)
 for(i=0;i<=k;i++){

  for(j=0;j<num;j++){
  if(key[0]==base[i][j])
  {
  flag=1; /*found */
  break;
  }
 }
 if(flag==1) break;

 }

 if(i>k && !flag)
  {
   k++;
   for(j=0;j<num;j++)
   base[k][j]=key[j];
  }

  return k;

}

int intcomp(const void *p1,const void * p2)
{

return (*(int *)p1-*(int *)p2);

}

int sum_kmin(int * aux, int k,int length)
 {

 int i,sum=0;
 for(i=0;i<k;i++)
  sum+=aux[i];

 return sum;
 }

int sum_kmax(int * aux, int k, int length)
 {

 int i, sum=0;
 for(i=0;i<k;i++)
  sum+=aux[length-1-i];

 return sum;

 }

int min(int *aux, int length)
 {
 int i=0,min=aux[0];
 for(i=1;i<length;i++)
  if(aux[i]<min) min=aux[i];

 return min;

 }

int max(int * aux, int length)
 {

 int i=0,max=aux[0];
 for(i=1;i<length;i++)
  if(aux[i]>max) max=aux[i];

 return max;
 }

int main(void)
{

int marks[100][2]; 									/* vasya's marks */
int aux[100],maux[100],l;

int k,n,q,num,i,j,current=-1,length,sum,before;
double avg;
int cards[100][100];

scanf("%d%d",&n,&k);

for(i=0;i<n;i++){
scanf("%d",&marks[i][0]);
marks[i][1]=0;
}

num=n/k;
scanf("%d",&q);

l=0;
for(i=0;i<q;i++)
 {
 sum=0;
 for(j=0;j<num;j++){
 scanf("%d",&aux[j]);
 marks[aux[j]-1][1]=1;
 sum+=marks[aux[j]-1][0];
 }

 before=current;
current=find(cards,aux,num,current);
if(before!=current) maux[l++]=sum;
 }

j=0;
for(i=0;i<n;i++)
{
if(marks[i][1]==0) { aux[j++]=marks[i][0];}
}

qsort(aux,j,sizeof(int),intcomp);
length=j;

/*for(i=0;i<=current;i++){
 printf("\n");
 for(j=0;j<num;j++)
  printf("  %d",cards[i][j]);

 }*/

maux[l++]=sum_kmin(aux,num,length);
maux[l++]=sum_kmax(aux,num,length);

avg=(double)min(maux,l)/num;
printf("%.10lf ",avg);
avg=(double)max(maux,l)/num;
printf("%.10lf ",avg);

return 0;
}

Vasya is about to take his first university exam in about several minutes. And it's not just some ordinary exam, it's on mathematical analysis. Of course, right now Vasya can only think of one thing: what the result of his talk with the examiner will be...To prepare for the exam, one has to study proofs of n theorems. It is known that there will be k examination cards on the exam and each card contains  distinct theorems. Besides, no theorem is mentioned in more than one card (that is,  theorems won't be mentioned in any card). During the exam several students may get the same card.We do not know the exact way theorems are distributed by cards, however the students that took the exam before Vasya told him what theorems their cards contained. Vasya evaluates his level of proficiency in the i-th theorem by some number ai. The level of proficiency in some card is the average of the levels of proficiency in the theorems that are included in the card. Now Vasya wants to know the minimally and maximally possible levels of his proficiency in the card he gets on the exam. Vasya wants to determine it by the data he has collected from other students. Unfortunately, Vasya has no time left to do the math and he asked you to help him.

The first line contains two integers n and k (1 ≤ k ≤ n ≤ 100) — the number of theorems and the number of cards correspondingly. The second line contains n integers ai (0 ≤ ai ≤ 100), the i-th number (1 ≤ i ≤ n) corresponds to Vasya's proficiency in the i-th theorem. The third line contains number q (0 ≤ q ≤ 100) — the number of people that have taken the exam before Vasya. Each of the following q lines contains the description of a student's card:  integers from 1 to n inclusive. They are the numbers of theorems included in the card in the order in which they are enumerated in the input data. The numbers are given in an arbitrary order. It is guaranteed that the given cards are valid (that is, that all theorems in one card are different and that different people get cards that either don't contain the same theorems or coincide up to the theorems' permutation).

Print two real numbers, representing Vasya's minimum and maximum proficiency in the card he will get on the exam. The absolute or relative error should not exceed 10 - 6.

NoteLet's analyze the first sample. Vasya's proficiency in the cards whose content he already knows equals 6 and 15.5 correspondingly. The three theorems that are left are only enough to make one exam card. If we consider all possible variants of theorems included in the card we can see that in the best case scenario Vasya gets the card that contains theorems 4 and 7 (his proficiency would equal 15.5) and in the worst case scenario he gets theorems 3 and 5 (his proficiency would equal 5).The ⌊ x⌋ operation denotes taking integer part of real number x (rounding down).

/*  problem B */

#include<stdio.h>
#include<stdlib.h>

int find(int base[][100], int  key[], int num,int k)
 {

 int i,j,flag=0;

 i=1;
 if(k>=0)
 for(i=0;i<=k;i++){

  for(j=0;j<num;j++){
  if(key[0]==base[i][j])
  {
  flag=1; /*found */
  break;
  }
 }
 if(flag==1) break;

 }

 if(i>k && !flag)
  {
   k++;
   for(j=0;j<num;j++)
   base[k][j]=key[j];
  }

  return k;

}

int intcomp(const void *p1,const void * p2)
{

return (*(int *)p1-*(int *)p2);

}

int sum_kmin(int * aux, int k,int length)
 {

 int i,sum=0;
 for(i=0;i<k;i++)
  sum+=aux[i];

 return sum;
 }

int sum_kmax(int * aux, int k, int length)
 {

 int i, sum=0;
 for(i=0;i<k;i++)
  sum+=aux[length-1-i];

 return sum;

 }

int min(int *aux, int length)
 {
 int i=0,min=aux[0];
 for(i=1;i<length;i++)
  if(aux[i]<min) min=aux[i];

 return min;

 }

int max(int * aux, int length)
 {

 int i=0,max=aux[0];
 for(i=1;i<length;i++)
  if(aux[i]>max) max=aux[i];

 return max;
 }

int main(void)
{

int marks[100][2]; 									/* vasya's marks */
int aux[100],maux[100],l;

int k,n,q,num,i,j,current=-1,length,sum,before;
double avg;
int cards[100][100];

scanf("%d%d",&n,&k);

for(i=0;i<n;i++){
scanf("%d",&marks[i][0]);
marks[i][1]=0;
}

num=n/k;
scanf("%d",&q);

l=0;
for(i=0;i<q;i++)
 {
 sum=0;
 for(j=0;j<num;j++){
 scanf("%d",&aux[j]);
 marks[aux[j]-1][1]=1;
 sum+=marks[aux[j]-1][0];
 }

 before=current;
current=find(cards,aux,num,current);
if(before!=current) maux[l++]=sum;
 }

j=0;
if(current<k-1)
for(i=0;i<n;i++)
{
if(marks[i][1]==0) { aux[j++]=marks[i][0];}
}

qsort(aux,j,sizeof(int),intcomp);
length=j;

/*for(i=0;i<=current;i++){
 printf("\n");
 for(j=0;j<num;j++)
  printf("  %d",cards[i][j]);

 }*/

if(j){
maux[l++]=sum_kmin(aux,num,length);
maux[l++]=sum_kmax(aux,num,length);
}

avg=(double)min(maux,l)/num;
printf("%.10lf ",avg);
avg=(double)max(maux,l)/num;
printf("%.10lf ",avg);

return 0;
}


#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main(void){
  int type=0,i,j,k,l,m,n;
  char str[4][10001],last[4][10001];
  scanf("%d %d%*c",&n,&k);
  while(n--){
    for(i=0;i<4;i++) scanf("%s%*c",str[i]);
    for(m=0;m<4;m++){
      l=strlen(str[m]);
      for(i=0,j=0;i<l && j<k;i++){
	switch(str[m][l-i-1]){
	case 'a':
	case 'i':
	case 'u':
	case 'e':
	case 'o':
	  j++;
	  break;
	}
      }
      strcpy(last[m],str[m]+(l-i));
    }
    if(type==0){
      if(strcmp(last[0],last[1])==0 && strcmp(last[1],last[2])==0 && strcmp(last[2],last[3])==0) type=0;
      else if(strcmp(last[0],last[1])==0 && strcmp(last[2],last[3])==0) type=1;
      else if(strcmp(last[0],last[2])==0 && strcmp(last[1],last[3])==0) type=2;
      else if(strcmp(last[0],last[3])==0 && strcmp(last[1],last[2])==0) type=3;
      else type=-1;
    }else if(type==1){
      if(strcmp(last[0],last[1])==0 && strcmp(last[2],last[3])==0) type=1;
      else type=-1;
    }else if(type==2){
      if(strcmp(last[0],last[2])==0 && strcmp(last[1],last[3])==0) type=2;
      else type=-1;
    }else if(type==3){
      if(strcmp(last[0],last[3])==0 && strcmp(last[1],last[2])==0) type=3;
      else type=-1;
    }else break;
  }
  switch(type){
  case -1:
    puts("NO");
    break;
  case 0:
    puts("aaaa");
    break;
  case 1:
    puts("aabb");
    break;
  case 2:
    puts("abab");
    break;
  case 3:
    puts("abba");
    break;
  }
  return 0;
}

Vera adores poems. All the poems Vera knows are divided into quatrains (groups of four lines) and in each quatrain some lines contain rhymes.Let's consider that all lines in the poems consist of lowercase Latin letters (without spaces). Letters "a", "e", "i", "o", "u" are considered vowels.Two lines rhyme if their suffixes that start from the k-th vowels (counting from the end) match. If a line has less than k vowels, then such line can't rhyme with any other line. For example, if k = 1, lines commit and hermit rhyme (the corresponding suffixes equal it), and if k = 2, they do not rhyme (ommit ≠ ermit).Today on a literature lesson Vera learned that quatrains can contain four different schemes of rhymes, namely the following ones (the same letters stand for rhyming lines):   Clerihew (aabb);  Alternating (abab);  Enclosed (abba). If all lines of a quatrain pairwise rhyme, then the quatrain can belong to any rhyme scheme (this situation is represented by aaaa).If all quatrains of a poem belong to the same rhyme scheme, then we can assume that the whole poem belongs to this rhyme scheme. If in each quatrain all lines pairwise rhyme, then the rhyme scheme of the poem is aaaa. Let us note that it doesn't matter whether lines from different quatrains rhyme with each other or not. In other words, it is possible that different quatrains aren't connected by a rhyme.Vera got a long poem as a home task. The girl has to analyse it and find the poem rhyme scheme. Help Vera cope with the task.

The first line contains two integers n and k (1 ≤ n ≤ 2500, 1 ≤ k ≤ 5) — the number of quatrains in the poem and the vowel's number, correspondingly. Next 4n lines contain the poem. Each line is not empty and only consists of small Latin letters. The total length of the lines does not exceed 104. If we assume that the lines are numbered starting from 1, then the first quatrain contains lines number 1, 2, 3, 4; the second one contains lines number 5, 6, 7, 8; and so on.

Print the rhyme scheme of the poem as "aabb", "abab", "abba", "aaaa"; or "NO" if the poem does not belong to any of the above mentioned schemes.

NoteIn the last sample both quatrains have rhymes but finding the common scheme is impossible, so the answer is "NO".

2 1
day
may
sun
fun
test
hill
fest
thrill

[{'input': '1 1\r\nday\r\nmay\r\nsun\r\nfun\r\n', 'output': ['aabb\r\n']}, {'input': '1 1\r\nday\r\nmay\r\ngray\r\nway\r\n', 'output': ['aaaa\r\n']}, {'input': '2 1\r\na\r\na\r\na\r\na\r\na\r\na\r\ne\r\ne\r\n', 'output': ['aabb\r\n']}, {'input': '2 1\r\nday\r\nmay\r\nsun\r\nfun\r\ntest\r\nhill\r\nfest\r\nthrill\r\n', 'output': ['NO\r\n']}, {'input': '2 5\r\na\r\na\r\na\r\na\r\na\r\na\r\ne\r\ne\r\n', 'output': ['NO\r\n']}, {'input': '1 1\r\nrezwbgy\r\nxakgmv\r\njogezwbgy\r\napezwbgy\r\n', 'output': ['NO\r\n']}, {'input': '2 1\r\nnuqfxwrb\r\napqfkw\r\nuqfxwrb\r\nnhcuqfxwrb\r\nogkznwncmt\r\nevf\r\nogkznwncmt\r\nogkznwncmt\r\n', 'output': ['NO\r\n']}, {'input': '1 1\r\naawjvkxx\r\nawjvkxx\r\nxawjvkxx\r\nawjvkxx\r\n', 'output': ['aaaa\r\n']}, {'input': '2 2\r\nrhcujgxabk\r\nnjgdqpurul\r\nueoedt\r\ncpcfhbyvo\r\nzmfwnieog\r\npkpylassbf\r\nhrfeod\r\ncdwuil\r\n', 'output': ['NO\r\n']}, {'input': '2 1\r\nol\r\nol\r\nol\r\nzol\r\nek\r\nek\r\nek\r\nqek\r\n', 'output': ['aaaa\r\n']}, {'input': '3 2\r\nexdaoao\r\nrdwunurp\r\ndunurp\r\ntyqzuxao\r\ndupocgsps\r\nzsiravcm\r\nnqiravcm\r\nlnupocgsps\r\niwashk\r\neepkqcykbv\r\nyviwashk\r\neepkqcykbv\r\n', 'output': ['NO\r\n']}, {'input': '2 1\r\ndaihacbnhgfts\r\nsqihpntjvczkw\r\nmihpntjvczkw\r\nvyacbnhgfts\r\ntsvovdpqajmgvcj\r\ncexqkwrvctomb\r\njxbomb\r\ngnpajmgvcj\r\n', 'output': ['abba\r\n']}, {'input': '3 2\r\netba\r\ntfecetba\r\nzkitbgcuuy\r\nuuy\r\nbuxeoi\r\nmekxoi\r\nblviwoehy\r\niwoehy\r\njyfpaqntiz\r\nqvaqntiz\r\nhciak\r\niak\r\n', 'output': ['aabb\r\n']}, {'input': '4 3\r\niixxiojrrdytjcbkvymw\r\nbjqixxiojrrdytjcbkvymw\r\nogjixxiojrrdytjcbkvymw\r\nevixxpfxpgicpg\r\njkotitixiughfhphliuurx\r\ngyubkqtonejprfjzvqxbdpn\r\ndpudxfoqnhekjytbwiuurx\r\noubkqtonejprfjzvqxbdpn\r\npgzaendrxjhsfzjmijv\r\npomuaendrxjhsfzjmijv\r\nafyuyxueaendrxjhsfzjmijv\r\naendrxjhsfzjmijv\r\nyubweicj\r\ntbnsuxqigmxdfnmbipubweicj\r\nfuftydlmoo\r\nmdkuftydlmoo\r\n', 'output': ['NO\r\n']}, {'input': '5 2\r\nqurcmcbxyoddgyyccsmb\r\nlsdzsqoa\r\neurcmcbxyoddgyyccsmb\r\noa\r\nutyxmdhcvaclynmstwsx\r\nmkyycelbmkmdrilmbvr\r\nutyxmdhcvaclynmstwsx\r\nrduyelbmkmdrilmbvr\r\nhmguhvqswwciowwgu\r\nnoe\r\nzmyncuwrowwgu\r\nqrhymghavvbmigzsjoe\r\nbvofhknbzusykztlxwms\r\nbpbfmvjaimkdeddy\r\neofhknbzusykztlxwms\r\nmhivpkxkpazimkdeddy\r\negvywnhmfngllaknmn\r\nmblkvhenlggoftwjgk\r\nzegvywnhmfngllaknmn\r\ngrdenlggoftwjgk\r\n', 'output': ['abab\r\n']}, {'input': '7 3\r\nferwljzwakxedlgwl\r\noerwljzwakxedlgwl\r\nhyqombizhuhxedprb\r\netptjrizhuhxedprb\r\nurtuckar\r\ndkartmwramklcmi\r\nrurtuckar\r\nnurartmwramklcmi\r\niraziomsv\r\nsaziomsv\r\nbprapiqpayzurgij\r\nusyemayzurgij\r\nztstmeecvmkvuu\r\nquexlecvmkvuu\r\nrlhwecvmkvuu\r\nzecvmkvuu\r\niikymgbncljtub\r\nqiikymgbncljtub\r\nbcavhexqamyszgfya\r\nojexqamyszgfya\r\nieyxqinjinjv\r\nxtiudieyxqinjinjv\r\nthtceyxqinjinjv\r\nmuneyxqinjinjv\r\nwreae\r\nqylcjhjzfhteae\r\nozcjthgyuchqo\r\nfcjozcjthgyuchqo\r\n', 'output': ['NO\r\n']}, {'input': '16 1\r\ni\r\ni\r\ni\r\ni\r\ni\r\nu\r\ni\r\ni\r\no\r\na\r\na\r\no\r\na\r\ni\r\na\r\na\r\ni\r\ni\r\no\r\no\r\ni\r\ni\r\ni\r\ni\r\nu\r\nu\r\nu\r\nu\r\no\r\ne\r\ne\r\ne\r\no\r\ni\r\no\r\ni\r\na\r\na\r\na\r\na\r\nu\r\no\r\no\r\nu\r\ni\r\no\r\no\r\ni\r\na\r\na\r\ne\r\ne\r\na\r\na\r\na\r\na\r\na\r\no\r\na\r\na\r\nu\r\na\r\nu\r\nu\r\n', 'output': ['NO\r\n']}, {'input': '16 1\r\neb\r\neb\r\nfe\r\nce\r\ner\r\ner\r\new\r\new\r\nu\r\ncu\r\nu\r\nu\r\nud\r\nik\r\nud\r\nik\r\nve\r\niw\r\niw\r\nne\r\nel\r\nob\r\nel\r\nob\r\no\r\neo\r\no\r\nyo\r\nav\r\nav\r\nei\r\nmi\r\nu\r\noh\r\noh\r\nzu\r\niw\r\niw\r\na\r\nma\r\ni\r\nu\r\nku\r\ngi\r\nac\r\no\r\no\r\nac\r\ni\r\ner\r\nai\r\ner\r\nyu\r\nuf\r\nuf\r\nhu\r\nef\r\nef\r\nef\r\nef\r\nmu\r\nu\r\nqe\r\nie\r\n', 'output': ['NO\r\n']}, {'input': '25 1\r\nw\r\ni\r\nv\r\nx\r\nh\r\ns\r\nz\r\ny\r\no\r\nn\r\nh\r\ni\r\nf\r\nf\r\ny\r\nr\r\nb\r\nu\r\no\r\np\r\nz\r\nh\r\nt\r\no\r\nw\r\nx\r\nh\r\no\r\nj\r\ny\r\nw\r\nj\r\ny\r\nh\r\nh\r\nr\r\ns\r\nb\r\ny\r\nr\r\nw\r\no\r\nl\r\nl\r\nh\r\nh\r\nw\r\nu\r\na\r\nv\r\no\r\nx\r\nd\r\nw\r\nc\r\nf\r\ni\r\ne\r\nj\r\nq\r\nk\r\na\r\ne\r\nl\r\nw\r\nm\r\nf\r\na\r\nc\r\na\r\nb\r\nf\r\nj\r\nb\r\nx\r\ni\r\nx\r\ne\r\nu\r\nh\r\nm\r\no\r\ni\r\nq\r\nm\r\nk\r\nn\r\nd\r\nl\r\np\r\nc\r\nw\r\nu\r\nz\r\nc\r\nk\r\ng\r\ny\r\nj\r\ny\r\n', 'output': ['NO\r\n']}, {'input': '1 1\r\ne\r\ne\r\ne\r\ne\r\n', 'output': ['aaaa\r\n']}, {'input': '1 1\r\na\r\ne\r\ne\r\ne\r\n', 'output': ['NO\r\n']}, {'input': '1 1\r\ne\r\na\r\ne\r\ne\r\n', 'output': ['NO\r\n']}, {'input': '1 1\r\na\r\na\r\ne\r\ne\r\n', 'output': ['aabb\r\n']}, {'input': '1 1\r\ne\r\ne\r\na\r\ne\r\n', 'output': ['NO\r\n']}, {'input': '1 1\r\na\r\ne\r\na\r\ne\r\n', 'output': ['abab\r\n']}, {'input': '1 1\r\ne\r\na\r\na\r\ne\r\n', 'output': ['abba\r\n']}, {'input': '1 1\r\na\r\na\r\na\r\ne\r\n', 'output': ['NO\r\n']}, {'input': '1 1\r\ne\r\ne\r\ne\r\na\r\n', 'output': ['NO\r\n']}, {'input': '1 1\r\na\r\ne\r\ne\r\na\r\n', 'output': ['abba\r\n']}, {'input': '1 1\r\ne\r\na\r\ne\r\na\r\n', 'output': ['abab\r\n']}, {'input': '1 1\r\na\r\na\r\ne\r\na\r\n', 'output': ['NO\r\n']}, {'input': '1 1\r\ne\r\ne\r\na\r\na\r\n', 'output': ['aabb\r\n']}, {'input': '1 1\r\na\r\ne\r\na\r\na\r\n', 'output': ['NO\r\n']}, {'input': '1 1\r\ne\r\na\r\na\r\na\r\n', 'output': ['NO\r\n']}, {'input': '1 1\r\na\r\na\r\na\r\na\r\n', 'output': ['aaaa\r\n']}, {'input': '1 2\r\neraub\r\nbee\r\naab\r\nttbee\r\n', 'output': ['NO\r\n']}, {'input': '10 1\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\na\r\ny\r\n', 'output': ['NO\r\n']}, {'input': '1 2\r\neeereaatktb\r\nbee\r\niaattb\r\nottbee\r\n', 'output': ['NO\r\n']}, {'input': '1 1\r\nab\r\nac\r\nad\r\naf\r\n', 'output': ['NO\r\n']}, {'input': '1 1\r\nar\r\nat\r\nay\r\naw\r\n', 'output': ['NO\r\n']}, {'input': '2 1\r\na\r\ne\r\na\r\ne\r\na\r\na\r\na\r\na\r\n', 'output': ['abab\r\n']}, {'input': '1 1\r\na\r\ne\r\na\r\ni\r\n', 'output': ['NO\r\n']}, {'input': '1 1\r\na\r\ne\r\na\r\ne\r\n', 'output': ['abab\r\n']}, {'input': '1 1\r\nabbbbbbbbbbbbbbbbcbbbbbbbbbbbbbbbb\r\nabbbbbbbbbbbbbbbbfbbbbbbbbbbbbbbbb\r\nabbbbbbbbbbbbbbbbxbbbbbbbbbbbbbbbb\r\nabbbbbbbbbbbbbbbbdbbbbbbbbbbbbbbbb\r\n', 'output': ['NO\r\n']}, {'input': '2 1\r\na\r\ne\r\ne\r\na\r\na\r\na\r\na\r\na\r\n', 'output': ['abba\r\n']}, {'input': '1 1\r\nbug\r\nsuy\r\nluh\r\ngut\r\n', 'output': ['NO\r\n']}, {'input': '1 1\r\nam\r\nat\r\nan\r\nag\r\n', 'output': ['NO\r\n']}, {'input': '2 1\r\na\r\na\r\ne\r\ne\r\na\r\na\r\na\r\na\r\n', 'output': ['aabb\r\n']}, {'input': '1 4\r\naieoabcd\r\naeioabcd\r\naoeiabcd\r\naoieabcd\r\n', 'output': ['NO\r\n']}, {'input': '1 2\r\naec\r\naed\r\naek\r\naem\r\n', 'output': ['NO\r\n']}, {'input': '1 1\r\nar\r\nab\r\nak\r\naz\r\n', 'output': ['NO\r\n']}, {'input': '2 1\r\na\r\na\r\na\r\na\r\na\r\nb\r\nb\r\nb\r\n', 'output': ['NO\r\n']}]


Polycarp loves geometric progressions — he collects them. However, as such progressions occur very rarely, he also loves the sequences of numbers where it is enough to delete a single element to get a geometric progression.In this task we shall define geometric progressions as finite sequences of numbers a1, a2, ..., ak, where ai = c·bi - 1 for some real numbers c and b. For example, the sequences [2, -4, 8], [0, 0, 0, 0], [199] are geometric progressions and [0, 1, 2, 3] is not.Recently Polycarp has found a sequence and he can't classify it. Help him to do it. Determine whether it is a geometric progression. If it is not, check if it can become a geometric progression if an element is deleted from it.

The first line contains an integer n (1 ≤ n ≤ 105) — the number of elements in the given sequence. The second line contains the given sequence. The numbers are space-separated. All the elements of the given sequence are integers and their absolute value does not exceed 104.

Print 0, if the given sequence is a geometric progression. Otherwise, check if it is possible to make the sequence a geometric progression by deleting a single element. If it is possible, print 1. If it is impossible, print 2.