bjfu---1196fudq and Digital

fudq and Digital

时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte
总提交:18            测试通过:2

描述

The other day, AC Goddness asked fudq for help as she was in trouble in a easy problem.The problem requires to find the minimum nonnegative integer M when given an integer N.M meets this condition:
(M^2)%(10^x)=N (x=0,1,2,3...).
fudq don't want to lose face in front of AC Goddness.So can you help fudq?

输入

The first line has an integer T(T <= 1000), the number of test cases.
For each case, each line contains one integer N(0 <= N <= 1,000,000,000), indicating the given number.

输出

For each case output the answer if it exists, otherwise print “Sorry, I tried.”.

样例输入

3
3
21
25

样例输出

Sorry, I tried.
11
5

提示

 

M^2 = M*M;

Code:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define NN 299999999
int flag;int ll;__int64 wei;int jie;
void FF(int len,__int64 M,__int64 t,__int64 N){
    int i;
    while(len--){//len代表位数;
        //if(flag==1) break;
        for(i=0;i<=9 ;i++){
            if(((M+i*t)*(M+i*t))%(t*10) == (N)%(t*10)){
                M=M+i*t;t=t*10;
                if(M*M%(wei)==N) {if(M<jie)jie=M; flag=1;break;}
                else {
                    t=t/10;M=M-i*t;  // 这里需要注意! 在参数中改变传入值,但M的值不能动。
                                    //否则递归时因为M变化会出错!
                    FF(len,M+i*t,t*10,N);
                }
            }
        }
        if(i>=10 ){if(flag!=1){flag=0;return;} break;}
    }
}
int main()
{
    freopen("in.txt","r",stdin);
    freopen("out.txt","w",stdout);
    __int64 i,j,k;__int64 a,t,num,n,m,N,T,M;
    scanf("%I64d",&T);
    while(T--){
        int len;
        scanf("%I64d",&N);
        a=N%10;
        flag=-2;
        if(a==2 || a==3 || a==7 || a==8) flag=0;
        else{
            len=0;
            M=N;
            while(M){len++;M=M/10;} ll=len;
            wei=1;for(i=1;i<=ll;i++) wei=wei*10;
            t=1;M=0;
            jie=NN;
            FF(len,M,t,N);
        }
        if(flag==1)
            if(jie>NN) printf("Sorry, I tried.\n");
            else printf("%I64d\n",jie);
        else if(flag==-1) printf("0\n");
        else{
            if(N==0) printf("0\n");
            else printf("Sorry, I tried.\n");
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值