UVA

/*
一开始没怎么想,直接就用一想就能想到的方法,果不其然,TLE
TLE代码:
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
ll n;

int main()
{
	int a, x;
	while (cin >> a)
	{
		ll sum = 0;
		for ( x = 0; ; x++)
		{
			sum += pow(10, x);
			if (sum % a == 0) break;
		}
		cout << ++x << endl;
		
	}
	return 0;
}

后来想起之前问过的一道题,数论里面经常用到的取模...再就是,得到的那个形式的数字,就是累加后的和,p,是所有位数都是1的一个数,除了pow函数,也能直接用循环实现,这样的话,还能每处理一次,cnt自增一次,否则pow函数需要多次调用,求和还得用一次循环...效率肯定就不高了

这题突破的关键点,就是最终是为了实现,取模a得到余数是0,那么在累加的过程中,就相当于步步都能取模a,知道取模后的结果为0,这时再输出数字中1的个数

以及,这题为了得到全是1的那个数,其实很巧妙,见上上段

但我自己做的时候,就太想当然了,题目有求和就求和,题目有幂就用pow,根本不动脑子想想怎么优化,难怪TLE

注意注意!!!取模是很好的优化方式!!以及,数论忘得太多了,虽然本就没怎么学过,得好好补补
*/



#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
ll n;

int main()
{
	int a;
	while (cin >> a)
	{
		int ans = 1, x = 1;
		
		while (x % a)
		{
			x = ( (x * 10) + 1 ) % a;
			ans++;
		}
		cout << ans << endl;
		
	}
	return 0;
}


原文地址:https://www.cnblogs.com/mofushaohua/p/7789482.html