Niven Numbers

My Tags   (Edit)
  Source : Unknown
  Time limit : 1 sec   Memory limit : 32 M

Submitted : 5349, Accepted : 965

A Niven number is a number such that the sum of its digits divides itself. For example, 111 is a Niven number because the sum of its digits is 3, which divides 111. We can also specify a number in another base b, and a number in base b is a Niven number if the sum of its digits divides its value.

Given b (2 <= b <= 10) and a number in base b, determine whether it is a Niven number or not.

Input

Each line of input contains the base b, followed by a string of digits representing a positive integer in that base.
There are no leading zeroes. The input is terminated by a line consisting of
0 alone.

Output

For each case, print "yes" on a line if the given number is a Niven
number, and "no" otherwise.

Sample Input

10 111
2 110
10 123
6 1000
8 2314
0

Sample Output

yes
yes
no
yes
no

本题意思为给定一个进制(base),然后给一个在base进制下的数字NUM,判断NUM是否为尼玛数(NUM能否被NUM各位数字之和整除) 由于NUM的大小限制在int内,而base会让int型超出范围,比如8(10) = 1000(2),所以NUM需要为字符串类。第二个关键在于溢出处理,同HOJ1008中,
整除判断可以变求余边扩展。
 #include<iostream>
using namespace std;
#include<string> int main(){
int base; string num;
while(cin>>base && base != ){
cin>>num;
int x = ; int y = ;
for(int i = ;i < num.length();i++){
x += num[i] - '';
}
for(int i = ;i < num.length();i++){
y = y * base + num[i] - '';
y %= x;
} if(y == ){
printf("yes\n");
}else{
printf("no\n");
}
}
return ;
}

HOJ1014的更多相关文章

  1. OJ题目分类

    POJ题目分类 | POJ题目分类 | HDU题目分类 | ZOJ题目分类 | SOJ题目分类 | HOJ题目分类 | FOJ题目分类 | 模拟题: POJ1006 POJ1008 POJ1013 P ...

随机推荐

  1. MySQL- InnoDB锁机制

    InnoDB与MyISAM的最大不同有两点:一是支持事务(TRANSACTION):二是采用了行级锁.行级锁与表级锁本来就有许多不同之处,另外,事务的引入也带来了一些新问题.下面我们先介绍一点背景知识 ...

  2. 转发:为什么Android使用弱加密算法?

      Android 2.2.1默认使用的加密算法是AES 256-SHA1,但从2010年发布的Android 2.3开始,它默认使用的是一种更弱的加密算法 RC4-MD5. 当Android应用建立 ...

  3. uva 10051 Tower of Cubes(DAG最长路)

    题目连接:10051 - Tower of Cubes 题目大意:有n个正方体,从序号1~n, 对应的每个立方体的6个面分别有它的颜色(用数字给出),现在想要将立方体堆成塔,并且上面的立方体的序号要小 ...

  4. 幸运大转盘-jQuery+PHP实现的抽奖程序

    目前好多网站上应用的转盘抽奖程序大多是基于flash的,而本文结合实例将使用jQuery和PHP来实现转盘抽奖程序,为了便于理解,作者分两部分来讲解,本文讲解第一部分,侧重使用jQuery实现转盘的转 ...

  5. 混淆篇之原生DOM操作方法小结

    1.0   DOM结构 1.1先来看结构图: 父节点        兄弟节点        当前节点            属性节点            子节点        兄弟节点一般任意一个节 ...

  6. (跨平台)cocos2d-x C++ or Object-C(前端)调用C# webservices(后台),实现交叉编译到Android/IOS/WinPhone等移动终端设备

    1.2014年4月2号算是正式找到自己的实习工作-杭州美迪软件有限公司(移动物联事业部)合作于:四川管家婆总部移动终端代理,由于在校选编程专业语言C#和在浙大网新培训课程(C#.Asp.net开发)缘 ...

  7. leetcode Binary Tree Inorder Traversal python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

  8. [分 享] PHPCMS V9 更换域名,附件地址无法批更新(更换变便)问题>解决方法!!

    大家应该都有在域名更换(比如说,从本地上传到空间)的情况下,用内容>附件管理>附件地址替换 功能. 基本上替换不了所有表中原来域名的地址. 现PHPCMS V9最新版本的依旧存在些问题. ...

  9. Properties读写资源文件

    Java中读写资源文件最重要的类是Properties,功能大致如下: 1. 读写Properties文件 2. 读写XML文件 3. 不仅可以读写上述两类文件,还可以读写其它格式文件如txt等,只要 ...

  10. HTML5 canvas准备知识

    利用canvas来进行画图工作.因此,我们有必要进行一些画图方面的术语说明. 一.画布 在日常生活中,如果我们要画画,可以找纸.板.画布等等工具.而在网页元素中,我们只需要定义一个标签即可. < ...