5.23每日一题题解

A - Candies

涉及知识点:

  • 位运算/思维

solution:

  • (通过读题我们发现这个题是让你算出x)
  • (x + 2x + 4x + ... + 2^{(k - 1)}*x = n)
  • (1 + 2 + 4 + .. + 2^{(k - 1)} = n / x)
  • (x = n / (2 ^ k - 1))
  • (所以我们只需要算出2 的所有次方然后减去1,前提是在int范围内)
  • (在这里提及一下位运算可以很快的算出2的次方)

std:

#include <iostream>
using namespace std;
const int N = 50;
int a[N];

int t;
int n;
int main()
{
    for(int i = 0;i < 31;i ++)
    {
        a[i] = (1 << i) - 1;
    }
    
    cin >> t;
    while(t --)
    {
        cin >> n;
        
        for(int i = 2;i < 31;i ++)
        {
            if(n % a[i] == 0){
                cout << n / a[i] << endl;
                break;
            }
        }
        
    }
    
	return 0;
}

原文地址:https://www.cnblogs.com/QFNU-ACM/p/12940941.html