Alice and Bob decided to eat some fruit. In the kitchen they found a large bag of oranges and apples. Alice immediately took an orange for herself, Bob took an apple. To make the process of sharing the remaining fruit more fun, the friends decided to play a game. They put multiple cards and on each one they wrote a letter, either 'A', or the letter 'B'. Then they began to remove the cards one by one from left to right, every time they removed a card with the letter 'A', Alice gave Bob all the fruits she had at that moment and took out of the bag as many apples and as many oranges as she had before. Thus the number of oranges and apples Alice had, did not change. If the card had written letter 'B', then Bob did the same, that is, he gave Alice all the fruit that he had, and took from the bag the same set of fruit. After the last card way removed, all the fruit in the bag were over.You know how many oranges and apples was in the bag at first. Your task is to find any sequence of cards that Alice and Bob could have played with.

The first line of the input contains two integers, x, y (1 ≤ x, y ≤ 1018, xy > 1) — the number of oranges and apples that were initially in the bag.

Print any sequence of cards that would meet the problem conditions as a compressed string of characters 'A' and 'B. That means that you need to replace the segments of identical consecutive characters by the number of repetitions of the characters and the actual character. For example, string AAABAABBB should be replaced by string 3A1B2A3B, but cannot be replaced by 2A1A1B2A3B or by 3AB2A3B. See the samples for clarifications of the output format. The string that you print should consist of at most 106 characters. It is guaranteed that if the answer exists, its compressed representation exists, consisting of at most 106 characters. If there are several possible answers, you are allowed to print any of them. If the sequence of cards that meet the problem statement does not not exist, print a single word Impossible.

NoteIn the first sample, if the row contained three cards with letter 'B', then Bob should give one apple to Alice three times. So, in the end of the game Alice has one orange and three apples, and Bob has one apple, in total it is one orange and four apples.In second sample, there is no answer since one card is not enough for game to finish, and two cards will produce at least three apples or three oranges.In the third sample, cards contain letters 'AB', so after removing the first card Bob has one orange and one apple, and after removal of second card Alice has two oranges and one apple. So, in total it is three oranges and two apples.

#include<stdio.h>
typedef long long unsigned llu;
typedef long long ll;
typedef unsigned u;
llu G(llu a,llu b){return b?G(b,a%b):a;}
ll lx,ly;
ll egcd(ll a,ll b)
{
	long long r,q,x=0,y=1;lx=1;ly=0;
	u ee=1;
	while(b!=0)
	{
		q=a/b;
		r=b;b=a%b;a=r;
		r=x;x=lx-q*x;lx=r;
		r=y;y=ly-q*y;ly=r;
		if((ee^=1)&&(q-(b==0)))printf("%I64iB",q-(b==0));
		else if((q-(b==0)))printf("%I64iA",q-(b==0));
	}
	return a;
}
int main()
{
	llu a,b;u i,j,k;
	scanf("%I64u%I64u",&a,&b);
	if(G(a,b)!=1){printf("Impossible\n");return 0;}
	egcd(a,b);
	putchar('\n');
	return 0;
}

#include <stdio.h>

long long gcd(long long a,long long b){
    return b?gcd(b,a%b):a;
}
int main()
{
    long long x,y;
    scanf("%I64d%I64d",&x,&y);
    if(x==y){
        printf("Impossible\n");
        return 0;
    }
    else if(gcd(x,y)!=1){
        printf("Impossible\n");
        return 0;
    }
    else if(x==1){
        printf("%I64dB\n",y-1);
        return 0;
    }
    else if(y==1){
        printf("%I64dA\n",x-1);
        return 0;
    }
    else{
        while(x!=0 && y!=0){
            if(x==1){
                printf("%I64dB",y-1);
                break;
            }
            else if(y==1){
                printf("%I64dA",x-1);
                break;
            }
            else if(x>y){
                printf("%I64dA",x/y);
                x=x%y;
            }
            else if(x<y){
                printf("%I64dB",y/x);
                y=y%x;
            }

        }
    }

    return 0;
}

#include <stdio.h>

long long gcd(long long a,long long b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
int main()
{
    long long x,y,test;
    scanf("%I64d %I64d",&x,&y);
    test=gcd(x,y);
    //printf("%I64d",test);
    if(test>1)
    {
        printf("Impossible");
        return 0;
    }
    else
    {
        while(x && y)
        {
            if(x>y)
            {
                printf("%I64dA",(x-1)/y);
                x=x%y;
            }
            else
            {
                printf("%I64dB",(y-1)/x);
                y=y%x;
            }
        }
    }
    return 0;
}

/* practice with Dukkha */
#include <stdio.h>

long long gcd(long long a, long long b) {
	return b == 0 ? a : gcd(b, a % b);
}

int main() {
	static char cc[1048576], *s = cc;
	long long x, y;

	scanf("%lld%lld", &x, &y);
	if (gcd(x, y) != 1) {
		printf("Impossible\n");
		return 0;
	}
	while (x > 1 || y > 1) {
		long long k;
		char c;

		if (x > y) {
			k = x / y;
			if (y == 1)
				k--;
			x %= y;
			c = 'A';
		} else {
			k = y / x;
			if (x == 1)
				k--;
			y %= x;
			c = 'B';
		}
		s += sprintf(s, "%lld%c", k, c);
	}
	printf("%s\n", cc);
	return 0;
}

#include <stdio.h>

long long gcd(long long a,long long b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
int main()
{
    long long x,y,test;
    scanf("%I64d %I64d",&x,&y);
    test=gcd(x,y);
    if(test>=1)
        printf("Impossible");
    else
    {
        while(x && y)
        {
            if(x>y)
            {
                printf("%I64dA",(x-1)/y);
                x=x%y;
            }
            else
            {
                printf("%I64dB",(y-1)/x);
                y=y%x;
            }
        }
    }
    return 0;
}

#include <stdio.h>

long long gcd(long a,long b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
int main()
{
    long long x,y,test;
    scanf("%I64d %I64d",&x,&y);
    test=gcd(x,y);
    if(test>1)
        printf("Impossible");
    else
    {
        while(x && y)
        {
            if(x>y)
            {
                printf("%I64dA",(x-1)/y);
                x=x%y;
            }
            else
            {
                printf("%I64dB",(y-1)/x);
                y=y%x;
            }
        }
    }
    return 0;
}

#include <stdio.h>

long long gcd(long long a,long long b)
{
    if(a%b==0)
        return b;
    else
        return gcd(b,a%b);
}
int main()
{
    long long x,y,test;
    scanf("%I64d %I64d",&x,&y);
    test=gcd(x,y);
    printf("%I64d",test);
    if(test>1)
    {
        printf("Impossible");
        return 0;
    }
    else
    {
        while(x && y)
        {
            if(x>y)
            {
                printf("%I64dA",(x-1)/y);
                x=x%y;
            }
            else
            {
                printf("%I64dB",(y-1)/x);
                y=y%x;
            }
        }
    }
    return 0;
}


Life is not easy for the perfectly common variable named Vasya. Wherever it goes, it is either assigned a value, or simply ignored, or is being used!Vasya's life goes in states of a program. In each state, Vasya can either be used (for example, to calculate the value of another variable), or be assigned a value, or ignored. Between some states are directed (oriented) transitions.A path is a sequence of states v1, v2, ..., vx, where for any 1 ≤ i < x exists a transition from vi to vi + 1.Vasya's value in state v is interesting to the world, if exists path p1, p2, ..., pk such, that pi = v for some i (1 ≤ i ≤ k), in state p1 Vasya gets assigned a value, in state pk Vasya is used and there is no state pi (except for p1) where Vasya gets assigned a value.Help Vasya, find the states in which Vasya's value is interesting to the world.

The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 105) — the numbers of states and transitions, correspondingly. The second line contains space-separated n integers f1, f2, ..., fn (0 ≤ fi ≤ 2), fi described actions performed upon Vasya in state i: 0 represents ignoring, 1 — assigning a value, 2 — using. Next m lines contain space-separated pairs of integers ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi), each pair represents the transition from the state number ai to the state number bi. Between two states can be any number of transitions.

Print n integers r1, r2, ..., rn, separated by spaces or new lines. Number ri should equal 1, if Vasya's value in state i is interesting to the world and otherwise, it should equal 0. The states are numbered from 1 to n in the order, in which they are described in the input.

NoteIn the first sample the program states can be used to make the only path in which the value of Vasya interests the world, 1  2  3  4; it includes all the states, so in all of them Vasya's value is interesting to the world.The second sample the only path in which Vasya's value is interesting to the world is , — 1  3; state 2 is not included there.In the third sample we cannot make from the states any path in which the value of Vasya would be interesting to the world, so the value of Vasya is never interesting to the world.

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

typedef struct node{
        int v;
        struct node *next;
} NODE;

char f[100000];

void dfs(int n, NODE *e[], char v[], int mode){
     v[n] = 1;
     if (!mode && f[n] == 1)return;
     NODE *tmp = e[n];
     while (tmp){
           if (!v[tmp->v] && !(mode && f[tmp->v] == 1))
              dfs(tmp->v, e, v, mode);
           tmp = tmp->next;
     }
     return;
}

int main(){
    int n, m, i, j, x, y;
    char f1 = 0, f2 = 0, v1[100000] = {}, v2[100000] = {};
    NODE *e[100000] = {}, *b[100000] = {}, *tmp;
    scanf("%d%d", &n, &m);
    for (i = 0 ; i < n ; ++i){
        scanf("%d", f + i);
        if (f[i] == 1)f1 = 1;
        else if (f[i] == 2)f2 = 1;
    }
    if (!f1 || !f2){
       while (n--)puts("0");
       return 0;
    }

    for (i = 0 ; i < m ; ++i){
        scanf("%d%d", &x, &y);
        --x, --y;
        tmp = calloc(1, sizeof(NODE));
        tmp->v = y, tmp->next = e[x], e[x] = tmp;
        tmp = calloc(1, sizeof(NODE));
        tmp->v = x, tmp->next = b[y], b[y] = tmp;
    }
    for (i = 0 ; i < n ; ++i)
        if (f[i] == 1)
           dfs(i, e, v1, 1);
    for (i = 0 ; i < n ; ++i)
        if (f[i] == 2)
           dfs(i, b, v2, 0);
    for (i = 0 ; i < n ; ++i)
        printf("%d\n", v1[i] && v2[i]);
    scanf(" ");
    return 0;
}

#include <stdio.h>

#define maxN 100010

struct {
    int to;
    int nextID;
} E[maxN], R[maxN];

int LE[maxN], RE[maxN];
int t[maxN], afs[maxN], lfs[maxN];

void dfs( int s ) {
    afs[s] = 1;

    int ID;
    for( ID = LE[s]; ID != 0; ID = E[ID].nextID )
	if( !afs[E[ID].to] ) dfs(E[ID].to);
}

void ofs( int s ) {
    lfs[s] = 1;

    if( t[s] == 1 ) return;

    int ID;
    for( ID = RE[s]; ID != 0; ID = R[ID].nextID )
	if( !lfs[R[ID].to] ) ofs(R[ID].to);
}

int main( void ) {
    int N, M;
    scanf("%i %i", &N, &M);

    int i;
    for( i = 1; i <= N; i++ )
	scanf("%i", &t[i]);

    for( i = 1; i <= M; i++ ) {
	int u, v;
	scanf("%i %i", &u, &v);

	E[i].to = v;
	E[i].nextID = LE[u];
	LE[u] = i;

	R[i].to = u;
	R[i].nextID = RE[v];
	RE[v] = i;
    }

    for( i = 1; i <= N; i++ )
	if( t[i] == 1 ) dfs(i);

    for( i = 1; i <= N; i++ )
	if( t[i] == 2 ) ofs(i);

    for( i = 1; i <= N; i++ )
	printf("%i%c", (afs[i] && lfs[i]) ? 1 : 0, (i == N) ? '\n' : ' ');

    return 0;
}

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

typedef struct node{
        int v;
        struct node *next;
} NODE;

void dfs(int n, NODE *e[], char v[]){
     NODE *tmp = e[n];
     v[n] = 1;
     while (tmp){
           if (!v[tmp->v])
              dfs(tmp->v, e, v);
           tmp = tmp->next;
     }
     return;
}

int main(){
    int n, m, i, j, x, y;
    char f[100000], f1 = 0, f2 = 0, v1[100000] = {}, v2[100000] = {};
    NODE *e[100000] = {}, *b[100000] = {}, *tmp;
    scanf("%d%d", &n, &m);
    for (i = 0 ; i < n ; ++i){
        scanf("%d", f + i);
        if (f[i] == 1)f1 = 1;
        else if (f[i] == 2)f2 = 1;
    }
    if (!f1 || !f2){
       while (n--)puts("0");
       return 0;
    }

    for (i = 0 ; i < m ; ++i){
        scanf("%d%d", &x, &y);
        --x, --y;
        tmp = calloc(1, sizeof(NODE));
        tmp->v = y, tmp->next = e[x], e[x] = tmp;
        tmp = calloc(1, sizeof(NODE));
        tmp->v = x, tmp->next = b[y], b[y] = tmp;
    }
    for (i = 0 ; i < n ; ++i)
        if (f[i] == 1)
           dfs(i, e, v1);
    for (i = 0 ; i < n ; ++i)
        if (f[i] == 2)
           dfs(i, b, v2);
    for (i = 0 ; i < n ; ++i)
        printf("%d\n", v1[i] && v2[i]);
    return 0;
}

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

typedef struct node{
        int v;
        struct node *pre, *next;
} NODE;

int e[100000][2], v[100001] = {};
char s[100001] = {}, f[100001];
NODE pre[100000] = {}, *prep[100001];

int cmp(const void *a, const void *b){
    int *x = (int*)a, *y = (int*)b;
    if (x[0] < y[0])return -1;
    if (x[0] > y[0])return 1;
    if (x[1] < y[1])return -1;
    if (x[1] > y[1])return 1;
    return 0;
}

void fill(int now, NODE *prep[]){
     do {/**/printf(" f: %d %d\n", now, prep[now]->v);
        if (!prep[now]->v)return;
        if (s[prep[now]->v])continue;
        s[prep[now]->v] = 1;
        fill(prep[now]->v, prep);
     }while (prep[now]->pre && (prep[now] = prep[now]->pre));
     return;
}

int main(){
    int n, m, i, j;
    char f1 = 0, f2 = 0;

    scanf("%d%d", &n, &m);
    for (i = 1 ; i <= n ; ++i){
        scanf("%d", f + i);
        if (f[i] == 1)f1 = 1;
        else if (f[i] == 2)f2 = 1;
    }
    if (!f1 || !f2){
       while (n--)puts("0");
       return 0;
    }

    for (i = 0 ; i < m ; ++i)
        scanf("%d%d", &e[i][0], &e[i][1]);
    qsort(e, m, sizeof(int) * 2, cmp);
    for (i = 0, j = 1 ; i < m ; ++i)
        while (e[i][0] > j)v[j++] = i;
    while (j <= n)v[j++] = m;

    for (i = 1 ; i <= n ; ++i){
        if (f[i] != 1)continue;
        int l, r, q[100000];
        char c[100001] = {};
        memset(pre, 0, sizeof(pre));
        q[0] = i, c[i] = 1;
        prep[i] = &pre[0];
        for (l = 0, r = 1 ; l < r ; ++l){
            for (j = v[q[l] - 1] ; j < v[q[l]] ; ++j){
                if (f[e[j][1]] == 1)continue;
                if (c[e[j][1]]){
                   /**/printf("c: %d %d %d %d\n", l, r, e[j][0], e[j][1]);
                   NODE *tmp = prep[e[j][1]];
                   do {
                      if (tmp->v == q[l])break;
                   }while (tmp->pre && (tmp = tmp->pre));
                   if (tmp->v == q[l])continue;
                   prep[e[j][1]]->next = calloc(1, sizeof(NODE));
                   prep[e[j][1]]->next->pre = prep[e[j][1]];
                   prep[e[j][1]] = prep[e[j][1]]->next;
                   prep[e[j][1]]->v = q[l];
                }
                else {
                   /**/printf("d: %d %d %d %d\n", l, r, e[j][0], e[j][1]);
                     c[e[j][1]] = 1;
                     pre[r].v = q[l];
                     prep[e[j][1]] = &pre[r];
                     q[r++] = e[j][1];
                }
                if (s[e[j][1]] || f[e[j][1]] == 2){
                  /**/printf("s: %d %d %d %d\n", l, r, e[j][0], e[j][1]);
                   s[e[j][1]] = 1;
                   fill(e[j][1], prep);
                }
            }
        }
    }
    for (i = 1 ; i <= n ; ++i)
        printf("%d\n", s[i]);
    return 0;
}

#include <stdio.h>

#define maxN 100010

struct {
    int to;
    int nextID;
} E[maxN];

int LE[maxN], P[maxN];
int t[maxN], afs[maxN], lfs[maxN];

void dfs( int s ) {
    afs[s] = 1;

    int ID;
    for( ID = LE[s]; ID != 0; ID = E[ID].nextID )
	if( !afs[E[ID].to] ) {
	    P[E[ID].to] = s;
	    dfs(E[ID].to);
	}
}

void ofs( int s ) {
    while( (s != 0) && (lfs[s] == 0) ) {
	lfs[s] = 1;
	s = P[s];

	if( t[s] == 1 ) break;
    }

    if( t[s] == 1 ) lfs[s] = 1;
}

int main( void ) {
    int N, M;
    scanf("%i %i", &N, &M);

    int i;
    for( i = 1; i <= N; i++ )
	scanf("%i", &t[i]);

    for( i = 1; i <= M; i++ ) {
	int u, v;
	scanf("%i %i", &u, &v);

	E[i].to = v;
	E[i].nextID = LE[u];
	LE[u] = i;
    }

    for( i = 1; i <= N; i++ )
	if( t[i] == 1 ) dfs(i);

    for( i = 1; i <= N; i++ )
	if( t[i] == 2 ) ofs(i);

    for( i = 1; i <= N; i++ )
	printf("%i%c", (afs[i] && lfs[i]) ? 1 : 0, (i == N) ? '\n' : ' ');

    return 0;
}

#include <stdio.h>

#define maxN 100010

struct {
    int to;
    int nextID;
} E[maxN];

int LE[maxN], P[maxN];
int t[maxN], afs[maxN], lfs[maxN];

void dfs( int s ) {
    afs[s] = 1;

    int ID;
    for( ID = LE[s]; ID != 0; ID = E[ID].nextID )
	if( !afs[E[ID].to] ) {
	    P[E[ID].to] = s;
	    dfs(E[ID].to);
	}
}

void ofs( int s ) {

    while( s != 0 && !lfs[s] ) {
	lfs[s] = 1;
	s = P[s];
    }
}

int main( void ) {
    int N, M;
    scanf("%i %i", &N, &M);

    int i;
    for( i = 1; i <= N; i++ )
	scanf("%i", &t[i]);

    for( i = 1; i <= M; i++ ) {
	int u, v;
	scanf("%i %i", &u, &v);

	E[i].to = v;
	E[i].nextID = LE[u];
	LE[u] = i;
    }

    for( i = 1; i <= N; i++ )
	if( t[i] == 1 ) dfs(i);

    for( i = 1; i <= N; i++ )
	if( t[i] == 2 && afs[i] ) ofs(i);

    for( i = 1; i <= N; i++ )
	printf("%i%c", (afs[i] && lfs[i]) ? 1 : 0, (i == N) ? '\n' : ' ');

    return 0;
}


The stardate is 1977 and the science and art of detecting Death Stars is in its infancy. Princess Heidi has received information about the stars in the nearby solar system from the Rebel spies and now, to help her identify the exact location of the Death Star, she needs to know whether this information is correct. Two rebel spies have provided her with the maps of the solar system. Each map is an N × N grid, where each cell is either occupied by a star or empty. To see whether the information is correct, Heidi needs to know whether the two maps are of the same solar system, or if possibly one of the spies is actually an Empire double agent, feeding her false information.Unfortunately, spies may have accidentally rotated a map by 90, 180, or 270 degrees, or flipped it along the vertical or the horizontal axis, before delivering it to Heidi. If Heidi can rotate or flip the maps so that two of them become identical, then those maps are of the same solar system. Otherwise, there are traitors in the Rebel ranks! Help Heidi find out.

The first line of the input contains one number N (1 ≤ N ≤ 10) – the dimension of each map. Next N lines each contain N characters, depicting the first map: 'X' indicates a star, while 'O' indicates an empty quadrant of space. Next N lines each contain N characters, depicting the second map in the same format.

The only line of output should contain the word Yes if the maps are identical, or No if it is impossible to match them by performing rotations and translations.

4
XOOO
XXOO
OOOO
XXXX
XOOO
XOOO
XOXO
XOXX

NoteIn the first test, you can match the first map to the second map by first flipping the first map along the vertical axis, and then by rotating it 90 degrees clockwise.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
char a[20][11],b[20][11];
int n;
int check()
{
    int k=0;
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<n; j++)
        {
            if (a[n+i][j]!=b[i][j])
            {
                k=1;
                break;
            }
        }
    }
    if (k==0)
    {
        printf("YES");
        return 10;
    }
    else
        return 0;
}
void ud()
{
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<n; j++)
        {
            b[n-i-1][j]=a[i][j];
        }
    }
}
void lr()
{
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<n; j++)
        {
            b[i][n-j-1]=a[i][j];
        }
    }
}
void rot()
{
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<n; j++)
        {
            b[n-j-1][i]=a[i][j];
        }
    }
}
void match()
{
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<n; j++)
        {
            a[i][j]=b[i][j];
        }
    }
}
int main()
{
    scanf("%d",&n);
    for (int i=0; i<n*2; i++)
    {
        scanf("%s",a[i]);
    }
    for (int i=0; i<4; i++)
    {
        rot();
        match();
        if (check()==10)
            return 0;
        ud();
        match();
        if (check()==10)
            return 0;
        lr();
        match();
        if (check()==10)
            return 0;
        ud();
        match();
        if (check()==10)
            return 0;
        lr();
        match();
        if (check()==10)
            return 0;
    }
    printf("NO");
    return 0;
}

#include<stdio.h>

int main()
{
    int a,i,j,k,flag,count=0;
    scanf("%d",&a);
    char arr[a][a],oth[a][a],change[a][a],matha_nosto[a][a];
    for(i=0; i<a; i++)
    {
        for(j=0; j<a; j++)
            scanf(" %c",&arr[i][j]);
    }

    for(i=0; i<a; i++)
    {
        for(j=0; j<a; j++)
            scanf(" %c",&oth[i][j]);
    }

    for(i=0; i<a; i++)
    {
        for(j=0; j<a; j++)
            change[j][a-1-i]=oth[i][j];
    }
    for(k=0; k<a+6; k++)
    {
        for(i=0; i<a; i++)
        {
            for(j=0; j<a; j++)
            {
                if(arr[i][j]==change[i][j])
                    flag=1;
                else
                {
                    flag=0;
                    break;
                }
            }
            if(flag==0)
                break;
        }
        if(flag==1)
        {
            printf("YES");
            return 0;
        }
        else
        {
            for(i=0; i<a; i++)
            {
                for(j=0; j<a; j++)
                {
                    if(arr[i][j]==change[i][a-1-j])
                        flag=1;
                    else
                    {
                        flag=0;
                        break;
                    }
                }
                if(flag==0)
                    break;
            }
            if(flag==1)
            {
                printf("YES");
                return 0;
            }
            else
            {
                for(i=0; i<a; i++)
                {
                    for(j=0; j<a; j++)
                        oth[i][j]=change[i][j];
                }

                for(i=0; i<a; i++)
                {
                    for(j=0; j<a; j++)
                        change[j][a-1-i]=oth[i][j];
                }
            }
        }

    }
    printf("NO");
    return 0;
}


Fox Ciel has a robot on a 2D plane. Initially it is located in (0, 0). Fox Ciel code a command to it. The command was represented by string s. Each character of s is one move operation. There are four move operations at all:  'U': go up, (x, y)  →  (x, y+1);  'D': go down, (x, y)  →  (x, y-1);  'L': go left, (x, y)  →  (x-1, y);  'R': go right, (x, y)  →  (x+1, y). The robot will do the operations in s from left to right, and repeat it infinite times. Help Fox Ciel to determine if after some steps the robot will located in (a, b).

The first line contains two integers a and b, ( - 109 ≤ a, b ≤ 109). The second line contains a string s (1 ≤ |s| ≤ 100, s only contains characters 'U', 'D', 'L', 'R') — the command.

Print "Yes" if the robot will be located at (a, b), and "No" otherwise.

NoteIn the first and second test case, command string is "RU", so the robot will go right, then go up, then right, and then up and so on.The locations of its moves are (0, 0)  →  (1, 0)  →  (1, 1)  →  (2, 1)  →  (2, 2)  →  ...So it can reach (2, 2) but not (1, 2).

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

#define REP(i,n) for(int i=0;i<(int)n;++i)
typedef long long ll;

const int dy[] = {1,0,-1,0};
const int dx[] = {0,1,0,-1};
const char dir[] = "URDL";

int d[100];
char s[105];
int vx[105], vy[105];
int ix, iy;
int main() {
	int a,b;
	while(scanf("%d%d", &a, &b) == 2) {
		scanf("%s",s);
		ll y=0,x=0;
		int n = strlen(s);
		REP(i,n) {
			d[i] = strchr(dir, s[i]) - dir;
		}
		ix = 0;
		iy = 0;
		vx[ix++] = 0;
		vy[iy++] = 0;
		REP(i,n) {
			y += dy[d[i]];
			x += dx[d[i]];
			vy[iy++] = y;
			vx[ix++] = x;
		}
		ll Y = y;
		ll X = x;
		int ok = 0;
		REP(i,n) {
			REP(j,n) {
				ll yy = vy[j];
				ll xx = vx[j];
				ll y = b-yy;
				ll x = a-xx;
				if (Y && y % Y == 0 && y/Y>=0) {
					if (X*(y/Y) == x) {
						ok = 1;
					}
				}
				if (Y == 0 && y == 0) {
					if (x == 0 || (X && x % X == 0 && x/X>=0)) {
						ok = 1;
					}
				}
			}
		}
		printf("%s\n",ok?"Yes":"No");
	}
}

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

int main(){
    int sx=0,sy=0,p=1,a,b,i,num_len,dx,dy;
    char s[110];
    int pos_x[110];
    int pos_y[110];
    scanf("%d %d %s",&a,&b,s);
    pos_x[0]=pos_y[0]=0;
    num_len=strlen(s);
    for(i=0;i<num_len;i++){
        switch(s[i]){
        case 'U':
            sy++;
            break;
        case 'D':
            sy--;
            break;
        case 'R':
            sx++;
            break;
        case 'L':
            sx--;
            break;
        default: ;
        }
        pos_x[i+1]=sx;
        pos_y[i+1]=sy;
    }
    dx=pos_x[num_len];
    dy=pos_y[num_len];
    for(i=0;i<=num_len;i++){
        if(dx && dy){
            if((float)(a-pos_x[i])/dx==(float)(b-pos_y[i])/dy && (a-pos_x[i])%dx==0 && ((dx>0 && (a-pos_x[i])>=0)||(dx<0 && (a-pos_x[i])<=0))) {printf("Yes"); return 0;}
        }
        else if(dx){
            if(b-pos_y[i]==0 && (a-pos_x[i])%dx==0 && ((dx>0 && (a-pos_x[i])>=0)||(dx<0 && (a-pos_x[i])<=0))) {printf("Yes"); return 0;}
        }else if(dy){
            if(a-pos_x[i]==0 && (b-pos_y[i])%dy==0 && ((dy>0 && (b-pos_y[i])>=0)||(dy<0 && (b-pos_y[i])<=0))) {printf("Yes"); return 0;}
        }else{
            if(a==pos_x[i] && b==pos_y[i]) {printf("Yes"); return 0;}
        }
    }
    printf("No");
    return 0;
}

#include <stdio.h>
#include <stdbool.h>

int a, b;
int dx, dy;
char str[110];

int abs(int a){
	return a >= 0 ? a : -a;
}

int sign(int x){
	if (x > 0)
		return 1;
	else if (x == 0)
		return 0;
	else
		return -1;
}

bool check(int x, int y){
	int xdiff = a - x;
	int ydiff = b - y;

	if (x == a && y == b)
		return true;

	if (a != x && dx == 0)
		return false;
	if (b != y && dy == 0)
		return false;

	if (sign(xdiff) * sign(dx) < 0 || sign(ydiff) * sign(dy) < 0)
		return false;

	if (dx == 0)
		return abs(ydiff) % abs(dy) == 0;
	else if (dy == 0)
		return abs(xdiff) % abs(dx) == 0;
	else
		return
			abs(ydiff) % abs(dy) == 0 &&
			abs(xdiff) % abs(dx) == 0 &&
			((long long int)xdiff) * dy == ((long long int)ydiff) * dx;
}

int main(void){
	int i, j;

	scanf("%d %d", &a, &b);
	scanf("%s", str);

	for (i = 0; str[i]; i++){
		if (str[i] == 'U')
			dy++;
		else if (str[i] == 'D')
			dy--;
		else if (str[i] == 'R')
			dx++;
		else if (str[i] == 'L')
			dx--;
	}

	int x = 0, y = 0;
	bool works = check(0,0);

	for (i = 0; !works && str[i]; i++){
		if (str[i] == 'U')
			y++;
		else if (str[i] == 'D')
			y--;
		else if (str[i] == 'R')
			x++;
		else if (str[i] == 'L')
			x--;
		works = check(x,y);
	}

	printf("%s\n", works ? "Yes" : "No");

	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define For(i,n) for(i=0;i<n;i++)
#define MAX(x,y) x>y?x:y
#define MAXN 111

int mov[MAXN][2];

main()
{
    int i, j, flag = 1;
    int k, a, b;
    int x, y, sx = 0, sy = 0;
    char s[111];
    scanf("%d%d%s",&x,&y,s);
    mov[0][0] = mov[0][1] = 0;
    for(i = 0; s[i]; i++)
    {
        mov[i+1][0] = (s[i]=='U'||s[i]=='D')?0:s[i]=='R'?1:-1;
        mov[i+1][1] = (s[i]=='R'||s[i]=='L')?0:s[i]=='U'?1:-1;
        sx += mov[i+1][0];
        sy += mov[i+1][1];
    }
    For(i,strlen(s)){
        x -= mov[i][0];
        y -= mov[i][1];
        //printf("%d %d\n",x,y);
        if(sx==0&&sy==0) k = 1;
        else if(sx!=0)  k = x/sx;
        else k = y/sy;
        if(sx*k==x&&sy*k==y&&k>=0)
        {
            printf("Yes\n");
            flag = 0; break;
        }
    }
    if(flag) printf("No\n");
    return 0;
}

#include<stdio.h>
#include<string.h>
int main()
{
    int a,b,fx=0,fy=0,i,j,k,l,mx,ny,ix,iy,c=0,n;
    char s1[1000];
    scanf("%d%d",&a,&b);
    scanf("%s",s1);
    l=strlen(s1);
    if(a==0 && b==0)
    {
        printf("Yes");
        return(0);
    }
    for(i=0;i<l;i++)
    {
        if(s1[i]=='U')
            fy=fy+1;
        else if(s1[i]=='D')
            fy-=1;
        else if(s1[i]=='R')
            fx+=1;
        else if(s1[i]=='L')
            fx-=1;
    }
    //printf("%d %d\n",fx,fy);
    if(fx!=0 || fy!=0)
    {
        if(fx!=0)
            mx=(a-fx)/fx;
        if(fy!=0)
            ny=(b-fy)/fy;
        if(fx==0 && fy!=0)
            mx=ny;
        else if(fy==0 && fx!=0)
            ny=mx;
        //printf("%d %d\n",mx,ny);
        if(mx<0 || ny<0)
        {
            printf("No");
            return(0);
        }
        else
        {
            c=0;
            n=(mx<ny)?mx:ny;
            ix=fx+n*fx;
            iy=fy+n*fy;
            for(i=0;i<l;i++)
            {
                if(ix==a && iy==b)
                {
                    c++;
                    printf("Yes");
                    return(0);
                }
                if (s1[i]=='U')
                iy=fy+1;
                else if(s1[i]=='D')
                iy-=1;
                else if(s1[i]=='R')
                ix+=1;
                else if(s1[i]=='L')
                ix-=1;

            }
            if(ix==a && iy==b)
                {
                    c++;
                    printf("Yes");
                    return(0);
                }
            if(c==0)
            {
                printf("No");
                return(0);
            }
        }
    }
    else
    {
        c=0;
        if(fx==0 && fy==0)
            {
                c=0;
                for(i=0;i<l;i++)
                {
                    if(fx==a && fy==b)
                    {
                        printf("Yes");
                        c++;
                        return(0);
                    }
                    if(s1[i]=='U')
                    fy=fy+1;
                    else if(s1[i]=='D')
                    fy-=1;
                    else if(s1[i]=='R')
                    fx+=1;
                    else if(s1[i]=='L')
                    fx-=1;
                }
            if(c==0)
            {
                printf("No");
                return(0);
            }
            }
    }
}

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

#define REP(i,n) for(int i=0;i<(int)n;++i)
typedef long long ll;

const int dy[] = {1,0,-1,0};
const int dx[] = {0,1,0,-1};
const char dir[] = "URDL";

int d[100];
char s[105];
int vx[105], vy[105];
int ix, iy;
int main() {
	int a,b;
	while(scanf("%d%d", &a, &b) == 2) {
		scanf("%s",s);
		ll y=0,x=0;
		int n = strlen(s);
		REP(i,n) {
			d[i] = strchr(dir, s[i]) - dir;
		}
		ix = 0;
		iy = 0;
		vx[ix++] = 0;
		vy[iy++] = 0;
		REP(i,n) {
			y += dy[d[i]];
			x += dx[d[i]];
			vy[iy++] = y;
			vx[ix++] = x;
		}
		ll Y = y;
		ll X = x;
		int ok = 0;
		REP(i,n) {
			REP(j,n) {
				ll yy = vy[j];
				ll xx = vx[j];
				ll y = b-yy;
				ll x = a-xx;
				if (Y && y % Y == 0) {
					if (X*(y/Y) == x) {
						ok = 1;
					}
				}
				if (Y == 0 && y == 0) {
					if (x == 0 || (X && x % X == 0)) {
						ok = 1;
					}
				}
			}
		}
		printf("%s\n",ok?"Yes":"No");
	}
}

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

int main(){
    int sx=0,sy=0,p=1,a,b,i,num_len,dx,dy;
    char s[101];
    int pos_x[101];
    int pos_y[101];
    scanf("%d %d %s",&a,&b,s);
    pos_x[0]=pos_y[0]=0;
    num_len=strlen(s);
    for(i=0;i<num_len;i++){
        switch(s[i]){
        case 'U':
            sy++;
            break;
        case 'D':
            sy--;
            break;
        case 'R':
            sx++;
            break;
        case 'L':
            sx--;
            break;
        default: ;
        }
        pos_x[i+1]=sx;
        pos_y[i+1]=sy;
    }
    dx=pos_x[num_len];
    dy=pos_y[num_len];
    for(i=0;i<=num_len;i++){
        if((a-pos_x[i])*dy==(b-pos_y[i])*dx) {printf("Yes"); return 0;}
    }
    printf("No");
    return 0;
}

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

int main(){
    int sx=0,sy=0,p=1,a,b,i,num_len,dx,dy;
    char s[101];
    int pos_x[101];
    int pos_y[101];
    scanf("%d %d %s",&a,&b,s);
    pos_x[0]=pos_y[0]=0;
    num_len=strlen(s);
    for(i=0;i<num_len;i++){
        switch(s[i]){
        case 'U':
            sy++;
            break;
        case 'D':
            sy--;
            break;
        case 'R':
            sx++;
            break;
        case 'L':
            sx--;
            break;
        default: ;
        }
        pos_x[i+1]=sx;
        pos_y[i+1]=sy;
    }
    dx=pos_x[num_len];
    dy=pos_y[num_len];
    for(i=0;i<=num_len;i++){
        if(dx && dy){
            if((a-pos_x[i])*dy==(b-pos_y[i])*dx) {printf("Yes"); return 0;}
        }
        else if(dx){
            if(b-pos_y[i]==0 && (a-pos_x[i])%dx==0) {printf("Yes"); return 0;}
        }else{
            if(a-pos_x[i]==0 && (b-pos_y[i])%dy==0) {printf("Yes"); return 0;}
        }
    }
    printf("No");
    return 0;
}

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

int main(){
    int sx=0,sy=0,p=1,a,b,i,num_len,dx,dy;
    char s[110];
    int pos_x[110];
    int pos_y[110];
    scanf("%d %d %s",&a,&b,s);
    pos_x[0]=pos_y[0]=0;
    num_len=strlen(s);
    for(i=0;i<num_len;i++){
        switch(s[i]){
        case 'U':
            sy++;
            break;
        case 'D':
            sy--;
            break;
        case 'R':
            sx++;
            break;
        case 'L':
            sx--;
            break;
        default: ;
        }
        pos_x[i+1]=sx;
        pos_y[i+1]=sy;
    }
    dx=pos_x[num_len];
    dy=pos_y[num_len];
    for(i=0;i<=num_len;i++){
        if(dx && dy){
            if((float)(a-pos_x[i])/dx==(float)(b-pos_y[i])/dy && ((dx>0 && (a-pos_x[i])>=0)||(dx<0 && (a-pos_x[i])<=0))) {printf("Yes"); return 0;}
        }
        else if(dx){
            if(b-pos_y[i]==0 && (a-pos_x[i])%dx==0 && ((dx>0 && (a-pos_x[i])>=0)||(dx<0 && (a-pos_x[i])<=0))) {printf("Yes"); return 0;}
        }else if(dy){
            if(a-pos_x[i]==0 && (b-pos_y[i])%dy==0 && ((dy>0 && (b-pos_y[i])>=0)||(dy<0 && (b-pos_y[i])<=0))) {printf("Yes"); return 0;}
        }else{
            if(a==pos_x[i] && b==pos_y[i]) {printf("Yes"); return 0;}
        }
    }
    printf("No");
    return 0;
}

#include <stdio.h>
#include <stdbool.h>

int a, b;
int dx, dy;
char str[110];

int abs(int a){
	return a >= 0 ? a : -a;
}

int sign(int x){
	if (x > 0)
		return 1;
	else if (x == 0)
		return 0;
	else
		return -1;
}

bool check(int x, int y){
	int xdiff = a - x;
	int ydiff = b - y;

	if (x == a && y == b)
		return true;

	if (a != x && dx == 0)
		return false;
	if (b != y && dy == 0)
		return false;

	if (sign(xdiff) * sign(dx) < 0 || sign(ydiff) * sign(dy) < 0)
		return false;

	if (dx == 0)
		return abs(ydiff) % abs(dy) == 0;
	else if (dy == 0)
		return abs(xdiff) % abs(dx) == 0;
	else
		return ((unsigned long long int)xdiff) * dy / dx == ydiff;
}

int main(void){
	int i, j;

	scanf("%d %d", &a, &b);
	scanf("%s", str);

	for (i = 0; str[i]; i++){
		if (str[i] == 'U')
			dy++;
		else if (str[i] == 'D')
			dy--;
		else if (str[i] == 'R')
			dx++;
		else if (str[i] == 'L')
			dx--;
	}

	int x = 0, y = 0;
	bool works = check(0,0);

	for (i = 0; !works && str[i]; i++){
		if (str[i] == 'U')
			y++;
		else if (str[i] == 'D')
			y--;
		else if (str[i] == 'R')
			x++;
		else if (str[i] == 'L')
			x--;
		works = check(x,y);
	}

	printf("%s\n", works ? "Yes" : "No");

	return 0;
}

#include <stdio.h>
#include <stdbool.h>

int a, b;
int dx, dy;
char str[110];

int abs(int a){
	return a >= 0 ? a : -a;
}

int sign(int x){
	if (x > 0)
		return 1;
	else if (x == 0)
		return 0;
	else
		return -1;
}

bool check(int x, int y){
	int xdiff = a - x;
	int ydiff = b - y;

	if (x == a && y == b)
		return true;

	if (a != x && dx == 0)
		return false;
	if (b != y && dy == 0)
		return false;

	if (sign(xdiff) * sign(dx) < 0 || sign(ydiff) * sign(dy) < 0)
		return false;

	if (dx == 0)
		return abs(ydiff) % abs(dy) == 0;
	else if (dy == 0)
		return abs(xdiff) % abs(dx) == 0;
	else
		return ((long long int)xdiff) * dy / dx == ydiff;
}

int main(void){
	int i, j;

	scanf("%d %d", &a, &b);
	scanf("%s", str);

	for (i = 0; str[i]; i++){
		if (str[i] == 'U')
			dy++;
		else if (str[i] == 'D')
			dy--;
		else if (str[i] == 'R')
			dx++;
		else if (str[i] == 'L')
			dx--;
	}

	int x = 0, y = 0;
	bool works = check(0,0);

	for (i = 0; !works && str[i]; i++){
		if (str[i] == 'U')
			y++;
		else if (str[i] == 'D')
			y--;
		else if (str[i] == 'R')
			x++;
		else if (str[i] == 'L')
			x--;
		works = check(x,y);
	}

	printf("%s\n", works ? "Yes" : "No");

	return 0;
}

#include <stdio.h>
#include <stdbool.h>

int a, b;
int dx, dy;
char str[110];

int abs(int a){
	return a >= 0 ? a : -a;
}

int sign(int x){
	if (x > 0)
		return 1;
	else if (x == 0)
		return 0;
	else
		return -1;
}

bool check(int x, int y){
	int xdiff = a - x;
	int ydiff = b - y;

	if (x == a && y == b)
		return true;

	if (a != x && dx == 0)
		return false;
	if (b != y && dy == 0)
		return false;

	if (sign(xdiff) * sign(dx) < 0 || sign(ydiff) * sign(dy) < 0)
		return false;

	if (dx == 0)
		return abs(ydiff) % abs(dy) == 0;
	else if (dy == 0)
		return abs(xdiff) % abs(dx) == 0;
	else
		return xdiff * dy / dx == ydiff;
}

int main(void){
	int i, j;

	scanf("%d %d", &a, &b);
	scanf("%s", str);

	for (i = 0; str[i]; i++){
		if (str[i] == 'U')
			dy++;
		else if (str[i] == 'D')
			dy--;
		else if (str[i] == 'R')
			dx++;
		else if (str[i] == 'L')
			dx--;
	}

	int x = 0, y = 0;
	bool works = check(0,0);

	for (i = 0; !works && str[i]; i++){
		if (str[i] == 'U')
			y++;
		else if (str[i] == 'D')
			y--;
		else if (str[i] == 'R')
			x++;
		else if (str[i] == 'L')
			x--;
		works = check(x,y);
	}

	printf("%s\n", works ? "Yes" : "No");

	return 0;
}

#include <stdio.h>
#include <stdbool.h>

int a, b;
int dx, dy;
char str[110];

int abs(int a){
	return a >= 0 ? a : -a;
}

bool check(int x, int y){
	int xdiff = a - x;
	int ydiff = b - y;

	if (x == a && y == b)
		return true;

	if (a != x && dx == 0)
		return false;
	if (b != y && dy == 0)
		return false;

	if (xdiff * dx < 0 || ydiff * dy < 0)
		return false;

	if (dx == 0)
		return abs(ydiff) % abs(dy) == 0;
	else if (dy == 0)
		return abs(xdiff) % abs(dx) == 0;
	else
		return a * dy / dx == b;
}

int main(void){
	int i, j;

	scanf("%d %d", &a, &b);
	scanf("%s", str);

	for (i = 0; str[i]; i++){
		if (str[i] == 'U')
			dy++;
		else if (str[i] == 'D')
			dy--;
		else if (str[i] == 'R')
			dx++;
		else if (str[i] == 'L')
			dx--;
	}

	int x = 0, y = 0;
	bool works = check(0,0);

	for (i = 0; !works && str[i]; i++){
		if (str[i] == 'U')
			y++;
		else if (str[i] == 'D')
			y--;
		else if (str[i] == 'R')
			x++;
		else if (str[i] == 'L')
			x--;
		works = check(x,y);
	}

	printf("%s\n", works ? "Yes" : "No");

	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define For(i,n) for(i=0;i<n;i++)
#define MAX(x,y) x>y?x:y
#define MAXN 111

int mov[MAXN][2];

main()
{
    int i, j, flag = 1;
    int k, a, b;
    int x, y, sx = 0, sy = 0;
    char s[111];
    scanf("%d%d%s",&a,&b,s);
    mov[0][0] = mov[0][1] = 0;
    for(i = 0; s[i]; i++)
    {
        mov[i+1][0] = (s[i]=='U'||s[i]=='D')?0:s[i]=='R'?1:-1;
        mov[i+1][1] = (s[i]=='R'||s[i]=='L')?0:s[i]=='U'?1:-1;
        sx += mov[i+1][0];
        sy += mov[i+1][1];
    }
    For(i,strlen(s)){
        x = a - mov[i][0];
        y = b - mov[i][1];
        if(sx==0&&sy==0) k = 1;
        else if(sx!=0)  k = x/sx;
        else k = y/sy;
        if(sx*k==x&&sy*k==y)
        {
            printf("Yes\n");
            flag = 0; break;
        }
    }
    if(flag) printf("No\n");
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define For(i,n) for(i=0;i<n;i++)
#define MAX(x,y) x>y?x:y
#define MAXN 10001

int mov[MAXN][2];

main()
{
    int i, j, flag = 1;
    int k, a, b;
    int x, y, sx = 0, sy = 0;
    char s[111];
    scanf("%d%d%s",&a,&b,s);
    mov[0][0] = mov[0][1] = 0;
    for(i = 0; s[i]; i++)
    {
        mov[i+1][0] = (s[i]=='U'||s[i]=='D')?0:s[i]=='R'?1:-1;
        mov[i+1][1] = (s[i]=='R'||s[i]=='L')?0:s[i]=='U'?1:-1;
        sx += mov[i+1][0];
        sy += mov[i+1][1];
    }
    For(i,strlen(s)){
        x = a - mov[i][0];
        y = b - mov[i][1];
        if(sx) k = x/sx;
        else if(sy) k = y/sy;
        else k = 1;
        if(sx*k==x&&sy*k==y)
        {
            printf("YES\n");
            flag = 0; break;
        }
    }
    if(flag) printf("NO\n");
    return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define For(i,n) for(i=0;i<n;i++)
#define MAX(x,y) x>y?x:y
#define MAXN 111

int mov[MAXN][2];

main()
{
    int i, j, flag = 1;
    int k, a, b;
    int x, y, sx = 0, sy = 0;
    char s[111];
    scanf("%d%d%s",&x,&y,s);
    mov[0][0] = mov[0][1] = 0;
    for(i = 0; s[i]; i++)
    {
        mov[i+1][0] = (s[i]=='U'||s[i]=='D')?0:s[i]=='R'?1:-1;
        mov[i+1][1] = (s[i]=='R'||s[i]=='L')?0:s[i]=='U'?1:-1;
        sx += mov[i+1][0];
        sy += mov[i+1][1];
    }
    For(i,strlen(s)){
        x -= mov[i][0];
        y -= mov[i][1];
        if(sx==0&&sy==0) k = 1;
        else if(sx!=0)  k = x/sx;
        else k = y/sy;
        if(sx*k==x&&sy*k==y&&k>0)
        {
            printf("Yes\n");
            flag = 0; break;
        }
    }
    if(flag) printf("No\n");
    return 0;
}


For each positive integer n consider the integer ψ(n) which is obtained from n by replacing every digit a in the decimal notation of n with the digit (9  -  a). We say that ψ(n) is the reflection of n. For example, reflection of 192 equals 807. Note that leading zeros (if any) should be omitted. So reflection of 9 equals 0, reflection of 91 equals 8.Let us call the weight of the number the product of the number and its reflection. Thus, the weight of the number 10 is equal to 10·89 = 890.Your task is to find the maximum weight of the numbers in the given range [l, r] (boundaries are included).

Input contains two space-separated integers l and r (1 ≤ l ≤ r ≤ 109) — bounds of the range.

Output should contain single integer number: maximum value of the product n·ψ(n), where l ≤ n ≤ r. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout (also you may use %I64d).

NoteIn the third sample weight of 8 equals 8·1 = 8, weight of 9 equals 9·0 = 0, weight of 10 equals 890.Thus, maximum value of the product is equal to 890.

#include<stdio.h>
char s1[30],s2[30];
long long change(char *s)
{
	long long sum=0,i;
	for(i=0;i<strlen(s);i++)
	{
		sum=sum*10+s[i]-'0';
	}
	return sum;
}
long long f(int a,int b)
{
	long long sum=1;
	int i;
	for(i=1;i<=b;i++)
	{
		sum*=a;
	}
	return sum;
}
int main(void)
{
	long long l,r,i;
	scanf("%s%s",s1,s2);
	long long x1,x2,x3;
	int len=strlen(s2);
	x1=change(s1);
	x2=change(s2);
	x3=5*f(10,len-1)-1;
	if(x3<=x2&&x3>=x1) printf("%I64d\n",x3*(f(10,len)-1-x3));
	else if(x3<x1) printf("%I64d\n",x1*(f(10,len)-1-x1));
	else printf("%I64d\n",x2*(f(10,len)-1-x2));
	return 0;
}

#include<stdio.h>
int main()
{long long int l,r,t,n,x=0;
scanf("%I64d %I64d",&r,&l);
n=l;
while(n!=0)
{n=n/10;
x=x*10+9;
}

if(l<=x/2)
printf("%I64d",l*(x-l));
else if(l>x/2&&r<x/2)
printf("%I64d",x/2*(x-x/2));
else
printf("%I64d",r*(x-r));

return 0;
}

#include<stdio.h>
long long find_max(long long low,long long high,long long power)
{
    if(low>high)
        return 0;
    if((power/2>=low&&power/2<=high)||(power/2-1>=low&&power/2-1<=high))
        return (power/2*(power/2-1));
    if(low>power/2)
        return (low*(power-1-low));
    return (high*(power-1-high));
}
main()
{
 long long l,r,max=0;
 scanf("%I64d%I64d",&l,&r);
 int low_digit=0,high_digit=0;
 long long no=l,power=1;
 while(no!=0)
 {
    low_digit++;
    no/=10;
    power*=10;
 }
 no=r;
 while(no!=0)
 {
     high_digit++;
     no/=10;
 }
 long long current_max,low=l,high=power-1;
 while(low_digit<high_digit)
 {
     current_max=find_max(low,high,power);
     if(max<current_max)
        max=current_max;
     low=power;
     high=power*10-1;
     power=power*10;
     low_digit++;
 }
 current_max=find_max(low,r,power);
 if(current_max>max)
    max=current_max;
 printf("%I64d",max);
 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)

#define ll long long

ll pw[30];

ll ref(ll a){
  int i, now=0;
  ll res = 0;

  while(a){
    res += pw[now] * (9 - (a%10));
    now++; a/=10;
  }

  return res;
}

int main(){
  int i,j,k,l,m,n;
  int a,b;
  ll res, tmp, go;

  pw[0] = 1;
  REP(i,1,30) pw[i] = pw[i-1]*10;

  while(scanf("%d%d",&a,&b)==2){
    res = 0;
    REP(i,1,15){
      go = pw[i] / 2;
      if(go < a) go = a;
      if(go > b) go = b;
      tmp = go * ref(go);
      if(res < tmp) res = tmp;
    }
    printf("%I64d\n",res);
  }

  return 0;
}

#include<stdio.h>
__int64 i,j=1,l,r,a=0,b,c=0,k;
__int64 weight(int n1,int n)
{
    b=n;
    while(n!=0)
    {
        n/=10;
        a=a*10+9;
        c++;
    }
    for(i=1;i<=c;i++)
      j*=10;
    k=j/2;
    if(k>=n1 && k<=b)
        return((a-k)*k);
    else if(k>b)
        return((a-b)*b);
        else
            return((a-n1)*n1);
}
int main()
{
    scanf("%d%d",&l,&r);
    printf("%I64d",weight(l,r));
    return(0);
}

#include <stdio.h>

long long max(long long a, long long b)
{
     if (a > b) {
	  return a;
     } else {
	  return b;
     }
}

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

long long reverse(long long n)
{
     long long r = 0, d = 1;
     int i;

     while (n) {
	  r += (9 - n % 10) * d;
	  n /= 10;
	  d *= 10;
     }

     return r;
}

int main()
{
     int l, r, c, i;
     long long d = 1;

     scanf("%d %d", &l, &r);

     c = r;

     while (c) {
	  c /= 10;
	  d *= 10;
     }

     d /= 10;

     l = max(l, d);

     if (r < d * 5) {
	  printf("%I64d\n", (long long)r * reverse(r));
     } else if (l > d * 5) {
	  printf("%I64d\n", (long long)l * reverse(l));
     } else {
	  printf("%I64d\n", (long long)d * 5 * reverse(d * 5));
     }

     return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
 long long int l,r,i,y,max,n,x,j,m;
  scanf("%I64d%I64d",&l,&r);
  max=0;

  for(i=l;i<=r;i++)
  { x=log10(i);
      n=pow(10,(x+1))-1-i;

     if(n*i>max)
       max=n*i;
       }
     printf("%I64d",max);
     return  0;
     }

#include<stdio.h>
#include<math.h>
int main()
{
 long long int l,r,i,y,max,n,x,j,m;
  scanf("%I64d%I64d",&l,&r);
  max=0;

  for(i=l;i<=r;i++)
  { x=log10(i);
  if(i%10==0)
  n=pow(10,(x+1))-i;
  else
      n=pow(10,(x+1))-1-i;

     if(n*i>max)
       max=n*i;
       }
     printf("%I64d",max);
     return  0;
     }

#include<stdio.h>
int main()
{
  long int l,r,i,y,max,n,x,j,m;
  scanf("%ld%ld",&l,&r);
  max=0;

  for(i=l;i<=r;i++)
  { n=0;
    j=1;
    m=i;

     while(m>0)
     {  y=m%10;
        x=9-y;
        n=n+j*x;
        m=m/10;
        j=j*10;
        }
     if(n*i>max)
       max=n*i;
       }
     printf("%ld",max);
     return  0;
     }

#include<stdio.h>
int main()
{long long int l,r,t,n,x=0;
scanf("%I64d %I64d",&r,&l);
n=l;
while(n!=0)
{n=n/10;
x=x*10+9;
}

if(l<x/2)
printf("%I64d",l*(x-l));
else if(l>x/2&&r<x/2)
printf("%I64d",x/2*(x-x/2));
else
printf("%I64d",r*(x-r));

return 0;
}

#include<stdio.h>
__int64 i,j=1,l,r,a=0,b,c=0,k;
__int64 weight(int n)
{
    b=n;
    while(n!=0)
    {
        n/=10;
        a=a*10+9;
        c++;
    }
    for(i=1;i<=c;i++)
      j*=10;
    k=j/2;
    if(b>=k)
        return((a-k)*k);
    else
        return((a-b)*b);
}
int main()
{
    scanf("%d%d",&l,&r);
    printf("%I64d",weight(r));
    return(0);
}

#include <stdio.h>

long long max(long long a, long long b)
{
     if (a > b) {
	  return a;
     } else {
	  return b;
     }
}

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

int reverse(int n)
{
     int r = 0, d = 1, i;

     while (n) {
	  r += (9 - n % 10) * d;
	  n /= 10;
	  d *= 10;
     }

     return r;
}

int main()
{
     int l, r, c, i;
     long long d = 1;

     scanf("%d %d", &l, &r);

     c = r;

     while (c) {
	  c /= 10;
	  d *= 10;
     }

     d /= 10;

     l = max(l, d);

     if (r < d * 5) {
	  printf("%I64d\n", (long long)r * reverse(r));
     } else if (l > d * 5) {
	  printf("%I64d\n", (long long)l * reverse(l));
     } else {
	  printf("%I64d\n", (long long)d * 5 * reverse(d * 5));
     }

     return 0;
}

#include<stdio.h>
#include<math.h>
int main()
{
 long long int l,r,i,y,max,n,x,j,m;
  scanf("%I64d%I64d",&l,&r);
  max=0;

  for(i=l;i<=r;i++)
  { x=log10(i);
  if(i==10)
  n=89;
  else
      n=pow(10,(x+1))-1-i;

     if(n*i>max)
       max=n*i;
       }
     printf("%I64d",max);
     return  0;
     }


Jamie loves sleeping. One day, he decides that he needs to wake up at exactly hh: mm. However, he hates waking up, so he wants to make waking up less painful by setting the alarm at a lucky time. He will then press the snooze button every x minutes until hh: mm is reached, and only then he will wake up. He wants to know what is the smallest number of times he needs to press the snooze button.A time is considered lucky if it contains a digit '7'. For example, 13: 07 and 17: 27 are lucky, while 00: 48 and 21: 34 are not lucky.Note that it is not necessary that the time set for the alarm and the wake-up time are on the same day. It is guaranteed that there is a lucky time Jamie can set so that he can wake at hh: mm.Formally, find the smallest possible non-negative integer y such that the time representation of the time x·y minutes before hh: mm contains the digit '7'.Jamie uses 24-hours clock, so after 23: 59 comes 00: 00.

The first line contains a single integer x (1 ≤ x ≤ 60). The second line contains two two-digit integers, hh and mm (00 ≤ hh ≤ 23, 00 ≤ mm ≤ 59).

Print the minimum number of times he needs to press the button.

NoteIn the first sample, Jamie needs to wake up at 11:23. So, he can set his alarm at 11:17. He would press the snooze button when the alarm rings at 11:17 and at 11:20.In the second sample, Jamie can set his alarm at exactly at 01:07 which is lucky.

#include<stdio.h>
int main(void){
int count,mm,hh,x;
scanf("%d",&x);
scanf("%d %d",&hh,&mm);
for(count=0;hh%10!=7&&mm%10!=7;){
    mm=mm-x;
    count++;
    if(mm<0) {mm=60+mm;
    hh--;}
    if(hh<0) {hh=24+hh;}
}
printf("%d",count);
return 0;
}

#include <stdio.h>

int main()
{
	int x,hh,mm;
	int h,m;
	int a,i;

	scanf("%d", &x);
	scanf("%d %d", &hh, &mm);

	for(i=0; ; i++)
	{
	    h=hh%10;
		m=mm%10;

		mm=mm-x;
		if(mm<0)
		{
			mm=mm+60;
			hh=hh-1;
		}
		if(hh<0)
		{
			hh=hh+24;
		}

		if(h==7 || m==7 || (hh==7 && mm==0) )
		{
			break;
		}
	}
	printf("%d\n", i);
	return 0;
}

#include <stdio.h>

int main (void)
{
	int x, h, m, count=0;
	scanf ("%d %d %d", &x, &h, &m);

	while (h%10!=7 && m%10!=7)
	{
		m=m-x;

		while (m<0)
		{
			m=60+m;
			h--;
		}
		while (h<0)
			h=24+h;
		count++;
	}

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

	return 0;
}

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <stdbool.h>

#define max(A, B) ((A > B) ? A : B)
#define min(A, B) ((A > B) ? B : A)
#define INF 999999

int x;
int h, m;

int main()
{
    int ans;
    int y;

    scanf("%d", &x);
    scanf("%d%d", &h, &m);
    if (h == 7 || h == 17 || m % 10 == 7)
        printf("0");
    else{
        if (h > 17) {
            y = 0;
            while (1) {
                if (m - x < 0) {
                    h--;
                    m += 60;
                }
                m -= x;
                y++;
                if (m % 10 == 7 || h == 17) {
                    ans = y;
                    break;
                }
            }
        }else if (h < 7) {
            y = 0;
            h += 24;
            while (1) {
                if (m - x < 0) {
                    h--;
                    m += 60;
                }
                m -= x;
                y++;
                if (m % 10 == 7 || h == 17) {
                    ans = y;
                    break;
                }
            }
        }else{
            y = 0;
            while (1) {
                if (m - x < 0) {
                    h--;
                    m += 60;
                }
                m -= x;
                y++;
                if (m % 10 == 7 || h == 7) {
                    ans = y;
                    break;
                }
            }
        }
        printf("%d", ans);
    }
    return 0;
}

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

int lucky_checker(int h,int m);
void time_minus(int *h, int *m);

int main()
{
	int x,hour,min,counter=0;
	float button_times;
	int loop_x;

	scanf("%d %d %d",&x,&hour,&min);
	loop_x = x;

	while(! lucky_checker(hour,min)) {
		counter++;
		loop_x = x;
		while(loop_x--) {
			//printf("%d\n",loop_x);
			//getchar();
			time_minus(&hour,&min);
		}
		//printf("%d:%d\n",hour,min);
	}
	//printf("%d %d\n",hour,min);
	//button_times = (float)counter/x;
	//if (button_times>0 && button_times<1)
		//button_times=1;

	//printf("%d\n",(int)button_times);
	printf("%d\n",counter);

//	check 7
//	no 7
//	minus 1 min
//	check 7 or change hour

	return 0;
}

int lucky_checker(int h,int m)
{
	int lucky=0,checker_7;
	while(h || m) {
		checker_7 = h%10;
		if (checker_7==7) {
			//printf("Hour yes\n");
			lucky=1;
			break;
		}
		h/=10;

		checker_7 = m%10;
		if (checker_7==7) {
			//printf("Min yes\n");
			lucky=1;
			break;
		}
		m/=10;
	}
	return lucky;
}

void time_minus(int *h, int *m)
{
	if (*m == 0) {
		*h-=1;
		*m = 59;
		if (*h < 0) {
			*h = 23;
		}
	}
	else {
		*m -=1;
	}
}

#include<stdio.h>

int check(int h,int m)
{
	int count=0;
	while(h>0)
	{
		if(h%10==7)
		{
			count++;
		}
		h=h/10;
	}
	while(m>0)
	{
		if(m%10==7)
		{
			count++;
		}
		m=m/10;
	}
	return count;
}
int main()
{
	int x;
	scanf("%d",&x);
	int h,m,count1=0,count=0,i;
	scanf("%d %d",&h,&m);
	while(1)
	{
		count=check(h,m);
		if(count>0)
		{
			printf("%d\n",count1);
			break;
		}
		else
		{
			m=m-x;
			if(m<0)
			{
				h--;
				if(h<0)
				{
					h=h+24;
				}
				m=60+m;
			}
			h=h%24;
			m=m%60;
			count1++;
		}
	}
	return 0;
}

#include<stdio.h>
// 1 means lucky
int check(int a,int b)
{if(a%10==7 || b%10==7)
return 1;
else
return 0;
}
int main()
    {int x,a,b,count=0;
    scanf("%d %d %d",&x,&a,&b);
    while(check(a,b)==0)
    {  count++;
    if((b-x)>=0)
    b=b-x;
    else
    {b=b-x+60;
    if(a-1<0)
    a=a-1+24;
        else
        a=a-1;
    }

    }
    printf("%d",count);
    }

#include<stdio.h>
main()
{

    int x,hour,d,minute,click=0,snooze,c;
    scanf("%d",&x);
    scanf("%d %d",&hour,&minute);
    c = hour;
    d = minute;
    while(1)
        {
            if(hour==0)
            {
                hour=24;

            }
    click++;

    if(x <= minute)
        snooze = minute-x;
    else
      {
          snooze = 60 + (minute-x);
             hour = hour-1;

        if(hour%10==7)
            {

            break;
            }
      }

        if(snooze%10==7)
        {

            break;
        }
        else
            minute=snooze;

}

if(c%10==7||d%10==7)
    click=0;
printf("%d",click);
}

#include <stdio.h>
int main(){
	int x,h,m;
	int lh,lm;
	int cnt = 0;
	scanf("%d",&x);
	scanf("%d %d",&h,&m);
	for(;;){
		if(m%10==7){
			printf("%d",cnt);
			break;
		}
		if(h%10==7){
			printf("%d",cnt);
			break;
		}
		m = m - x;
		cnt++;
		if(m<0){
			m = 60 + m;
			h = h - 1;
			if(h<0){
				h = 23;
			}
		}
		if(m%10==7){
			printf("%d",cnt);
			break;
		}
		if(h%10==7){
			printf("%d",cnt);
			break;
		}
	}
	return 0;
}

#include<stdio.h>
int F(int x,int n,int m)
{
	int i;
    for(i=0;;i++)
	{
		if(n%10==7||m%10==7)
		{
			printf("%d\n",i);
			break;
		}
		else
		{
			m-=x;
			if(m<0)
			{
				m+=60;
				n--;
			}
			if(n<0)
				n+=24;
		}
	}
}
int main()
{
	int a,b,c;
	scanf("%d%d%d",&a,&b,&c);
	F(a,b,c);
	return 0;
}

#include<stdio.h>
int main()
{
    int x, hh, mm;
    scanf("%d%d%d",&x,&hh,&mm);

    if(hh== 17 || hh == 7)
    {
        printf("0\n");
        return 0;
    }
    int remainder = mm % 10;
    if(remainder - 7 == 0)
    {
        printf("0\n");
        return 0;
    }
    int temp, y = 0, count = 0;

    while(count<=0)
    {
        if(mm>=x)
        {
            mm = mm - x;
            y++;

        }
        else
        {
            if(hh==0)
            {
                hh = 24;
            }
            temp = x - mm;
            mm = 60 - temp;
            hh--;
            y++;

        }
        remainder = mm % 10;
        if(remainder - 7 == 0)
        {
            count++;
        }
        if(hh == 7 || hh == 17)
        {
            count++;
        }
    }
    printf("%d",y);
    return 0;
}

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

typedef struct{
	int hh,mm;
}Time;

void *goBack(Time *time, int min){

	time->mm -= min;
	if(time->mm < 0){
		time->mm  += 60;
		time->hh--;
		if(time->hh < 0)
			time->hh += 24;
	}

}

int solve(Time *time, int n){
	int total = 0;
	int luck = 0;

	if(time->hh % 10 == 7 || time->mm % 10 == 7)
		return total;

	while(luck == 0){
		total++;
		goBack(time,n);
		if(time->hh % 10 == 7 || time->mm % 10 == 7)
			luck = 1;

	}

	return total;
}

int main(){

	int n;
	Time *time = (Time*)malloc(sizeof(Time));

	scanf("%d %d %d",&n, &time->hh, &time->mm);

	printf("%d\n",solve(time,n));

	return 0;
}

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

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

int main(){
    int x,h,m,count=0;
    scanf("%d",&x);
    scanf("%d %d",&h,&m);
    while((h%10)!=7 && (m%10)!=7){
        if(x > m){
            m = 60 - (x-m);
            if(h==0)    h = 23;
            else    h--;
        }
        else{
            m -= x;
        }
        count++;
    }
    printf("%d",count);
    return 0;
}

#include<stdio.h>

int x, nSnoozes;
int hh, mm;
int h, m;

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

void cntSnoozes(int a, int b)
{

	if (checkLucky(a) || checkLucky(b))
	{
     //  printf("%d :%d  nSnoozes : %d \n",a,b,nSnoozes);
		return;
	}
	else
	{
		int tx = x, th = a, tm = b;
		while (tm >= 0)
		{
		//	printf(" in while%d :%d  snooze is : %d \n",th,tm,nSnoozes);
			tm -= x; nSnoozes++;
			//printf(" updated %d :%d nSnoozes :%d \n",th,tm,nSnoozes);
			if (tm < 0){
			//	printf("negative %d :%d  nSnoozes : %d \n",th,tm,nSnoozes);
				break;}

			else if (checkLucky(tm))
				return;
		}
		if (th == 0)
		{
			th = 23;
		}
		else   th = th - 1;

		tm = 60 + tm; //nSnoozes++;
	//	printf(" outside updated %d :%d nSnoozes : %d \n",th,tm,nSnoozes);
		cntSnoozes(th, tm);
	}
	return;
}

int main()
{
		x = hh = mm = nSnoozes = 0;
		scanf("%d", &x);
		scanf("%d %d", &h, &m);

		int th = checkLucky(h);
		int tm = checkLucky(m);
		if (th == 1 || tm == 1)
			nSnoozes = 0;
		else cntSnoozes(h, m);
		printf("%d\n", nSnoozes);
		return 0;
	}

#include <stdio.h>

int x, hh, mm,ans;
int main()
{
    scanf("%d%d%d", &x, &hh, &mm);
    while (1)
    {
        if (hh % 10 == 7 || mm % 10 == 7)break;
        ans++;
        mm -= x;
        if (mm < 0)
        {
            hh--;
            mm += 60;
        }
        if (hh < 0)hh += 24;
    }
    printf("%d", ans);
    return 0;
}

#include <stdio.h>

int main()
{
    int x, h, m, count;
    count = 0;
    scanf("%d %d %d", &x, &h, &m);
    for(;;)
    {
        if(m%10 == 7 || h%10 == 7)
        {
            printf("%d", count);
            break;
        }
        else
        {
            m = m - x;
            if(m < 0)
            {
                h--;
                m = m + 60;
            }
            if(h < 0)
            {
                h = h + 24;
            }
        }
        count++;
    }
    return 0;
}

#include<stdio.h>
int is_lucky(int hh, int mm);
int main()
{
    int hh,mm,x,y,i,j;
    while(scanf("%d",&x)==1){
        scanf("%d %d",&hh,&mm);
        y=0;

        while(is_lucky(hh,mm)!=1){
            mm=mm-x;
            y++;
            if(mm<0){
                mm=60+mm;
                hh--;
            }
            if(hh<0)
                hh=24+hh;

        }
        printf("%d\n",y);
    }
    return 0;
}
int is_lucky(int hh, int mm)
{
    while(mm!=0){
        if(mm%10==7)
            return 1;
        mm/=10;
    }
    while(hh!=0){
        if(hh%10==7)
            return 1;
        hh/=10;
    }
    return 0;
}

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

int main()
{
    int x,hh,mm,ans=0;
    scanf("%d %d %d",&x,&hh,&mm);
    while(mm%10!=7){
        if(hh%10==7){
            break;
        }
        if(mm>=x){
            mm-=x;
        }
        else{
            mm=60-(x-mm);
            if(hh==0){
                hh=23;
            }
            else{
                hh--;
            }
        }
        ans++;

    }
    printf("%d",ans);

    return 0;
}

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

int contains(int m){
    int r;
    while(m > 0){
        r = m % 10;
        if(r == 7)
            return 0;
        m = m /10;
    }
    return 1;

}
int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int t;
    scanf("%d",&t);
    int m,n;
    scanf("%d%d",&m,&n);
    long long int c = 0;
    while(contains(m) && contains(n)){

        n = n - t;

        c++;
        if(n < 0){
            m = m - 1;
            if(m == -1)
                m = 23;
            n = n + 60;
        }

    }
    printf("%lld\n",c);
    return 0;
}

#include <stdio.h>
#include <stdbool.h>

// - WRONG in isluck
// - timeout
bool isluck(int h, int m) {
    return h % 10 == 7 || m % 10 == 7;
}
int luck(int h, int m, int x) {
    int cnt = 0;
    for (cnt = 0; !isluck(h, m); cnt++) {
        m -= x;
        if (m < 0) {
            m += 60;
            h -= 1;
            if (h < 0) {
                h += 24;
            }
        }
    }
    // printf("%d %d\n", h, m);
    return cnt;
}
int test();
int main() {
#if 1
    int x;
    int h, m;
    scanf("%d", &x);
    scanf("%d%d", &h, &m);
    printf("%d\n", luck(h, m, x));
    return 0;
#else
    return test();
#endif
}

int unit(int h, int m, int x, int e) {
    int r;
    if (e != (r = luck(h, m, x))) {
        printf("fail at luch(%d, %d, %d) = %d != %d\n", h, m, x, r, e);
    }
}
int test() {
    unit(11, 23, 3, 2);
    unit(1, 7, 5, 0);
    unit(0, 0, 3, 1);
    unit(0, 0, 1, 3);
    unit(0, 0, 2, 181);
    return 0;
}

#include<stdio.h>
int fun(int h, int m){
	if(m%10==7 || h%10==7)return 0;
	return 1;
}
int main(){
	int x,h,m,h1,m1,ans=0;
	scanf("%d%d%d",&x,&h,&m);
	h1=h;
	m1=m;
	while(fun(h,m)){
		ans++;
		m-=x;
		if(m<0){
			h--;
			if(h<0)h=23;
			m+=60;
		}
	}
	printf("%d\n",ans);
	return 0;
}

#include<stdio.h>

int check(int h,int m){
	if(h%10==7||m%10==7)
		return 0;
	h/=10;
	m/=10;
	if(h%10==7||m%10==7)
		return 0;
	return 1;
}
void adjust(int *h,int *m,int x){
	if(*m<x){
		if(*h==0)
			*h=23;
		else
			*h=*h-1;
		*m=*m+60;
	}
	*m=*m-x;

}
int main(){
	int x,h,m;
	scanf("%d%d%d",&x,&h,&m);
	int count=0;
	while(check(h,m)){
		count++;
		adjust(&h,&m,x);
	}
	printf("%d\n",count);
	return 0;
}

#include<stdio.h>
#include<stdbool.h>
bool ThereIsSeven(int num)
{
    while (num!=0)
    {
        if (num%10==7)
            return true;
        num/=10;
    }
    return false;

}
int main()
{
    int x, hh, mm;
    int count=0;
    scanf("%d", &x);
    scanf("%d %d", &hh,&mm);
    while (ThereIsSeven(hh)==false && ThereIsSeven(mm)==false)
   {
       mm=mm-x;
       if (mm<0)
       {
           mm=mm+60;
           hh=hh-1;
           hh=(hh+24)%24;
       }
       count++;
   }
   printf("%d", count);
    return 0;

}

#include <stdio.h>
int main()
{
    int x,hh,mm,c=0;
    scanf("%d%d%d",&x,&hh,&mm);
    while(1)
    {
        if(hh%10==7||mm%10==7)
        {printf("%d",c);
         break;
        }
        else
       { mm-=x;
        c++;
        if(mm<0)
        { mm+=60;
          hh--;
          if(hh<0)
          hh+=24;
        }
       }
    }

    return 0;
}

#include <stdio.h>

int main()
{
    int x,shi,fen,c=0;
    scanf("%d",&x);
    scanf("%d%d",&shi,&fen);
    while(1)
    {
        if(shi%10==7 || fen%10==7)
            break;
        fen-=x;
        if(fen<0)
        {
            fen=60+fen;
            shi-=1;
            if(shi<0)
                shi=24+shi;
        }
        c++;
    }
    printf("%d\n",c);
    return 0;
}

#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
long long cmpfunc (const void * a, const void * b) {
   return ( *(long long*)a - *(long long*)b );
}

int main(void){

long long int test,i,j,n,count,flag=0,o1=0,o2=0,b1,x,m,l,max,sum2,min,f,c,r,o,sum1,sum=0,y,a,b,count1=0,a2,b2;

scanf("%lld%lld%lld",&n,&a,&b);
count=0;

while(a%10!=7 && b%10!=7){
    b-=n;
    count++;
    if(b<0){
        b=60+b;
        a--;
    }
    if(a<0){
        a=23;
    }
}
printf("%lld",count);
return 0;
}

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

int main()
{  int n,hh,mm,c=0;
scanf("%d%d%d",&n,&hh,&mm);

while(1)

{if(hh%10==7||mm%10==7){
   break;

}

else{
    if(mm-n>=0){
            mm=mm-n;
            c++;

    }
    else
    {
        mm=60+mm-n;
        c++;
       if(hh-1>=0){
        hh=hh-1;
       }
       else
       { hh=23;

       }
    }
}

}
printf("%d",c);

    return 0;}

#include <stdio.h>
#include <string.h>
int counter=0;

void check(int hh, int mm, int snooze){

	while(mm % 10 != 7 && hh % 10 != 7){
		mm -= snooze;
		if(mm < 0){
			mm = mm + 60;
			hh--;
			if(hh < 0)
				hh = hh + 24;
		}
		counter++;
	}
	//printf("%d",counter);
}

int main(void){
	int snooze;
	int hh, mm;
	//int finalcounter=0;

	scanf("%d",&snooze);
	scanf("%d %d", &hh, &mm);

	check(hh,mm,snooze);

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

}

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

long long find(long long h,long long m){
long long k;
while(h!=0){
k=h%10;
if(k==7){
return 0;};
h=h/10;}

while(m!=0){
k=m%10;
if(k==7){
return 0;}
m=m/10;}

return -1;}

void main(){
long long x,h,m,k1,k2,k,count=0;
scanf("%lld",&x);
scanf("%lld %lld",&h,&m);

k1=h;k2=m;

while(k2!=0){
k=k2%10;
if(k==7){
printf("0\n");exit(0);};
k2=k2/10;}

while(k1!=0){
k=k1%10;
if(k==7){
printf("0\n");exit(0);}
k1=k1/10;}

while(1){

count++;

if(m<x){
m=60-(x-m);
if(h==0)
	h=23;
else
h=h-1;

if(find(h,m)==0){
printf("%lld\n",count);exit(0);}}

else if(m>=x){
m=m-x;

if(find(h,m)==0){
printf("%lld\n",count);exit(0);}}

}

exit(0);}

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

int main(){

   int i,x,hh,mm,snooze=0;
   scanf("%d",&x);
   scanf("%d %d",&hh,&mm);
   while(1){

   condition:
    if((hh==7)||(hh==17)||(mm%10==7)){
        break;
    }
    if(x>mm){
            mm = 60-x+mm;
            snooze++;
            hh--;
            if(hh<0){
            hh = 23;
            }
            goto condition;
        }
        mm-=x;
        snooze++;

        }

    printf("%d ",snooze);

}

#include <stdio.h>

int main() {
    int x;
    scanf("%d",&x);
    int hh,mm,tempMM,count,tempHH;
    scanf("%d %d",&hh,&mm);

    if((hh == 17)||(hh == 7)||(mm % 10 == 7))
        printf("0\n");
    else{
        tempMM = mm , tempHH = hh,count = 0;
        while(1){
            if((tempMM % 10 == 7) || (tempHH%10) == 7)
               break;
             if((tempMM - x) < 0){
                 if(tempHH == 0)
                    tempHH = 23;
                else
                    tempHH--;
             }
//                tempHH--;
             if((tempMM - x)<0){
                 tempMM = 60 + tempMM - x ;
             }else{
                 tempMM = tempMM - x;
             }
//            tempMM = tempMM - x;
            count++;
        }
        printf("%d\n",count);
    }

	return 0;
}

#include<stdio.h>
int F(int x,int n,int m)
{
	int i;
    for(i=0;;i++)
	{
		if(n%10==7||m%10==7)
		{
			printf("%d\n",i);
			break;
		}
		else
		{
			m-=x;
			if(m<0)
			{
				m+=60;
				n--;
			}
			if(n<0)
				n+=24;
		}
	}
}
int main()
{
	int a,b,c;
	scanf("%d%d%d",&a,&b,&c);
	F(a,b,c);
	return 0;
}

#include<stdio.h>

int seven(int x,int y)
{
	int p;
	if(x==7||y==7||(x/10)==7||(y/10)==7)
		return 1;
	x=x%10;
	y=y%10;
	if(x==7||y==7)
		return 1;
	else
		return 0;
}

int main()
{
	int x,hh,mm,i=0,p=0;
	scanf("%d%d%d",&x,&hh,&mm);
	for(i=0;i<10000;i++)
	{
		if(seven(hh,mm)==1)
		{
			break;
		}
		if(x<=mm)
		{
			mm=mm-x;
		}
		else
		{
			if(x>mm)
			{
				mm=60+mm-x;
				if(hh==0)
					hh=23;
				else
					hh=hh-1;
			}
		}
		p++;
	}
	printf("%d\n",p);
	return 0;
}

#include<stdio.h>

int main()
{
    int x, hr, mm, ans = 0, tm;

    scanf("%d", &x);
    scanf("%d %d", &hr, &mm);
    tm = (hr * 60) + mm;

    while(1)
    {
        hr = tm / 60;
        mm = tm % 60;
        if(hr % 10 == 7 || mm % 10 == 7)
            break;
        else
        {
            ans++;
            if(tm < x)
                tm = tm + 1440;
            tm = tm - x;
        }
    }

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

    return 0;
}

#include <stdio.h>

int x, hh, mm,ans;
int main()
{
    scanf("%d%d%d", &x, &hh, &mm);
    while (1)
    {
        if (hh % 10 == 7 || mm % 10 == 7)break;
        ans++;
        mm -= x;
        if (mm < 0)
        {
            hh--;
            mm += 60;
        }
        if (hh < 0)hh += 24;
    }
    printf("%d", ans);
    return 0;
}

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int main()
{
    int x;int y,v,s;v=0;
    int sa,sb,r;
    scanf("%d",&x);
    int a,b;
    scanf("%d%d",&a,&b);
    for(int i=0;i<10000;i++)
    {
        sb=abs(b);
        sa=abs(a);
        while(sb>0)
        {
          r=sb%10;
          sb=sb/10;
          if(r==7)
          {
              s=i;v=1;
              break;
          }
        }
        if(v==1)
        {
            break;
        }
        while(sa>0)
        {
          r=sa%10;
          sa=sa/10;
          if(r==7)
          {
              s=i;v=1;
              break;
          }
        }
        if(v==1)
        {
            break;
        }
        b=b-x;
        if(b<0)
        {
            b=60+b;
            a=a-1;
        }
    }
             printf("%d",s);
}

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

int main(int argc, char *argv[])
{
	int x, hh, mm, res = 0;
	scanf("%d", &x);
	scanf("%d%d", &hh, &mm);

	while ((hh - 7) % 10 != 0 && (mm - 7) % 10 != 0) {
		mm -= x;
		if (mm < 0) {
			mm += 60;
			if (hh == 0) {
				hh = 24;
			}
			--hh;
		}
		++res;
	}

	printf("%d", res);
	return 0;
}

#include <stdio.h>

int main(){
    int x,hh,mm,i=0;
    scanf("%d",&x);
    scanf("%d %d",&hh,&mm);
    while(((hh%10)!=7)&&((mm%10)!=7)){
       i++;
       if(mm<x){
          mm=(mm+60-x);
          if(hh==0)
            hh=23;
          else
            hh=hh-1;
       }
       else{
        mm=mm-x;
       }
    }
     printf("%d",i);
  return 0;
}

#include<stdlib.h>
#include<stdio.h>
int main()
{int hh,mm,x,y=0;
scanf("%d",&x);
scanf("%d %d",&hh,&mm);
while(hh%10!=7 && mm%10!=7)
{   mm=mm-x;
   y++;
  if(mm<0)

    {mm=mm+60;
    hh--;}
  if(hh<0)
    {hh=hh+24;}

}
printf("%d",y);
    return(0);
}

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

int main()
{  int n,hh,mm,c=0,d;
scanf("%d%d%d",&n,&hh,&mm);

while(1)

{if(hh%10==7||mm%10==7){
   break;

}

else{
    if(mm-n>=0){
            mm=mm-n;
            c++;

    }
    else
    {
        mm=60+mm-n;
        c++;
       if(hh-1>=0){
        hh=hh-1;
       }
       else
       { hh=23;

       }
    }
}

}
printf("%d",c);

    return 0;}

#include <stdio.h>
int check(int h,int m)
{
	if((h%10)==7 || (m%10)==7)
	{
		return 1;
	}
	else return 0;
}
int findy(int x,int h, int m)
{
	int i=0,k=0;
	if(m<x)
	{
		h--;
		m=(60+m)-x;
		i++;
		if(check(h,m))
			return i;
	}
	if(m<x && h<=0)
	{
		h=23;
		m=(60+m)-x;
		i++;
		if(check(h,m))
			return i;
	}
	else
	{
		m=m-x;
		i++;
		if(check(h,m))
			return i;
	}
	return i+findy( x,h,m);
}
int main()
{
	int x,h,m;
	scanf("%d%d%d",&x,&h,&m);
	if(check(h,m))
	{
		printf("0");
		return 0;
	}
	else
	{
		int y;
		y=findy(x,h,m);
		printf("%d",y);
	}
	return 0;
}

#include <stdio.h>

int main()
{
	int x,hh,mm;
	int h,m;
	int a,i;

	scanf("%d", &x);
	scanf("%d %d", &hh, &mm);

	for(i=0; ; i++)
	{
	    h=hh%10;
		m=mm%10;

		mm=mm-x;
		if(mm<=0)
		{
			mm=mm+60;
			hh=hh-1;
		}
		if(hh<=0)
		{
			hh=hh+24;
		}

		if(h==7 || m==7)
		{
			break;
		}
	}
	printf("%d\n", i);
	return 0;
}

#include<stdio.h>

int main()
{
    int x, hr, mm, ans = 0, tm;

    scanf("%d", &x);
    scanf("%d %d", &hr, &mm);
    tm = (hr * 60) + mm;

    while(1)
    {
        hr = tm / 60;
        mm = tm % 60;
        if(hr % 10 == 7 || mm % 10 == 7)
            break;
        else
        {
            ans++;
            tm = tm - x;
        }
    }

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

    return 0;
}

#include <stdio.h>
#define nax 1000
int main()
{

	int x, hours,minutes;
	scanf("%i",&x);
	scanf("%d%d",&hours,&minutes);
	int newHour = minutes;
	int minus;
	int counter = 0 ;
	while (1){

		if (newHour % 10 == 7 || hours % 7 == 0 ){
			break;
		}

		if (newHour < x){
			minus = x - newHour;
			newHour = 60 - minus;
			hours -=1;
		}

		else
		{
			newHour -=  x;
		}
		counter += 1;

	}
	printf("%i",counter);

}

#include<stdio.h>
main()
{
    int x,minute,time_delay=0,k,hour;
    scanf("%d",&x);
    scanf("%d %d",&hour,&minute);
    hour = hour%10;
    while(1)
    {

        k = minute%10;

        if(k==7||hour==7)
            break;
            if(minute==0)
            {
                if(hour-1==7)
                {

                    minute--;
                    time_delay++;
                    break;
                }
                else
                    minute = 60;
            }
             minute--;
                  time_delay++;
    }
    if(time_delay/x==0)
        time_delay = time_delay%x;
    else
        time_delay = time_delay/x;

    printf("%d",time_delay);
}

#include<stdio.h>
main()
{

    int x,hour,d,minute,click=0,snooze,c;
    scanf("%d",&x);
    scanf("%d %d",&hour,&minute);
    c = hour;
    d = minute;
    while(1)
        {
            if(hour==0)
            {
                hour=24;

            }
    click++;

    if(x <= minute)
        snooze = minute-x;
    else
      {
          snooze = 60 + (minute-x);
             hour = hour-1;

        if(hour%10==7)
            {

            break;
            }
      }

        if(snooze%10==7)
        {

            break;
        }
        else
            minute=snooze;

}
if(c%10==7||d==0)
    click=0;
printf("%d",click);
}

#include<stdio.h>
int main()
{
    int x;int y,s;
    scanf("%d",&x);
    int a,b;
    scanf("%d%d",&a,&b);
    y=b%10;
    if(y<7)
    {
    y=y+3;
    }
    else
    {
        y=y-7;
    }
    if(y%x==0)
    {
        s=y/x;
    }
    else
    {
        s=y/x;
        s=s+1;
    }
    printf("%d",s);
}

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>

int x, hh, mm,ans=0x7fffffff,sh,sm,tmp1,tmp2;
int myabs(int x) { return x > 0 ? x : -x; }
int main()
{
    scanf("%d%d%d", &x, &hh, &mm);
    if (hh % 10 == 7 || mm % 10 == 7)
    {
        putchar('0');
        return 0;
    }
    tmp1 = hh * 60 + mm;
    for (sh = 0; sh < 24; ++sh)
    {
        for (sm = 0; sm < 60; ++sm)
        {
            if (sh == hh && sm == mm)continue;
            if (sh % 10 != 7 && sm % 10 != 7)continue;
            tmp2 = (sh * 60 + sm);
            tmp2 += tmp2 > tmp1 ? 24 * 60 : 0;
            tmp2 -= tmp1;
            if (tmp2 < 0)tmp2 = -tmp2;
            if (tmp2 % x == 0)
            {
                //printf("%d:%d - %d:%d is %d\n", hh, mm,sh, sm,tmp2);
                tmp2 /= x;
                if (tmp2 <= ans)ans = tmp2;
            }
        }
    }
    printf("%d", ans);
    return 0;
}

#include <stdio.h>

int main()
{
    int x, h, m, count;
    count = 0;
    scanf("%d %d %d", &x, &h, &m);
    for(;;)
    {
        if(m%10 == 7 || h%10 == 7)
        {
            printf("%d", count);
            break;
        }
        else
        {
            m = m - x;
            if(m <= 0)
            {
                h--;
                m = m + 60;
            }
            if(h < 0)
            {
                h = h + 24;
            }
        }
        count++;
    }
    return 0;
}

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

int main()
{
    int x,hh,mm,ans=0;
    scanf("%d %d %d",&x,&hh,&mm);
    while(mm%10!=7){
        if(mm>=x){
            mm-=x;
        }
        else{
            mm=60-(x-mm);
            if(hh==0){
                hh=23;
            }
            else{
                hh--;
            }
        }
        ans++;
        if(hh%10==7){
            break;
        }
    }
    printf("%d",ans);

    return 0;
}

#include<stdio.h>
main()
{
    int x,minute,time_delay=0,k,hour,c;
    scanf("%d",&x);
    scanf("%d %d",&hour,&minute);
    c=minute;
    hour = hour%10;
    while(1)
    {

        k = minute%10;

        if(k==7||hour==7)
            break;
            if(minute==0)
            {
                if(hour-1==7)
                {

                    minute--;
                    time_delay++;
                    break;
                }
                else
                    minute = 60;
            }
             minute--;
                  time_delay++;
    }
    if(time_delay/x==0)
        time_delay = 1;
    else
        time_delay = time_delay/x;
        if(hour==7||c%10==7)
            time_delay=0;

    printf("%d",time_delay);
}

#include<stdio.h>

int check(int h,int m)
{
	int hh[2]={0},mm[2]={0},i=0,j=0,count=0;
	while(h>0)
	{
		if(h%10==7)
		{
			count++;
		}
		h=h/10;
	}
	while(m>0)
	{
		if(m%10==7)
		{
			count++;
		}
		m=m/10;
	}
	return count;
}
int main()
{
	int x;
	scanf("%d",&x);
	int h,m,count1=0,count=0,i;
	scanf("%d %d",&h,&m);
	while(1)
	{
		count=check(h,m);
	//	printf("%d %d\n",h,m);
	//	printf("%d\n",count );
		if(count>0)
		{
	//		printf("%d\n",count1 );
			break;
		}
		else
		{
			h--;
			m=m-x;
			if(h<0)
			{
				h=24+h;
			}
			if(m<0)
			{
				m=60+m;
			}
			h=h%24;
			m=m%60;
			count1++;
		}
	}
	printf("%d\n", count1);
	return 0;
}

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>

int x, hh, mm,ans;
int main()
{
    scanf("%d%d%d", &x, &hh, &mm);
    while (mm-=x)
    {
        if (hh % 10 == 7 || mm % 10 == 7)break;
        if (mm < 0)
        {
            hh--;
            mm += 60;
        }
        if (hh < 0)hh += 24;
        ans++;
    }
    printf("%d", ans);
    return 0;
}

#include <stdio.h>

int main()
{
    int y=0;
    int x;
    int hh,mm;
    scanf("%d",&x);
    scanf("%d %d",&hh,&mm);

    while(1)
    {
        if((hh%10==7)||(mm%10==7))
        {
            break;
        }
        if(mm-x<0)  //分钟数小于7
        {
            mm=60+mm-x;//
            if(hh-1<0)
            {
                hh=24;
            }
            else
            {
                hh--;
            }
        }
        else
        {
            mm=mm-x;
        }
        //printf("\nhh:mm=%d:%d\n",hh,mm);
        y++;
    }
    printf("%d\n",y);

    return 0;
}

#include<stdio.h>
main()
{

    int x,hour,d,minute,click=0,snooze,c;
    scanf("%d",&x);
    scanf("%d %d",&hour,&minute);
    c = hour;
    d = minute;
    while(1)
        {
            if(hour==0)
            {
                hour=24;

            }
    click++;
    if(x < minute)
        snooze = minute-x;
    else
      {
          snooze = 60 + (minute-x);
             hour = hour-1;

      }

        if(hour%10==7)
            {

            break;
            }
        if(snooze%10==7)
        {
            click++;
            break;
        }
        else
            minute=snooze;

}
if(c%10==7||d==0)
    click=0;
printf("%d",click);
}

#include <stdio.h>

int islucky(int HH,int MM){
	if(HH%10==7 || HH/10==7 || MM%10==7 || MM/10==7) return 1;
	else return 0;
}

int main(){
	int a,b,HH,MM,t=0,n,x;
	scanf("%d",&x);
	scanf("%d %d",&HH,&MM);
	n=(HH*60)+MM;
	a=HH;
	b=MM;
	while(islucky(a,b)==0){
		t=t+1;
		n=n-x;
		a=n/60;
		b=n%60;
	}
	printf("%d",t);
}

#include<stdio.h>
int main()
{
    int x,h,m,a,y;
    scanf("%d%d%d",&x,&h,&m);
    if(m%10==7)
    {
        printf("0");
        return 0;
    }
    a=m;
    while(a%10!=7)
    {
        a--;
    }
    y=ceil((m-a)/x);
    printf("%d",y);
    return 0;
}

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

int main()
{
	int n = 0;
	int hh = 0, mm = 0;
	scanf("%d %d %d",&n,&hh,&mm);
	int count = 0;
	while(  mm % 10 != 7 && hh % 10 != 7)
	{
		count++;
		if ( mm > n )
		{
			mm -= n;
		}
		else if ( mm == n)
		{
			mm = 59;
			hh -= 1;
		}
		else
		{
			mm = mm + 60 - n;
			hh -= 1;
		}
	}
	printf("%d\n",count);
	return 0;
}

#include <stdio.h>

int main()
{
    int x, hh, mm, ans = 0;
    scanf("%d %d %d", &x, &hh, &mm);
    while(1) {
        if(mm % 10 == 7) {
            break;
        }
        if(--mm == 0) {
            mm = 59;
            if(--hh == 0) {
                hh = 23;
            }
        }
        ++ans;
    }
    printf("%d\n", ans / x);
    return 0;
}

#include <stdio.h>
int main()
{
    int x,hh,mm,c=0;
    scanf("%d%d%d",&x,&hh,&mm);
    while(1)
    {
        if(hh%10==7||mm%10==7)
        {printf("%d",c);
         break;
        }
        else
       { mm-=x;
        c++;
        if(mm<0)
        { mm+=59;
          hh--;
          if(hh<0)
          hh+=23;
        }
       }
    }

    return 0;
}

#include<stdio.h>

int seven(int x,int y)
{
	int p;
	if(x==7||y==7||(x/10)==7||(y/10)==7)
		return 1;
	x=x%10;
	y=y%10;
	if(x==7||y==7)
		return 1;
	else
		return 0;
}

int main()
{
	int x,hh,mm,i=0,p=0;
	scanf("%d%d%d",&x,&hh,&mm);
	for(i=0;i<10000;i++)
	{
		if(seven(hh,mm)==1)
		{
			break;
		}
		if(x<=mm)
		{
			mm=mm-x;
		}
		if(x>mm)
		{
			mm=60+mm-x;
			if(hh==0)
				hh=23;
			else
				hh=hh-1;
		}
		p++;
	}
	printf("%d\n",p);
	return 0;
}

#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
long long cmpfunc (const void * a, const void * b) {
   return ( *(long long*)a - *(long long*)b );
}

int main(void){

long long int test,i,j,n,count,flag=0,o1=0,o2=0,b1,x,m,l,max,sum2,min,f,c,r,o,sum1,sum=0,y,a,b,count1=0,a2,b2;

scanf("%lld%lld%lld",&n,&a,&b);
count=0;

while(a%10!=7 && b%10!=7){
    b-=3;
    count++;
    if(b<0){
        b=59+b;
        a--;
    }
    if(a<0){
        a=24;
    }
}
printf("%lld",count);
return 0;
}

#include<stdio.h>
struct time{
	int hour;
	int minute;
};
int whether(int x);
int main()
{
	struct time times;
	int x,item=0;
	scanf("%d%d%d",&x,&times.hour,&times.minute);

	while(1)
	{
		if(whether(times.hour)||whether(times.minute))
		{
			printf("%d\n",item);
			return 0;
		}
		else
		{
			if((times.minute-x)>=0)
			{
				times.minute-=x;
			}
			else {
				times.minute=times.minute+60-x;
				if(times.hour>=1)
					times.hour-=1;
				else
					times.hour=23;
			}
		}
		item++;
		//printf("%d:%d\n",times.hour,times.minute);
	}

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

#include<stdio.h>
struct time{
	int hour;
	int minute;
};
int whether(int x);
int main()
{
	struct time times;
	int x,item=0;
	scanf("%d%d%d",&x,&times.hour,&times.minute);

	while(1)
	{
		if(whether(times.hour)||whether(times.minute))
		{
			printf("%d\n",item);
			break;
		}
		else
		{
			if((times.minute-x)>=0)
			{
				times.minute-=x;
			}
			else {
				times.minute=times.minute+60-x;
				if(times.hour>=1)
					times.hour-=1;
				else
					times.hour=23;
			}
		}
		item++;
		printf("%d:%d\n",times.hour,times.minute);

	}

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

#include<stdio.h>
int yeah(int n)
{
    while(n){
        if(n%10==7) return 1;
        n/=10;
    }
    return 0;
}
int main()
{
    int h,m,time,count1=0,ans1,ans2,count2=0,count=0,x,i;
    scanf("%d",&x);
    scanf("%d %d",&h,&m);
    if((yeah(h)==1)||(yeah(m)==1)) printf("0\n");
    else {
            for(;;){
            if((m-x)==0) m=60;
            else if((m-x)<0) {
                m=60-(x-m);
                h-=1;
            }
            else m-=x;
            ++count;
            if(h<0) h=24+h;
            if((yeah(h)==1)||(yeah(m)==1)) break;

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

#include <stdio.h>
int check(int h,int m)
{
	if((h%10)==7 || (m%10)==7)
	{
		return 1;
	}
	else return 0;
}
int findy(int x,int h, int m)
{
	int i=0,k=0;
	if(m<x || h<=0)
	{
		if(m<x)
		{
			h--;
			m=(60+m)-x;
			i++;
			if(check(h,m))
				return i;
		}
		if(h<=0)
		{
			h=23;
			m=(60+m)-x;
			i++;
			if(check(h,m))
				return i;
		}
	}
	else
	{
		m=m-x;
		i++;
		if(check(h,m))
			return i;
	}
	return i+findy( x,h,m);
}
int main()
{
	int x,h,m;
	scanf("%d%d%d",&x,&h,&m);
	if(check(h,m))
	{
		printf("0");
		return 0;
	}
	else
	{
		int y;
		y=findy(x,h,m);
		printf("%d",y);
	}
	return 0;
}

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

int lucky_checker(int h,int m);
void time_minus(int *h, int *m);

int main()
{
	int x,hour,min,counter=0;
	float button_times;

	scanf("%d %d %d",&x,&hour,&min);

	while(! lucky_checker(hour,min)) {
		counter++;
		time_minus(&hour,&min);
		//printf("%d:%d\n",hour,min);
	}
	//printf("%d %d\n",hour,min);
	button_times = (float)counter/x;
	if (button_times>0 && button_times<1)
		button_times=1;

	printf("%d\n",(int)button_times);

//	check 7
//	no 7
//	minus 1 min
//	check 7 or change hour

	return 0;
}

int lucky_checker(int h,int m)
{
	int lucky=0,checker_7;
	while(h || m) {
		checker_7 = h%10;
		if (checker_7==7) {
			//printf("Hour yes\n");
			lucky=1;
			break;
		}
		h/=10;

		checker_7 = m%10;
		if (checker_7==7) {
			//printf("Min yes\n");
			lucky=1;
			break;
		}
		m/=10;
	}
	return lucky;
}

void time_minus(int *h, int *m)
{
	if (*m == 0) {
		*h-=1;
		*m = 59;
		if (*h < 0) {
			*h = 23;
		}
	}
	else {
		*m -=1;
	}
}

#include <stdio.h>

int main()
{
	int x;
	int h, m;
	scanf("%d%d%d",&x,&h,&m);
	int ans=0;
	while (m%10!=7 && h!=7){
		ans++;
		m -= x;
		if (m<=0){
			h--;
			m += 60;
		}
		if (h==0) h=12;
	}
	printf("%d",ans);
	return 0;
}

#include<stdio.h>

int min, hh, mm;
int cnt = 0;

int is_lucky(int hh, int mm) {
  return hh/10 == 7 || hh%10 == 7 || mm/10 == 7 || mm%10 == 7;
}

int get_lucky(int hh, int mm) {
  if(is_lucky(hh, mm))
    return hh*100 + mm;

  if(mm == 0) {
    if(hh == 0)
      return get_lucky(23, 59);
    return get_lucky(hh-1, 59);
  }
  return get_lucky(hh, mm-1);
}

int get_times(int lhh, int lmm) {
  if((lhh == hh && lmm == mm) || (lhh >= hh && lmm >= mm))
    return cnt;

  cnt++;
  lmm += min;
  if(lmm >= 60) {
    lhh += 1;
    lmm -= 60;
  }
  if(lhh >= 24)
    lhh = 0;
  return get_times(lhh, lmm);
}

int main() {
  scanf("%d %d %d", &min, &hh, &mm);
  int luck_hh = get_lucky(hh, mm) / 100, luck_mm = get_lucky(hh, mm) % 100;

 printf("%d\n",get_times(luck_hh, luck_mm));
}

#include <stdio.h>
#include <string.h>
int main()
{
   int x,hh,mm,i,flag=0,l,point,h;
   scanf("%d",&x);
   scanf("%d%d",&hh,&mm);
   h=hh;
   for (i=mm;i>=0;i--)
   {
   	 if (i==7||i==17||i==27||i==37||i==47||i==57)
	 {
	 	flag =1;
	 	point=i;
	 	break;
	 }
   }
   if (flag==0)
   {
   	  h=hh-1;
   	  point=57;
   }
   if (h==hh)
   {
   	  l=(mm-point)/x;
   }
   else if(h!=hh)
   {
   	l=(3+mm)/x;
   }
   printf("%d\n",l);
}

#include <stdio.h>
#include <string.h>
int counter;

void check(int hh, int mm, int snooze){

	if (mm % 10 == 7 || hh % 10 == 7){
		//memset(counter,0,sizeof(counter));
		printf("%d",counter);
	}
	else{
		do{
			mm -= snooze;
			if(mm < 0){
				mm = mm + 60;
				hh--;
			}
			counter++;
		}while(mm % 10 != 7);
	printf("%d",counter);
	}

}

int main(void){
	int snooze;
	int hh, mm;
	//int finalcounter=0;

	scanf("%d",&snooze);
	scanf("%d %d", &hh, &mm);

	check(hh,mm,snooze);

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

}

#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>

int x, hh, mm,ans=0x7fffffff,sh,sm,tmp1,tmp2;
int myabs(int x) { return x > 0 ? x : -x; }
int main()
{
    scanf("%d%d%d", &x, &hh, &mm);
    if (hh % 10 == 7 || mm % 10 == 7)
    {
        putchar('0');
        return 0;
    }
    tmp1 = hh * 60 + mm;
    for (sh = 0; sh < 24; ++sh)
    {
        for (sm = 0; sm < 60; ++sm)
        {
            if (sh == hh && sm == mm)continue;
            if (sh % 10 != 7 && sm % 10 != 7)continue;
            tmp2 = myabs(tmp1 - (sh*60+sm));
            if (tmp2 % x == 0)
            {
                //printf("%d:%d - %d:%d is %d\n", hh, mm,sh, sm,tmp2);
                tmp2 /= x;
                if (tmp2 <= ans)ans = tmp2;
            }
        }
    }
    printf("%d", ans);
    return 0;
}

#include<stdio.h>
int main()
{
    int x,h,m,a,y;
    scanf("%d%d%d",&x,&h,&m);
    if(m%10==7)
    {
        printf("0");
        return 0;
    }
    a=m;
    while(a%10!=7)
    {
        a--;
    }
    y=ceil((m-a)/x);
    printf("%d",y);
    return 0;
}

#include <stdio.h>
#define nax 1000
int main()
{

	int x, hours,minutes;
	scanf("%i",&x);
	scanf("%d%d",&hours,&minutes);
	int newHour = minutes;
	int minus;
	int counter = 0 ;
	while (1){

		if (newHour % 10 == 7)
			break;
		if (newHour < x){
			minus = x - newHour;
			newHour = 59 - minus;
		}
		else
		{
			newHour -=  x;
		}
		counter += 1;

	}
	printf("%i",counter);

}

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

int main(int argc, char *argv[])
{
	int x, hh, mm, dist = 0, res = 0;
	scanf("%d", &x);
	scanf("%d%d", &hh, &mm);

	while ((hh - 7) % 10 != 0 && (mm - 7) % 10 != 0) {
		if (mm == 0) {
			mm = 60;
			if (hh == 0) {
				hh = 24;
			}
			--hh;
		}
		--mm;
		if (dist++ % x == 0)
			++res;
	}

	printf("%d", res);
	return 0;
}

#include <stdio.h>

int main()
{
    int x, h, m, count;
    count = 0;
    scanf("%d %d %d", &x, &h, &m);
    for(;;)
    {
        if(m%10 == 7 || h%10 == 7)
        {
            printf("%d", count);
            break;
        }
        else
        {
            m = m - x;
            if(m <= 0)
            {
                h--;
                m = m + 60;
            }
            if(h < 0)
            {
                h = h + 24;
            }
        }
        count++;
    }
    return 0;
}