ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1402. Cocktails

C ++ Accepted with long long :)
Posted by Khujamurod Murtozakulov (Tashkent U of IT) 11 Dec 2018 11:38
I accepted this problem using long long variables. I know that is not optiomal solution but very interesting and I want to share it !!!
#include <iostream>
using namespace std;
typedef long long ll;
const ll mod = 10000000000ll;
void solve(int n) {
    ll s1 = 0, s2 = 0;
    for(int i = 1; i <= n; i ++) {
        // inc
        s2 = s2 + 1;
        s1 = s1 + s2 / mod;
        s2 = s2 % mod;
        //mult
        s2 *= i;
        s1 *= i;
        s1 = s1 + s2 / mod;
        s2 = s2 % mod;
    }
    // sub
    s2 -= n;
    if(s2 < 0) {
        s1 --;
        s2 += mod;
    }
    // print
    if(s1 > 0) {
        printf("%lld%010lld", s1, s2);
    } else {
        printf("%lld", s2);
    }
}
void test() {
    for(int n = 1; n <= 21; n ++) {
        cout << n << " ";
        solve(n);
        cout << endl;
    }
}
int main() {
    //test();
    int n;
    cin >> n;
    solve(n);
}

Edited by author 11.12.2018 11:39