POJ 1426 Find The Multiple
Find The Multiple
Time Limit: 1000MS | Memory Limit: 10000K | |||
Total Submissions: 46927 | Accepted: 19608 | Special Judge |
Description
Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains only the digits 0 and 1. You may assume that n is not greater than 200 and there is a corresponding m containing no more than 100 decimal digits.
Input
The input file may contain multiple test cases. Each line contains a value of n (1 <= n <= 200). A line containing a zero terminates the input.
Output
For each value of n in the input print a line containing the corresponding value of m. The decimal representation of m must not contain more than 100 digits. If there are multiple solutions for a given value of n, any one of them is acceptable.
Sample Input
2
6
19
0
Sample Output
10
100100100100100100
111111111111111111
Source
思路: 使用dfs枚举所有可能的结果(01串),当该结果可以整除n时,就输出。由于long long最多可以表示19位数字,所以当该数的位数大于19时,return。
1 #include <iostream> 2 3 using namespace std; 4 5 int n, flag; 6 7 void dfs(int k, long long cur) // k:位数 cur:当前数字 8 { 9 if (k == 19 || flag) // long long最多只能表示19位数 10 return; 11 12 if (cur % n == 0) { 13 cout << cur << endl; 14 flag = 1; 15 return; 16 } 17 for (int i = 0; i <= 1; ++i) 18 { 19 dfs(k + 1, cur * 10 + i); 20 } 21 } 22 23 24 int main() 25 { 26 while (cin >> n, n != 0) { 27 flag = 0; 28 dfs(0, 1); 29 } 30 return 0; 31 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南