0x01 位运算

【例题】CH0101 a^b

 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 using namespace std;
 6 int power(int a, int b, int p) {
 7     int ans=1%p;
 8     for (; b; b>>=1) {
 9         if (b&1) ans=(long long)ans*a%p;
10         a=(long long)a*a%p;
11     }
12     return ans;
13 }
14 int main() {
15     int a, b, p;
16     scanf("%d%d%d", &a, &b, &p);
17     printf("%d\n", power(a, b, p));
18     return 0;
19 }
View Code

 

【例题】CH0102 64位整数乘法

 写法1:

 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 typedef long long ll;
 6 using namespace std;
 7 ll mul(ll a, ll b, ll p) {
 8     ll ans=0;
 9     for (; b; b>>=1) {
10         if (b&1) ans=(ans+a)%p;
11         a=a*2%p;
12     }
13     return ans;
14 }
15 int main() {
16     ll a, b, p;
17     scanf("%lld%lld%lld", &a, &b, &p);
18     printf("%lld\n", mul(a, b, p));
19     return 0;
20 }
View Code

写法2:

 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 typedef long long ll;
 6 using namespace std;
 7 ll mul1(ll a, ll b, ll p) {
 8     ll ans=0;
 9     for (; b; b>>=1) {
10         if (b&1) ans=(ans+a)%p;
11         a=a*2%p;
12     }
13     return ans;
14 }
15 ll mul2(ll a, ll b, ll p) {
16     a%=p, b%=p;
17     ll c=(long double) a*b/p;
18     ll ans=a*b-c*p;
19     if (ans<0) ans+=p;
20     else if (ans>p) ans-=p;
21     return ans;
22 }
23 int main() {
24     ll a, b, p;
25     scanf("%lld%lld%lld", &a, &b, &p);
26     printf("%lld\n", mul2(a, b, p));
27     return 0;
28 }
View Code

 

【例题】CH0103 最短Hamilton路径

状压dp

 1 #include 
 2 #include 
 3 #include 
 4 #include 
 5 using namespace std;
 6 int f[1<<20][20];
 7 int main() {
 8     int n, weight[20][20];
 9     scanf("%d", &n);
10     for (int i=0; ii) {
11         for (int j=0; jj) {
12             int x;
13             scanf("%d", &x);
14             weight[i][j]=weight[j][i]=x;
15         }
16     }
17     memset(f, 0x3f, sizeof(f));
18     f[1][0]=0;
19     for (int i=1; i<(1<i) {
20         for (int j=0; jif ((i>>j)&1) {
21             for (int k=0; kif ((i^(1<> k & 1)
22                 f[i][j]=min(f[i][j], f[i^1<weight[k][j]);
23         }
24     }
25     printf("%d\n", f[(1<1][n-1]);
26     return 0;
27 }
View Code

 

你可能感兴趣的:(0x01 位运算)