琪露诺的算术教室

来源:互联网 发布:极客学院源码下载 编辑:程序博客网 时间:2024/05/01 05:52

琪露诺的算术教室

Time Limit: 1000ms

Memory Limit: 65536KB

Description

给出一个非负整数A,将这个数字的最低位移动到最高位(原来的最高位变为次高位,次低位变成最低位),得到非负整数B,发现B恰好是A的k倍。现给出A的最低位的值n,和倍数k,求最小的非负整数B。

Input

第一行输入一个正整数T(1 <= T <= 1000),表示有T组测试数据。对于每组测试数据:输入两个整数n,k(0<=n<=9 , 0<=k<=9)。

Output

对于每组测试数据,输出一个非负整数B,若无解,请输出-1。

Sample Input

12 2

Sample Output

210526315789473684

#include <iostream>using namespace std;#include <stdio.h>#include <cmath>int a[5000005];int main(){    int T,n,k,q;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&k);        if(n==0)        {            printf("0\n");            continue;        }        if(k==0)        {            printf("-1\n");            continue;        }        if(k>n)        {            printf("-1\n");            continue;        }            a[0]=n;        int t=0,i=1,flag=-1;        while(flag!=1)        {            a[i]=(t+a[i-1]*k)%10;            t=(t+a[i-1]*k)/10;            i++;            if(i>=1005)            {                printf("-1\n");                break;            }            if(a[i-1]==n && t==0 )            {                flag=1;                for(int q=i-1; q>=1; q--)                    printf("%d",a[q]);                printf("\n");                break;            }        }    }    return 0;}

0 0