刷题总结#11

#319. Bulb Switcher
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it’s off or turning off if it’s on). For the ith round, you toggle every i bulb. For the nth round, you only toggle the last bulb. Find how many bulbs are on after n rounds.

Given n = 3.

At first, the three bulbs are [off, off, off].
After first round, the three bulbs are [on, on, on].
After second round, the three bulbs are [on, off, on].
After third round, the three bulbs are [on, off, off].

As we know that there are n bulbs, let’s name them as 1, 2, 3, 4, …, n.

You first turn on all the bulbs.
Then, you turn off every second bulb.(2, 4, 6, …)
On the third round, you toggle every third bulb.(3, 6, 9, …)
For the ith round, you toggle every i bulb.(i, 2i, 3i, …)
For the nth round, you only toggle the last bulb.(n)
If n > 6, you can find that bulb 6 is toggled in round 2 and 3.

Later, it will also be toggled in round 6, and round 6 will be the last round when bulb 6 is toggled.

Here, 2,3 and 6 are all factors of 6 (except 1).

Prove:

We can come to the conclusion that the bulb i is toggled k times.

Here, k is the number of i’s factors (except 1).

k + 1 will be the total number of i’s factors

For example:

Factors of 6: 1, 2, 3, 6 (3 factors except 1, so it will be toggled 3 times)
Factors of 7: 1, 7 (1 factors except 1, so it will be toggled once)
….
Now, the key problem here is to judge whether k is even or odd.

Since all bulbs are on at the beginning, we can get:

If k is odd, the bulb will be off in the end.(after odd times of toggling).
If k is even, the bulb i will be on in the end (after even times of toggling).
As we all know, a natural number can divided by 1 and itself, and all factors appear in pairs.

When we know that p is i’s factor, we are sure q = i/p is also i’s factor.

If i has no factor p that makes p = i/p, k+ 1 is even.

If i has a factor p that makes p = i/p (i = p^2, i is a perfect square of p), k+ 1 is odd.

So we get that in the end:

If i is a perfect square , k+ 1 is odd, k is even (bulb i is on).
If i is NOT a perfect square , k+ 1 is even, k is odd (bulb i is off).
We want to find how many bulbs are on after n rounds (In the end).

That means we need to find out how many perfect square numbers are NO MORE than n.

The number of perfect square numbers which are no more than n, is the square root of the maximum perfect square number which is NO MORE than n

Result:

The square root of the maximum perfect square number which is NO MORE than n is the
integer part of sqrt(n).

(If i = 1, it will NEVER be toggled, k is 0 (even) here which meets the requirement.)

也就是说i这个灯泡,i有k+1个因子(1,……,i),那么假设一开始i是亮的,那么i会被toggle 有k次。
因为q=i/p. 那么一般因子都是成对的。那么如果i为整数的平方,则k+1为奇数,否则为偶数。
我们知道如果k为偶数,那么灯亮,则对应i是整数平方情况。
那么i不是整数平方,灯灭。
所以问题转换为知道1到n中有几个i的平方。则是根号n个

#46. Permutations
Given a collection of distinct numbers, return all possible permutations.
in:[1,2,3]
out:[
[1,2,3],
[1,3,2],
[2,1,3],
[2,3,1],
[3,1,2],
[3,2,1]
]

我的做法,很麻烦,虽然也是全排列
void search(int cnt,vector < vector < int> >&ans,vector < int>tmp,vector < int>& nums,vector < int>visit)
{
if(cnt==nums.size()+1)
{
ans.push_back(tmp);
return;
}
for(int i=0;i < nums.size();i++)
{
if(visit[i]==0)
{
visit[i]=1;
tmp.push_back(nums[i]);
search(cnt+1,ans,tmp,nums,visit);
visit[i]=0;
tmp.pop_back();
}
}
}

值得注意的是,可以通过swap来实现每一个位置填什么:
void permuteRecursive(vector < int> &num, int begin, vector < vector < int> > &result) {
if (begin >= num.size()) {
// one permutation instance
result.push_back(num);
return;
}

    for (int i = begin; i < num.size(); i++) {
        swap(num[begin], num[i]);
        permuteRecursive(num, begin + 1, result);
        // reset
        swap(num[begin], num[i]);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值