Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

我用的贪心,每次贪出一位数字,自己用计算器测试都对,不过WA,难道是因为数据没改过来?

Posted by jxfzlmb at 2008-12-10 14:20:59 on Problem 2109 and last updated at 2008-12-10 14:22:13
//用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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator