| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
我用的贪心,每次贪出一位数字,自己用计算器测试都对,不过WA,难道是因为数据没改过来?//用double挺爽的,不过想用下别的方法
//每次贪出一位数字,最后验证。
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
int n;
char s[200],s0;
bool bijiao(long long a){
char x[200],x0;
long long b;
int q,w,e;
x[1]=1;x0=1;
for(q=1;q<=n;q++){
b=0;
for(w=1;w<=x0;w++){
b+=a*x[w];
x[w]=b%10;
b/=10;
}
while(b){
x[++x0]=b%10;
b/=10;
}
}
if(x0!=s0)return 0;
else{
for(q=1;q<=x0;q++)if(x[q]!=s[q])break;
if(q>x0)return 1;
else return 0;
}
}
int digui(int i,int j,long long a){
long long b,c;
int q,w,e;
for(q=1,e=1;q<=i;q++)e*=10;
for(q=0;q<=9;q++){
if(i==j&&q==0)continue;
b=q;
for(w=2;w<=i;w++)b*=10;
b+=a;
c=1;
for(w=1;w<=n;w++)c=c*b%e;
for(w=1;w<=i;w++)if(c%10!=s[w])break;else c/=10;
if(w>i){
if(i==j){
if(bijiao(b))return b;
}else{
w=digui(i+1,j,b);
if(w>=0)return w;
}
}
}
return -1;
}
int main(){
int q,w,e;
char s1[200];
while(scanf("%d",&n)!=-1){
scanf("%s",s1);
s0=strlen(s1);
for(q=s0;q>0;q--)s[q]=s1[s0-q]-'0';
q=(s0-1)/n+1;
printf("%d\n",digui(1,q,0));
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator