Live2d Test Env

zoj 1889 ones 数学

Ones

Time Limit: 2 Seconds      Memory Limit: 65536 KB

Given any integer 0 <= n <= 10000 not divisible by 2 or 5, some multiple of n is a number which in decimal notation is a sequence of 1's. How many digits are in the smallest such a multiple of n?


Sample Input

3
7
9901


Sample Output

3
6
12

做题经验:把1...1(n)转化为9...9(n)/9,再转化为1...0(n+1)-1。如1111换成9999=10000-1

然后就可以结合数学姿势同余乱搞了。

#include<cstdio>
#include<cstdlib>
#include<iostream>
using namespace std;

int main()
{
	int n;
	while(cin>>n)
	{
		if(n==0){cout<<1<<endl;continue;}//这一行不要也,但题目说了n=0是存在的,以防万一
		n*=9;
		int cnt=1;
		long long ans=9;
		while(ans%n!=0)
		{
			ans=(((ans+1)*10)%n-1)%n;
			cnt++;
		}
		cout<<cnt<<endl;
	}
	return 0;
} 

当然直接搞也可以,如下(暗含了秦九韶思想)




 

posted @ 2017-09-27 21:06  nimphy  阅读(167)  评论(0编辑  收藏  举报