A. Candies
题目详情
Recently Vova found candy wrappers. He remembers that he bought candies during the first day, candies during the second day, candies during the third day, ……, 2^k−1 candies during the day. But there is an issue: Vova remembers neither nor but he is sure that x and k are positive integers and .
Vova will be satisfied if you tell him any positive integer so there is an integer that . It is guaranteed that at least one solution exists. Note that .
You have to answer independent test cases.
输入
The first line of the input contains one integer t () — the number of test cases. Then test cases follow.
The only line of the test case contains one integer n () — the number of candy wrappers Vova found. It is guaranteed that there is some positive integer xx and integer that .
输出
Print one integer — any positive integer value of x so there is an integer that .
题目大概意思
最近,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()测试用例的数量。接下来是t测试用例。测试用例的唯一一行包含一个整数n(),即Vova找到的糖果包装数量。可以保证存在一些正整数xx和整数k>1,即x+2x+4x+…+2k−1x=n。输出打印一个整数x的任何正整数值,因此存在一个整数k>1,即x+2x+4x+·+2k−1x=n。
解题思路
其实题目很简单,就是给出n,求出x的值,使得 时 。将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;
}