UCF Local Programming Contest 2018 D. Rounding Many Ways

Timothy and Alex’s hopes and dreams of running for UCF's Student Government Association have been crushed by the realization that their campaign ticket would not be alliterative. So, they have decided to analyze statistics from many different polls given to students to determine what pair of programming team members would be best situated to win the election. However, there is a problem. All of the statistics have been rounded off. This would not be an issue apart from the fact that the pollsters forgot to mention how the number was rounded! (Math Terminology Note: if we round, say, 198 to 200, then 198 is called the “true value” and 200 is called the “rounded value”.) 

For example, the rounded value 750 could have come from a true value rounded to the nearest 10 or maybe even the nearest 250. It may also have come from a true value rounded to the nearest 1 (thus not rounding it at all). Thus, the true value could have been something like 625 or maybe much closer like 748. 

Luckily for Timothy and Alex, after some reconnaissance work, they have discovered the general rounding methods used:

● The original statistic was a positive integer S

● Then a positive integer X was chosen such that X is a divisor of some power of 10, i.e., there exist non-negative integers Y and Z such that X * Y = 10^Z

● Finally the statistic S was rounded to the nearest positive multiple of X to get the rounded value N, i.e., there exists a positive integer W such that X * W = N and |S – N| is minimized.

The Problem:

Given the rounded value, find all the different ways it could have been      rounded(derived). In other words, given N and using the above constraints, you are to find all values of X that satisfy both of the following two equations:

● X * Y = 10^Z 

● X * W = N

The Input:

The first and only line of input contains an integer, N (1 ≤ N ≤ 1e18), representing the rounded value.

The Output:

First print out a single line containing an integer representing the number of different X values that the rounded value N could have been derived from. Then print out all of these values of X, in increasing order, on separate lines.

输出时每行末尾的多余空格,不影响答案正确性

样例输入1复制

30

样例输出1复制

4 
1 
2 
5 
10

样例解释1

Output: 1  

X = 1, Y = 10, Z = 1, W = 30   

Rounded to nearest multiple of 1: 1*10=10, 1*30=30   

Output: 2   

X = 2, Y = 5, Z = 1, W = 15  

Rounded to nearest multiple of 2: 2*5=10, 2*15=30   

Output: 5   

X = 5, Y = 2, Z = 1, W = 6   

Rounded to nearest multiple of 5: 5*2=10, 5*6=30   

Output: 10   

X = 10, Y = 1, Z = 1, W = 3   

Rounded to nearest multiple of 10: 10*1=10, 10*3=30 

样例输入2复制

120

样例输出2复制

8
1
2
4
5
8
10
20
40

样例输入3复制

8

样例输出3复制

4
1
2
4
8

思路:

求出 n 中 2 的数量和 5 的数量,进行组合

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
const int inf = 0x3f3f3f3f;

ll qpow(ll a, ll b)
{
    ll ans = 1;
    while(b)
    {
        if(b & 1)
        {
            ans *= a;
        }
        a *= a;
        b /= 2;
    }
    return ans;
}

int main()
{
    ll n;
    while(~scanf("%lld", &n))
    {
        ll a = 0, b = 0;
        while(n % 2 == 0)
        {
            n /= 2;
            a++;
        }
        while(n % 5 == 0)
        {
            n /= 5;
            b++;
        }
//        cout<<a<<' '<<b<<'\n';
        priority_queue<ll, vector<ll>, greater<ll> >q;
        for(int i = 0; i <= a; ++i)
        {
            for(int j = 0; j <= b; ++j)
            {
                q.push(qpow(2, i) * qpow(5, j));
            }
        }
        cout<<q.size()<<'\n';
        while(q.size())
        {
            cout<<q.top()<<'\n';
            q.pop();
        }
    }
    return 0;
}

 

 

 

全部评论

相关推荐

对空六翼:你真幸运,碰见这么好的人,不像我,秋招的时候被室友骗进cx了
实习好累,可以辞职全力准...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
6395次浏览 61人参与
# 你的实习产出是真实的还是包装的? #
1304次浏览 32人参与
# 巨人网络春招 #
11213次浏览 223人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7077次浏览 37人参与
# 简历第一个项目做什么 #
31329次浏览 315人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186521次浏览 1115人参与
# MiniMax求职进展汇总 #
23220次浏览 302人参与
# 研究所笔面经互助 #
118787次浏览 577人参与
# 面试紧张时你会有什么表现? #
30416次浏览 188人参与
# 简历中的项目经历要怎么写? #
309572次浏览 4163人参与
# 职能管理面试记录 #
10722次浏览 59人参与
# AI时代,哪些岗位最容易被淘汰 #
62719次浏览 748人参与
# 网易游戏笔试 #
6374次浏览 83人参与
# 把自己当AI,现在最消耗你token的问题是什么? #
6993次浏览 154人参与
# 腾讯音乐求职进展汇总 #
160437次浏览 1107人参与
# 从哪些方向判断这个offer值不值得去? #
56712次浏览 357人参与
# 正在春招的你,也参与了去年秋招吗? #
362741次浏览 2632人参与
# 你怎么看待AI面试 #
179417次浏览 1182人参与
# 小红书求职进展汇总 #
226905次浏览 1357人参与
# 你的房租占工资的比例是多少? #
92146次浏览 896人参与
# 校招笔试 #
467654次浏览 2954人参与
# 经纬恒润求职进展汇总 #
155713次浏览 1085人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务