codeforces练习

182 阅读2分钟

A. Candies

题目详情

Recently Vova found nn candy wrappers. He remembers that he bought xx candies during the first day, 2x2x candies during the second day, 4x4x candies during the third day, ……, 2^k−1xx candies during the kthk-th day. But there is an issue: Vova remembers neither xx nor kk but he is sure that x and k are positive integers and k>1k>1.

Vova will be satisfied if you tell him any positive integer xx so there is an integer k>1k>1 that  x+2x+4x++2k1x=nx+2x+4x+⋯+2k−1x=n. It is guaranteed that at least one solution exists. Note that k>1k>1.

You have to answer tt independent test cases.

输入

The first line of the input contains one integer t (1t1041≤t≤10^4) — the number of test cases. Then tt test cases follow.

The only line of the test case contains one integer n (3n1093≤n≤10^9) — the number of candy wrappers Vova found. It is guaranteed that there is some positive integer xx and integer k>1k>1 that x+2x+4x++2k1x=nx+2x+4x+⋯+2k−1x=n.

输出

Print one integer — any positive integer value of x so there is an integer k>1k>1 that x+2x+4x++2k1x=nx+2x+4x+⋯+2k−1x=n.

题目大概意思

最近,Vova发现了n糖果包装纸。他记得第一天买了x颗糖果,第二天买了2颗,第三天买了4x颗,……第k天买了2k−1x颗。但有一个问题:Vova既不记得x也不记得k,但他确信x和k是正整数,k>1。如果你告诉他任何正整数x,那么有一个整数k>1,x+2x+4x+…+2k−1x=n,Vova会很满意。可以保证至少存在一个解决方案。请注意,k>1。您必须回答t独立的测试用例。input输入的第一行包含一个整数t(1t1041≤t≤10^4)测试用例的数量。接下来是t测试用例。测试用例的唯一一行包含一个整数n(3n1093≤n≤10^9),即Vova找到的糖果包装数量。可以保证存在一些正整数xx和整数k>1,即x+2x+4x+…+2k−1x=n。输出打印一个整数x的任何正整数值,因此存在一个整数k>1,即x+2x+4x+·+2k−1x=n。

解题思路

其实题目很简单,就是给出n,求出x的值,使得k>1k>1 时 x+2x+4x++2k1x=nx+2x+4x+⋯+2k−1x=n。将x提出去,等式左侧其实是一个等比数列(首项1,公比2)。我们只需计算n的值是否是x的整数倍。

for (int j = 2;; j++)
        {
            x = (int)(pow(2, j) - 1);
            if (!(n % x))
            {
                cout << n / x << endl;
                break;
            }
        }

AC代码

#include<iostream>
#include<algorithm>
using namespace std;


int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n;
        cin >> n;

        int x;
        for (int j = 2;; j++)
        {
            x = (int)(pow(2, j) - 1);
            if (!(n % x))
            {
                cout << n / x << endl;
                break;
            }
        }
    }
    return 0;
}