Mod, Or and Everything HDU - 6950
生活随笔
收集整理的這篇文章主要介紹了
Mod, Or and Everything HDU - 6950
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Mod, Or and Everything HDU - 6950
題意:
給你一個n,問(n%1) or (n%2) or … or (n %n)的值
題解:
無論n為奇偶,定義m=(n-1)/2,
我們發現n mod i<=m,而當i<=m時,有n mod (n-i) =i ,于是就有n mod i 取到0 ~ m的所有整數,那0 ~ m的所有or不就是m的位數全是1,m的位數全是1怎么求,可以直接按照位數求,也可以用lowbit求,都一樣
代碼:
#include<bits/stdc++.h> #define debug(a,b) printf("%s = %d\n",a,b); typedef long long ll; using namespace std; //qdu打鐵匠 const ll INF=0x3f3f3f3f; inline int read(){int s=0,w=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();//s=(s<<3)+(s<<1)+(ch^48);return s*w; } int main() {int t;cin>>t;while(t--){ll n;cin>>n;if(n==1||n==2){cout<<0<<endl;continue;}ll w=(n-1)/2;ll num=0;while(w){num++;w>>=1;}ll tot=0;while(num--){tot=tot<<1|1;}cout<<tot<<endl;} return 0; }總結
以上是生活随笔為你收集整理的Mod, Or and Everything HDU - 6950的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 贸易公司怎么写开发信(贸易公司怎么写开发
- 下一篇: Alice and Bob