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

无聊水题

Posted by KatrineYang at 2016-08-21 23:45:09 on Problem 1426
#include <iostream>
#include <stdio.h>
using namespace std;

int mx(int a, int b){
	return (a>b)? a:b;
}

int main() {
	int n;
	while(1){
		scanf("%d", &n);
		if(n <= 0) return 0;
		int mod[100];
		mod[0] = 1;
		int tag = -1;
		int n2 = 0, n5 = 0;
		while(n%2 == 0){
			n/=2;
			n2++;
		}
		while(n%5==0){
			n/=5;
			n5++;
		}
		if(n == 1){
			printf("1");
			for(int i = 0; i < mx(n2, n5); i++) printf("0");
			printf("\n");
			continue;
		}
		for(int i = 1; i < 100; i++){
			mod[i] = (mod[i-1]*10)%n;
		}
		bool dp[101][200], trc[101][200];
		dp[1][1] = 1; dp[1][0] = 0; trc[1][1] = 1;
		for(int i = 2; i < n; i++) dp[1][i] = 0;
		for(int i = 2; i <= 100; i++){
			for(int j = 0; j < n; j++){
				if(dp[i-1][j]){
					dp[i][j] = 1;
					trc[i][j] = 0;
				}
				else{
					int orig = (n+j-mod[i-1])%n;
					if(dp[i-1][orig]){
						dp[i][j] = 1;
						trc[i][j] = 1;
					}
					else dp[i][j] = 0;
				}
				if(j == 0 && dp[i][j]){
					tag = i;
					goto done;
				}
			}
		}
		done:
		int j = 0;
		for(int i = tag; i > 0; i--){
			if(trc[i][j]) printf("1");
			else printf("0");
			if(trc[i][j]) j = (n+j-mod[i-1])%n;
		}
		int n25 = mx(n2, n5);
		for(int i = 0; i < n25; i++) printf("0");
		printf("\n");
	}
	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