QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#404970 | #3004. It's a Mod, Mod, Mod, Mod World | Lspeed# | WA | 1272ms | 3824kb | C++14 | 3.3kb | 2024-05-05 04:37:11 | 2024-05-05 04:37:12 |
Judging History
answer
#include<bits/stdc++.h>
#define x first
#define y second
#define eb emplace_back
#define pb push_back
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define ROF(i,a,b) for(int i=(a);i>=(b);--i)
#define all(x) (x).begin(),(x).end()
#define mp make_pair
#define sz(x) (int)(x).size()
#define make_unique(x) sort(all(x)), (x).erase(unique(all(x)), (x).end())
using namespace std;
typedef long long i64;
//typedef __int128 i128;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<i64> vl;
typedef vector<vl> vvl;
typedef pair<int,int> pii;
typedef pair<i64,i64> pll;
typedef tuple<int,int,int> iii;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
int readInt() { int a; scanf("%d",&a); return a; }
i64 readLong() { i64 a; scanf("%lld",&a); return a; }
char readChar() { char a; scanf(" %c",&a); return a; }
double readDouble() { double a; scanf(" %lf",&a); return a; }
void readString(char *s) { scanf(" %s",s); }
const int mod = 998244353;
int add(int a, int b) { return ((a+=b)>=mod) ? a-mod:a; }
int mul(int a, int b) { return a*1ll*b%mod; }
int pw(int a, int b) {
int ans = 1, res = a;
for(int i = 1; i <= b; i*=2, res=mul(res,res)) {
if(i&b) {
ans = mul(ans, res);
}
}
return ans;
}
void solve() {
int p = readInt();
int q = readInt();
int n = readInt();
p%= q;
i64 A = p*1ll*n*(n+1)/2;
if(q <= 1000) {
i64 sum = 0;
i64 cur = 0;
i64 m = 0;
FOR(i,1,q) {
cur += p;
if(cur >= q) {
m += 1;
cur -= q;
}
sum += m;
//printf("%d: %lld %lld\n",i,cur,m);
}
i64 k = n/q;
i64 ans = k*sum + m*q*k*(k-1)/2;
m *= k;
n %= q;
assert(cur == 0);
FOR(i, 1, n) {
cur += p;
if(cur >= q) {
m += 1;
cur -= q;
}
//printf("%d: %lld %lld\n",i,cur,m);
ans += m;
}
printf("%lld\n",A-q*ans);
return ;
}
if(n <= 1000) {
i64 sum = 0;
i64 cur = 0;
i64 m = 0;
FOR(i,1,n) {
cur += p;
if(cur >= q) {
m += 1;
cur -= q;
}
printf("%d: %lld\n",i,m);
sum += m;
}
//printf("%lld %lld %lld\n",A,q,sum);
printf("%lld\n",A-q*sum);
return ;
}
vector<int> bucket(1000);
//vector<int> prefix(1000);
i64 ans = 0;
i64 cur = 0;
i64 m = 0;
i64 sum = 0;
FOR(i,1,1000) {
cur += p;
if(cur >= q) {
cur -= q;
m += 1;
}
bucket[i-1] = cur;
sum += m;
//ans += m;
}
sort(all(bucket));
//prefix[0] = bucket[0].y;
//FOR(i,1,999) prefix[i] = prefix[i-1]+bucket[i].y;
//peek the first few
// loop
i64 save_cur = cur;
i64 save_m = m;
i64 save_sum = sum;
// reset cur and m
cur = 0;
m = 0;
int i = 0;
while(n >= 1000) {
// cur, m
// ans
// binary search to update m and ans
//int idx = upper_bound(all(bucket), pii(cur,1<<30))-bucket.begin()-1;
int idx = lower_bound(all(bucket), q-cur)-bucket.begin();
/*
ans += m*1000+prefix[idx];
cur += save_cur;
if(cur >= q) cur -= q;
m += (idx+1);
*/
ans += m*1000 + save_sum + (1000-idx);
m = p*1ll*(i+1000)/q;
cur += save_cur;
if(cur >= q) cur -= q;
n -= 1000;
i += 1000;
}
FOR(i,1,n) {
cur += p;
if(cur >= q) {
cur -= q;
m += 1;
}
ans += m;
}
printf("%lld\n",A-q*ans);
}
int main() {
int q = readInt();
while(q--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1272ms
memory: 3676kb
input:
91125 999956 999956 999956 999956 999956 999957 999956 999956 999958 999956 999956 999959 999956 999956 999960 999956 999956 999961 999956 999956 999962 999956 999956 999963 999956 999956 999964 999956 999956 999965 999956 999956 999966 999956 999956 999967 999956 999956 999968 999956 999956 999969 ...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 499956500946 499956500946 499957500902 499958500857 499959500811 499960500764 499961500716 499962500667 499963500617 499964500566 499965500514 499966500461 499967500407 499968500352 499969500296 499970500239 49...
result:
ok 91125 lines
Test #2:
score: -100
Wrong Answer
time: 27ms
memory: 3824kb
input:
8000 1 1 1 1 1 2 1 1 3 1 1 4 1 1 5 1 1 6 1 1 7 1 1 8 1 1 9 1 1 10 1 1 999991 1 1 999992 1 1 999993 1 1 999994 1 1 999995 1 1 999996 1 1 999997 1 1 999998 1 1 999999 1 1 1000000 1 2 1 1 2 2 1 2 3 1 2 4 1 2 5 1 2 6 1 2 7 1 2 8 1 2 9 1 2 10 1 2 999991 1 2 999992 1 2 999993 1 2 999994 1 2 999995 1 2 999...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 2 2 3 3 4 4 5 5 499996 499996 499997 499997 499998 499998 499999 499999 500000 500000 1 3 3 4 6 6 7 9 9 10 999991 999993 999993 999994 999996 999996 999997 999999 999999 1000000 1 3 6 6 7 9 12 12 13 15 1499988 1499988 1499989 1499991 1499994 1499994 149999...
result:
wrong answer 201st lines differ - expected: '1', found: '1: 0'