# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
22418 | 삼*전자 그린픽스 (#40) | Fully Generate (KRIII5_FG) | C++98 | 239 ms | 3932 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<stdio.h>
#define M 1000000007
#define X 60009
long long int n;
int m;
long long int a[X][2];
long long int g[X][2];
long long int gs[X];
int gl;
int find_index(long long int x) {
int l, r, mid;
l = 1; r = m;
while (l < r) {
mid = (l + r)/2;
if (a[mid][0] <= x) {
if (x < a[mid][1])
return mid;
else
l = mid + 1;
}
else
{
r = mid;
}
}
return l;
}
void inil() {
long long int i, j, k, l;
a[1][0] = 1;
a[1][1] = 2;
a[2][0] = 2;
a[2][1] = 4;
m = 3;
j = 4;
// a[i][0] <= x < a[i][1] = g[x] is i
for (i = 3; i < X; i++) {
k = find_index(i);
a[i][0] = j;
a[i][1] = j + k;
j += k;
m++;
}
// g[i][0] <= x < g[i][1] = repeat i times
// repeat number = gs[i](=a[g[i][0]]) + (x - g[i][0]) / i
g[1][0] = 1; g[1][1] = 2;
g[2][0] = 2; g[2][1] = 6;
g[3][0] = 6; g[3][1] = 12;
g[4][0] = 12; g[4][1] = 24;
gl = 5;
j = 24;
gs[1] = 1;
gs[2] = 2;
gs[3] = 4;
gs[4] = 6;
gs[5] = 9;
for (i = 5; i < X; i++)
{
g[i][0] = g[i - 1][1];
k = find_index(i);
g[i][1] = g[i][0] + i * k;
gs[i+1] = gs[i] + k;
if (g[i][1] > 1000000000000LL)
{
/// printf("%lld %lld %lld : %lld %lld\n", i, k, gs[i], g[i][0], g[i][1]);
gl = i + 1;
break;
}
}
}
long long int d[X];
long long int exp(long long int p, int q) {
long long int r = 1;
long long int z = p;
while (q) {
if (q & 1) {
r = (r*z) % M;
}
z = (z*z) % M;
q /= 2;
}
return r;
}
void precess() {
d[1] = 1;
//d[i] = gs[i]^i * (gs[i]+1)^i * ... * (gs[i]+k-1)^i
int i, j, k;
for (i = 2; i < gl; i++) {
k = find_index(i);
d[i] = 1;
for (j = 0; j < k; j++) {
d[i] = (d[i] * (gs[i] + j)) % M;
}
d[i] = exp(d[i], i);
d[i] = (d[i - 1] * d[i]) % M;
}
}
int find_gindex(long long int x) {
int l, r, mid;
l = 1; r = gl;
while (l < r) {
mid = (l + r) / 2;
if (g[mid][0] <= x) {
if (x < g[mid][1])
return mid;
else
l = mid + 1;
}
else
{
r = mid;
}
}
return l;
}
int main() {
int i, j, k, l;
inil();
precess();
// for (n = 2; n <= 1000000; n++)
{
scanf("%lld", &n);
// n = 1000000000000LL;
if (n == 1) { printf("1\n"); return 0; }
int gi = find_gindex(n);
long long int res = d[gi - 1];
//d[i] = gs[i]^i + (gs[i]+1)^i + ... + (gs[i]+k-1)^i
long long int dk = (n - g[gi][0]) / gi;
for (i = 0; i < dk; i++) {
res = (res * exp(gs[gi] + i, gi)) % M;
}
long long int si = g[gi][0] + dk*gi;
for (; si <= n; si++) {
res = (res * (gs[gi] + dk)) % M;
}
if(0)
{
long long int rrs = 1;
for (long long int i = 2; i <= n; i++) {
k = find_index(i);
rrs = (rrs * k) % M;
}
printf("%lld : %lld %lld %lld\n", n, res, rrs, res - rrs);
}
printf("%lld\n", res);
}
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |