Find The Multiple POJ
来源:互联网 发布:php 高德地图api 编辑:程序博客网 时间:2024/06/07 21:12
点击打开链接
利用了同余定理
(a*b)%m=((a%m)*(b%m))%m
(a+b)%m=a%m+b%m;
在这里 判断一个较大数是否为目标数n的倍数时(即%n) 等同于判断较大数的两个因子是否为目标数n的倍数 类似于快速幂的思想
基于这个规律 我们从高位开始构造这个数(最高位必是1) 而下一位有两个选择 因此采用DFS
如 目标数为8 当前已构造的数为1011 继续构有10110与10111两种情况
10110%8 = (1011*10)%8
= ((1011%8)*(10%8))%8
10111%8 = (1011*10+1)%8
= (1011*10)%8+1%8
= ((1011%8)*(10%8))%8+1
而式中 1011%8 又是当前构造数的余数 依次向前推 每个构造数取模 都可以将一部分转换为前一个构造数取模的结果
此题BFS亦可
#include <stdio.h>int ans[110];int n,flag;void dfs(int step,int remain);int main(){ int i; while(scanf("%d",&n)!=EOF) { if(n==0) break; if(n==1) { printf("1\n"); } else { ans[1]=1; flag=0; dfs(2,1); } } return 0;}void dfs(int step,int remain){ int t,i; if(remain==0) { flag=1; for(i=1;i<=step-1;i++) { printf("%d",ans[i]); } printf("\n"); return; } if(step==101) return; t=remain*10%n; ans[step]=0; dfs(step+1,t); if(flag==1) return; ans[step]=1; dfs(step+1,t+1); if(flag==1) return; return;}
阅读全文
0 0
- poj find the multiple
- Find The Multiple POJ
- POJ.Find The Multiple
- Find The Multiple POJ
- Find The Multiple POJ
- Find The Multiple POJ
- Find The Multiple POJ
- Find The Multiple POJ
- Find The Multiple POJ
- POJ-Find The Multiple
- poj 1426 Find The Multiple
- Poj 1426 Find The Multiple
- POJ 1426 Find the Multiple
- poj 1426Find The Multiple
- POJ 1426 Find The Multiple
- POJ 1426 Find The Multiple
- POJ 1426 Find The Multiple
- POJ 1426 Find The Multiple
- 时间过渡和鼠标hover
- mysql索引列为空,也可以走索引
- 怎么理解面向对象和面向过程到底的本质区别?
- HTML5的离线存储
- 设置input标签内容不能复制
- Find The Multiple POJ
- 杭电acm—2076 夹角有多大(题目已修改,注意读题)
- Java中json字符串的格式转换(缩进换行)
- LeetCode 563 Binary Tree Tilt
- js,angularjs 图片上传服务器,并解决照片旋转问题,前端图片压缩上传
- tensorflow训练权重保存和调用——tf.saver()
- 模拟实现STL中的list
- 漫话数据采集之杂谈
- SSH下,处理问题No Session,createCriteria,Could not open connection