Position in Fraction (模拟)

You have a fraction . You need to find the first occurrence of digit c into decimal notation of the fraction after decimal point.


Input

The first contains three single positive integers a, b, c (1 ≤ a < b ≤ 105, 0 ≤ c ≤ 9).

Output

Print position of the first occurrence of digit c into the fraction. Positions are numbered from 1 after decimal point. It there is no such position, print -1.

Examples
Input
1 2 0
Output
2
Input
2 3 7
Output
-1
Note

The fraction in the first example has the following decimal notation: . The first zero stands on second position.

The fraction in the second example has the following decimal notation: . There is no digit 7 in decimal notation of the fraction.

题意:求a/b的小数部分中第几位是c

思路:模拟除法,现将a,b约分,减少运算次数

代码:

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5;
int gcd(int a,int b){
	while(b){
		int c=a%b;
		a=b;
		b=c;
	}
	return a;
}
int main(){
	int n,m,c;
	cin>>n>>m>>c;
	int temp=gcd(n,m);
	n/=temp;
	m/=temp;
	int ans=-1;
	for(int i=1;i<=N;i++){
		n*=10;
		if(n/m==c){
			ans=i;
			break;
		}
		n%=m;
	}
	cout<<ans<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/islittlehappy/article/details/79992227