锘??xml version="1.0" encoding="utf-8" standalone="yes"?> Description Input Output Sample Input Sample Output Hint
]]>
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 1344
Accepted: 676
You know number bases have place values that start at 1 (base to the 0 power) and proceed right-to-left to base^1, base^2, and so on. In base −2, the place values are 1, −2, 4, −8, 16, −32, ... (reading from right to left). Thus, counting from 1 goes like this: 1, 110, 111, 100, 101, 11010, 11011, 11000, 11001, and so on.
Eerily, negative numbers are also represented with 1's and 0's but no sign. Consider counting from −1 downward: 11, 10, 1101, 1100, 1111, and so on.
Please help the cows convert ordinary decimal integers (range -2,000,000,000..2,000,000,000) to their counterpart representation in base −2.-13
110111
Reading from right-to-left:
1*1 + 1*-2 + 1*4 + 0*-8 +1*16 + 1*-32 = -13
濡傛灉n涓哄鏁幫紝閭d箞鏈綅鑲畾涓?錛屽洜姝ゅ氨鍙互杞負姹?x-1)/-2 鐨勫瓙闂浜嗭紒 錛堥櫎浠?2鍙互綾繪瘮鍗佽繘鍒跺氨鑳界悊瑙d簡錛?br> 濡傛灉n涓哄伓鏁幫紝鏈綅蹇呬負0錛岀劧鍚庡啀杞負姹倄/-2鐨勫瓙闂錛?br>緇撴潫鏉′歡褰?n=1銆?br>鎬濊礬寰堢畝鍗曪紝浣嗘槸鏈変袱涓皬鏂歸潰瑕佹彁鎻愶紝涓瀹氳鑰冭檻n=0鐨勬儏鍐碉紝鎴戝氨鏄病鑰冭檻鑰屼竴鐩磋秴鏃訛紝閮侀椃錛岃繕鎵句笉鍑洪敊璇紝闄峰叆浜嗘寰幆鎬笉寰楄秴鏃訛紝榪樻湁娉ㄦ剰鐞冪殑緇撴灉鏄嗗簭鐨勶紒錛佸懙鍛典笅闈㈡槸浠g爜錛?br>
//============================================================================
2
// Name : poj 3191.cpp
3
// Author : worm
4
// Copyright : Your copyright notice
5
// Description :鎶婁竴涓崄榪涘埗鐨勬暟杞崲涓?2榪涘埗鐨勬暟
6
//============================================================================
7
8
#include <iostream>
9
#include <stdio.h>
10
#include <string>
11
#include <math.h>
12
using namespace std;
13
string a= "";
14
int main()
{
15
int x;
16
cin >> x;
17
if (x == 0)
{
18
cout <<"0"<<endl;
19
return 0;
20
}
21
22
while (x != 1)
{
23
if (abs(x) % 2 == 1)
{
24
a += '1';
25
x = (x-1)/-2;
26
continue;
27
}
28
a += '0';
29
x /= -2;
30
}
31
cout <<"1";
32
for (int i = a.length() - 1; i >= 0; i--)
{
33
printf("%c",a[i]);
34
}
35
36
return 0;
37
}
38
]]>
#include <iostream>
10
#include <queue>
11
#include <math.h>
12
using namespace std;
13
int a, b;
14
int p[9999] =
{ 0 };
15
int visited[9999] =
{ 0 };
16
bool isprime(int x)
{
17
18
for (int i = 2; i <= sqrt((double) x); ++i)
{
19
if (x % i == 0)
20
return false;
21
}
22
return true;
23
}
24
int BFS(int s, int r)
{
25
queue<int> q;
26
q.push(s);
27
p[s] = 0;
28
visited[s] = 1;
29
while (!q.empty())
{
30
int temp = q.front();
31
q.pop();
32
for (int i = 0; i <= 9; i++)
{
33
int y1 = (temp / 10) * 10 + i;
34
if (isprime(y1) && !visited[y1])
{
35
q.push(y1);
36
p[y1] = p[temp] + 1;
37
visited[y1] = 1;
38
}
39
int y2 = temp % 10 + (temp / 100) * 100 + i * 10;
40
if (isprime(y2) && !visited[y2])
{
41
q.push(y2);
42
p[y2] = p[temp] + 1;
43
visited[y2] = 1;
44
}
45
int y3 = temp % 100 + (temp / 1000) * 1000 + 100 * i;
46
if (isprime(y3) && !visited[y3])
{
47
q.push(y3);
48
p[y3] = p[temp] + 1;
49
visited[y3] = 1;
50
}
51
if (i != 0)
{
52
int y4 = temp % 1000 + i * 1000;
53
if (isprime(y4) && !visited[y4])
{
54
q.push(y4);
55
p[y4] = p[temp] + 1;
56
visited[y4] = 1;
57
}
58
}
59
if (visited[r])
60
return p[r];
61
}
62
63
}
64
return 0;
65
}
66
int main()
{
67
int n;
68
cin >> n;
69
while (n--)
{
70
memset(visited,0,sizeof(visited));
71
memset(p,0,sizeof(p));
72
cin >> a >> b;
73
cout << BFS(a, b) << endl;
74
75
}
76
return 0;
77
}
78
]]>
//============================================================================
2
// Name : poj.cpp
3
// Author :
4
// Version :
5
// Copyright : Your copyright notice
6
// Description : Hello World in C++, Ansi-style
7
//============================================================================
8
9
#include <iostream>
10
#include <queue>
11
using namespace std;
12
queue<int> q;
13
int result[100001];
14
int visited[100001] =
{0};
15
int BFS(int start,int end)
{
16
if (start == end)
17
return 0;
18
q.push(start);
19
result[start] = 0;
20
visited[start] = 1;
21
while(!q.empty())
{
22
int temp = q.front();
23
q.pop();
24
int next;
25
for (int i = 0; i < 3; ++i)
{
26
if (i == 0)
27
next = temp - 1;
28
if (i == 1)
29
next = temp + 1;
30
if (i == 2)
31
next = temp*2;
32
if(next > 100000 || next < 0)
{
33
continue;
34
}
35
if (visited[next] != 1)
{
36
q.push(next);
37
result[next] = result[temp] + 1;
38
visited[next] = 1;
39
}
40
if (next == end)
41
return result[next];
42
}
43
}
44
return 0;
45
}
46
int main()
{
47
int n,k;
48
cin >> n >> k;
49
cout << BFS(n,k) << endl;
50
return 0;
51
}
52
]]>
//============================================================================
2
// Name : poj.cpp
3
// Author :
4
// Version :
5
// Copyright : Your copyright notice
6
// Description : 棰樼洰澶ф剰灝辨槸灝嗘搴忔暟鍒?,2,3,
,n,閫氳繃鏈灝戠殑“澶嶅埗綺樿創”鏁?br> 7
// 鍙樹負閫嗗簭搴忓垪鐨勯棶棰樸?br> 8
//鍩烘湰鎬濇兂錛?nbsp; 濡傛灉n涓哄鏁幫紝鍋囪n = 7錛?br> 9
// 1 2 3 4 5 6 7 灝唍宸﹁竟鐨勬渶涓棿鐨勪袱涓暟渚濇縐誨埌7鐨勫彸杈?br>10
// 1 2 5 6 7 3 4 鐨勬渶涓棿
11
// 1 6 7 3 2 5 4
12
// 7 3 2 1 6 5 4 灝?nbsp;3 2 1涓?nbsp;6 5 4 浜ゆ崲
13
// 7 6 5 4 3 2 1
14
//鎬葷殑嬈℃暟涓?n+1)/2;
15
// n = 鍋舵暟鏃訛紝鍙互鍏堟妸n涓嶇錛岃繖鏍穘-1灝變負濂囨暟鐨勬儏鍐碉紝姹傚嚭鍚庣殑搴忓垪鍦ㄥ拰n浜ゆ崲涓涓?br>16
//鍗沖彲錛岀粨鏋滀負n/2 + 1;
17
//============================================================================
18
19
#include <iostream>
20
using namespace std;
21
void solve(int n)
{
22
int x = (n+1)/2 - 1;
23
int y = n;
24
for (int i = 0; i < x; ++i)
{
25
cout << n/2 << " " << 2 << " " << y-2-i << endl;
26
n -= 2;
27
}
28
cout <<"2 " << x << " " << x + 1 << endl;
29
}
30
31
int main()
{
32
int n;
33
cin >> n;
34
if (n == 1)
{
35
cout << 0 <<endl;
36
return 0;
37
}
38
if (n == 2)
{
39
cout << "1" <<endl;
40
cout << "1 1 1" <<endl;
41
return 0;
42
}
43
if (n % 2 != 0)
{
44
cout << (n+1)/2 <<endl;
45
solve(n);
46
}
47
else
{
48
cout << n/2 + 1 << endl;
49
solve(n-1);
50
cout << 1 << " "<< n-1 <<" 1" <<endl;
51
}
52
53
return 0;
54
}
55
鏈鍚庝竴瀹氳娉ㄦ剰1 鍜?2 鐨勬儏鍐碉紝鎴戝洜涓哄繕浜嗚冭檻錛寃a浜嗗嚑嬈★紝鍛靛懙...
]]>