6242: LHC的二进制升级版
- 时间限制:1秒
- 内存限制:128MB
- Special Judge
- 提交:6
- 正确:3
题目描述
在发现了3的二进制特殊性质后,LHC认为形如1、3、7、15......2n−1之类的数具有共通的性质。但是他太懒了,请你帮他验证结论吧。
现在,LHC要求你在1s内判断对于给定多组的二进制数num是否是2n−1的倍数。
输入
多组数据,每组数据一行,包含一个二进制数num和整数n。
数据限制:
1≤len(num)≤10000
1≤n≤32
其中len(num)代表二进制数num的长度。
输出
每组数据输出一行,如果num是2n−1的倍数,输出“YES”,否则输出“NO”。
样例输入
1111 4 101000111 3
样例输出
YES NO
提示
1111(2)=15(10),15 mod 15=01111(2)=15(10),15 mod 15=0
101000111(2)=327(10),327 mod 7=5101000111(2)=327(10),327 mod 7=5
来源
LHC
Solution:
先考虑n=2,即mod=3的情况,上篇博客中有讲到把num两位两位拆分再求和得到sum,判断sum是否为mod的倍数即可。
那么,对于任意正整数n,mod=2^n-1,很容易想到把num按n位拆分再求和得到sum,判断sum是否为mod的倍数就好啦。
我居然写起了自己题目的题解,可还行,下面是三个标程。


#include <iostream> #include <string> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <vector> #include <queue> #include <deque> #include <map> #include <set> #include <fstream> #include <time.h> #define P(n) cout<<n<<' ' #define range(i,a,b) for(auto i=a;i<=b;++i) #define LL long long #define ULL unsigned long long #define elif else if #define itrange(i,a,b) for(auto i=a;i!=b;++i) #define rerange(i,a,b) for(auto i=a;i>=b;--i) #define fill(arr,tmp) memset(arr,tmp,sizeof(arr)) #define IOS ios::sync_with_stdio(false);cin.tie(0) using namespace std; string word; LL n; void init(){ } void solve(){ while(cin>>word>>n){ LL ans=0, mod=(1LL<<n)-1; while(!word.empty()){ LL pos=1; range(i,1,n)if(!word.empty()){ ans=(ans+(word.back()-'0')*pos)%mod; pos<<=1; word.pop_back(); } } cout<<(ans%mod?"NO":"YES")<<' '; } } int main() { init(); solve(); return 0; }


#这个有点偷懒了,不过它可以验证结论的正确性(我想了一晚上怎么把这个AC代码卡掉,但是失败了。。) while True: try: ss,n=input().split() ss=int(ss,2) n=(1<<int(n))-1 print("NO" if ss%n else "YES") except: break


#这个是正经的Python解法。。 while True: try: tb, n = input().split() n = int(n) tb = tb[::-1] mod = (1 << n) - 1 tb += '0' * (n - len(tb) % n) tmp = 0 for i in range(0, len(tb), n): tmp = (tmp + int(tb[i:i + n][::-1], 2)) % mod print("NO" if tmp % mod else "YES") except: break