-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNecklace of Beads.cpp
More file actions
48 lines (40 loc) · 921 Bytes
/
Necklace of Beads.cpp
File metadata and controls
48 lines (40 loc) · 921 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<iostream>
#include<algorithm>
#define endl '\n'
#define lli long long int
#define forn(i,n) for (int i = 0; i < n; i++)
#define fastIO(); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
lli binPow(lli a, lli b)
{
lli res = 1;
while(b)
{
if(b&1) res = (1LL*res*a);
b>>=1;
a = (1LL*a*a);
}
return res;
}
int main () {
fastIO();
while (true)
{
lli n, k=3; cin>>n; if(n==-1) break;
if(n<=0)
{
cout << 0 << endl; continue;
}
lli suma = 0;
for(lli i = 0; i<n; i++)
{
suma += binPow(k, __gcd(i, n));
}
if(n%2 == 0)
suma += (n/2) * binPow(k, n/2) * (k+1);
else
suma += n*binPow(k, (n+1)/2);
cout << suma/(2*n) << endl;
}
return 0;
}