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 ...
随机推荐
- Axis1.4 配置数组类型复杂对象
最近 项目对接 webservice,要求SOAP 标准是1.1,然后在axis 和 spring ws 和 cxf 之间进行选择,然后axis 可以自定义服务,然后随tomcat启动发布,sprin ...
- Letters Shop
B. Letters Shop time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- java并发编程(二十五)----(JUC集合)LinkedBlockingDeque和ConcurrentLinkedDeque介绍
Queue除了前面介绍的实现外,还有一种双向的Queue实现Deque.这种队列允许在队列头和尾部进行入队出队操作,因此在功能上比Queue显然要更复杂. LinkedBlockingDeque 我们 ...
- [趣学程序]java的常用类之String
java基础之常用类 String类 String表示字符串,所谓字符串,就是一连串的字符,是java中最常用的类之一. String是不可变类,一旦String对象被创建,包含在对象中的字符序列(内 ...
- ImageLoader_显示图片
public class MainActivity extends AppCompatActivity { private ListView lv; private List<Bean.Resu ...
- memcached中hash表相关操作
以下转自http://blog.csdn.net/luotuo44/article/details/42773231 memcached源码中assoc.c文件里面的代码是构造一个哈希表.memc ...
- SQL语句完成Excel数据导入数据库表中流程方法及注意事项
第一步:先查看数据库是否安装AccessDatabaseEngine_X64.exe, 如下图查看: 如果未安装先下载脚本之家下载地址 https://www.jb51.net/softs/29150 ...
- 100天搞定机器学习|day39 Tensorflow Keras手写数字识别
提示:建议先看day36-38的内容 TensorFlow™ 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes)在图中表示数学操作,图中的线(edge ...
- 用gcc/g++编译winsock程序
用gcc/g++编译winsock程序 D:\My\code>gcc -o getweb.exe getweb.c -lwin32socket 如果不加此句 -lwin32socket 编译会报 ...
- 消息中间件——RabbitMQ(四)命令行与管控台的基本操作!
前言 在前面的文章中我们介绍过RabbitMQ的搭建:RabbitMQ的安装过以及各大主流消息中间件的对比:,本章就主要来介绍下我们之前安装的管控台是如何使用以及如何通过命令行进行操作. 1. 命令行 ...