用C语言判断一个数能被3整除,Brainstorming——不用除法判断某数能否被3整除

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

考虑我们那 32-bit unsigned int 的性质,它做加法、乘法只会保留低 32-bit 的结果。

实际上,它是模 2^32 的剩余系。我们可以找到这样一个数 p 使得 3*p = 1 (mod 2^32)

p 可以取 0xAAAAAAAB,3*p = 0x200000001 = 2^33 + 1

对于给出的数 a,只要乘上 p(0xAAAAAAAB),如果 a 能被3整除,那么 a*p*3 = a (mod 2^32)

如果 a 能被3整除,那么 a*p mod 2^32 就是所求的商

如果 a 不能被3整除呢?a*p*3 = a (mod 2^32) 仍然成立。如何分辨呢?

设 a = 3q + r(保证 0 <= r 

ap = (2^33+1)q + pr,其中 pr 最大为 2*0xAAAAAAAB 

如果计算 ap 时,保留 64-bit 结果,那么右移33位就得到了 q,q 就是商,余数 r 可以是0~2

计算 q*3 就可以验证 a 是否是3的倍数

gcc/g++ 32位无符号数除以3的优化实现用到了以上方法

其实,gcc/g++ 对很多常数除法做了优化……

另外,(a+r)*p(规定 0 <= r 

-----

考虑数 4*a+b 和 a+b 模3同余,所以:c 和 (c >> 2) + (c & 3) 模3同余

对于 c >= 4,(c >> 2) + (c & 3) 至少比 c 小1

反复令 c = (c >> 2) + (c & 3),当 c <= 3 时,判断 c 是否为0或3,是则原始的 c 被3整除

-----

设 c = 2*a+b = 3*a+b-a,那么如果 b-a 和 c 同余

设 c = 4*a+2*b+d = 3*a+3*b + a-b+d,a-b+d 和 c 同余

设 c = 8a+4b+2d+e = 9a+3b+3d -a +b -c +d,-a +b -c +d 和 c 同余

……

二进制表示中,偶数位和 减去 奇数位和,与原数同余

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Title: The Next Big Thing Opening shot: A bustling city street with people walking and cars honking. Voiceover: "In a world where everyone is seeking fame and success, one man has a plan to make it big." Cut to an office building where we see our protagonist, John, working as a low-level employee. Voiceover: "Meet John, a hardworking man with big dreams but a small paycheck." John is seen working at his desk, staring at his computer screen, looking frustrated. John: "This is not what I want. I need to find a way to make it big." Cut to John brainstorming ideas with his friends at a local bar. John's friend 1: "You need to create something that people can't resist, something that's different, something that's never been done before." John's friend 2: "But what could that be?" John: "I don't know yet, but I'll figure it out." Cut to John walking down the street and seeing a group of people gathered around a street performer. Street performer: "Ladies and gentlemen, watch as I perform this incredible trick!" John watches as the street performer successfully performs a trick, and the crowd cheers. John: "That's it! I'll create a new kind of street performance that will blow people's minds." Cut to John practicing his new street performance routine. John's friend 1: "This is amazing! You're going to be a hit!" John's friend 2: "We need to film this and put it on social media." Cut to John's street performance going viral on social media. Voiceover: "In just a few days, John's street performance went viral, and he became an overnight sensation." Cut to John being interviewed on a talk show. Talk show host: "John, how did you come up with this incredible street performance?" John: "It was just an idea I had, and I put in the hard work to make it a reality." Cut to John performing his street performance in front of a huge crowd. Voiceover: "John's street performance became a global phenomenon, and he became the next big thing." Closing shot: John walking down the street with his friends, looking happy and successful. Voiceover: "All it takes is one great idea and the determination to make it happen. Who knows? You could be the next big thing."

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值