lightoj 1028 - Trailing Zeroes (I)(素数筛)
We know what a base of a number is and what the properties are. For example, we use decimal number system, where the base is 10 and we use the symbols - {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. But in different bases we use different symbols. For example in binary number system we use only 0 and 1. Now in this problem, you are given an integer. You can convert it to any base you want to. But the condition is that if you convert it to any base then the number in that base should have at least one trailing zero that means a zero at the end.
For example, in decimal number system 2 doesn't have any trailing zero. But if we convert it to binary then 2 becomes (10)2 and it contains a trailing zero. Now you are given this task. You have to find the number of bases where the given number contains at least one trailing zero. You can use any base from two to infinite.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains an integer N (1 ≤ N ≤ 1012).
Output
For each case, print the case number and the number of possible bases where N contains at least one trailing zero.
题意:给定一个10进制数n, n <= 10 ^ 12, 问把它转换成哪一些进制的数,这个数末尾会有0。
其实就是问你他的约数个数,由于题中给的组数有点大10的4次直接求因子会超时,所以要换种方法求。
由于每个数都可以化为几个素数的积,所以可以利用这种思想
a=prime1^a1 * prime2^a2 * prime^a3......
sum=(a1 + 1) * (a2 + 1) * (a3 + 1)......
这题还有一些要优化的东西具体优化看一下代码。
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
using namespace std;
typedef long long ll;
const int M = 1e6 + 10;
int prime[M];
int a[M];
bool isprime[M];
int counts;
void getprime() {
isprime[0] = isprime[1] = false;
isprime[2] = true;
for(int i = 3 ; i <= M ; i++) {
isprime[i] = i % 2 ? true : false;
}
int t = (int)sqrt(M * 1.0);
for(int i = 3 ; i <= t ; i++) {
if(isprime[i]) {
for(int j = i * i ; j <= M ; j += i) {
isprime[j] = false;
}
}
}
counts = 0;
for(int i = 2 ; i <= M ; i++) {
if(isprime[i]) {
prime[counts++] = i;
}
}
}
int main()
{
int t;
getprime();
scanf("%d" , &t);
int ans = 0;
while(t--) {
ans++;
ll n;
scanf("%lld" , &n);
ll sum = 1;
for(int i = 0 ; (ll)prime[i] * prime[i] <= n ; i++) {
int flag = 0;
while(n % prime[i] == 0) {
n /= prime[i];
flag++;
}
sum *= (flag + 1);
}
if(n > 1) {
sum *= 2;
}
sum--;
printf("Case %d: %lld\n" , ans , sum);
}
return 0;
}
lightoj 1028 - Trailing Zeroes (I)(素数筛)的更多相关文章
- LightOJ 1028 - Trailing Zeroes (I) 质因数分解/排列组合
题意:10000组数据 问一个数n[1,1e12] 在k进制下有末尾0的k的个数. 思路:题意很明显,就是求n的因子个数,本来想直接预处理欧拉函数,然后拿它减n就行了.但注意是1e12次方法不可行.而 ...
- LightOJ 1138 Trailing Zeroes (III)(二分 + 思维)
http://lightoj.com/volume_showproblem.php?problem=1138 Trailing Zeroes (III) Time Limit:2000MS M ...
- LightOj 1197 Help Hanzo 区间素数筛
题意: 给定一个区间a,b,a-b>=100000,1<=a<=b<=231,求出给定a,b区间内的素数的个数 区间素数筛 (a+i-1)/ ii向上取整,当a为 i 的整数倍 ...
- Light OJ 1028 - Trailing Zeroes (I) (数学-因子个数)
题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1028 题目大意:n除了1有多少个因子(包括他本身) 解题思路:对于n的每个因子 ...
- LightOj 1138 - Trailing Zeroes (III) 阶乘末尾0的个数 & 二分
题目链接:http://lightoj.com/volume_showproblem.php?problem=1138 题意:给你一个数n,然后找个一个最小的数x,使得x!的末尾有n个0:如果没有输出 ...
- LightOj 1090 - Trailing Zeroes (II)---求末尾0的个数
题目链接:http://lightoj.com/volume_showproblem.php?problem=1090 题意:给你四个数 n, r, p, q 求C(n, r) * p^q的结果中末尾 ...
- lightoj 1138 - Trailing Zeroes (III)【二分】
题目链接:http://lightoj.com/volume_showproblem.php? problem=1138 题意:问 N. 末尾 0 的个数为 Q 个的数是什么? 解法:二分枚举N,由于 ...
- Lightoj 1090 - Trailing Zeroes (II)
题目连接: http://www.lightoj.com/volume_showproblem.php?problem=1090 题目大意: 给出n,r,p,q四个数字1<=n,r,p,q< ...
- LightOJ 1138 Trailing Zeroes (III) 打表
就是统计5,然后当时因为发现最多有8000w个5的倍数,然后8000w/100,是80w,打表,二分找 然后我看网上的都是直接二分找,真是厉害 #include <cstdio> #inc ...
随机推荐
- java学习笔记(中级篇)—java实现高质量图片压缩
使用java几十行代码实现一个高质量图片压缩程序,再也不用去自己找网络的压缩程序啦!而且很多网上的工具还有水印或者其他的限制,自己动手写一个简单的应用,是再合适不过了. 一.实现原理 1.声明两个字符 ...
- JS构建多端应用
JS构建多端应用 一,需求与介绍 1.1,介绍 1,Taro 是一套遵循 React语法规范的 多端开发 解决方案.现如今市面上端的形态多种多样,Web.React-Native.微信小程序等各种端大 ...
- .Net异步编程详解入门
前言 今天周五,早上起床晚了.赶着挤公交上班.但是目前眼前有这么几件事情.刷牙洗脸.泡牛奶.煎蛋.在同步编程眼中.先刷牙洗脸,然后烧水泡牛奶.再煎蛋,最后喝牛奶吃蛋.毫无疑问,在时间紧促的当下.它完了 ...
- 6、高级的数组的复制(test4.java)
这里指的高级,并不是过么高大上,而是说我们可以调用系统函数,直接对数组进行复制,并且这个函数的强大并不止局限于,对数组的复制,而且可以对数组进行截取,在指定位置插入或删除某个元素. 本篇只介绍数组的复 ...
- 2、JAVA相关基础的学习和工具
个人感觉,各种语言的基础知识,例如标识符,运算符等在宏观上几乎是一样的,只是在某些方面上会有一点点差异,因为本人已经有了语言基础,所以对于标识符,关键字,运算符等方面的只是便不作赘述,敬请谅解,如果你 ...
- java学习-NIO(五)NIO学习总结以及NIO新特性介绍
我们知道是NIO是在2002年引入到J2SE 1.4里的,很多Java开发者比如我还是不知道怎么充分利用NIO,更少的人知道在Java SE 7里引入了更新的输入/输出 API(NIO.2).但是对于 ...
- redhat linux 5.3安装activeMQ
安装环境:linux redhat enterprise 5.3 activemq版本:5.9.01.从http://activemq.apache.org/download.html地址下载apac ...
- 走进JavaWeb技术世界5:初探Tomcat的HTTP请求过程
初探Tomcat的HTTP请求过程 前言:1.作为Java开发人员,大多都对Tomcat不陌生,由Apache基金会提供技术支持与维护,因为其免费开源且易用,作为Web服务器深受市场欢迎,所以有必要对 ...
- Cause: java.lang.NumberFormatException: For input string: "D"
异常:Cause: java.lang.NumberFormatException: For input string: "D" 问题回显: 原因分析:'D'只有1位,被认为是ch ...
- IDEA编辑器
一.打开含有jsx语法的文件都会显示红线,提示export declarations are not supported bu current javascript version 解决办法: 二.I ...