Number game [laws + thinking]

number games

Description

Dongdong is playing a number game with his classmates.

The rules of the game are as follows: Dongdong and his classmates sit in a circle with n people. Dongdong first said the number 1. Next, the student sitting on the left hand side of Dongdong will say the next number 2. The next classmate should count down two numbers from the previous classmate's number, that is, 4. The next student should count down three, say 7. And so on.

In order to keep the number from being too large, Dongdong and his classmates agreed that when the count reaches k-1 in his mind, the next number starts from 0. For example, when k=13, the first few numbers reported by Dongdong and his classmates are as follows:

1, 2, 4, 7, 11, 3, 9, 3, 11, 7。

After the game went on for a while, Dongdong wanted to know the sum of all the numbers he said so far.

Input

The first line of input contains three integers n, k, T, where n and k have the same meaning as described above, and T represents the total number of digits said so far.

Output

Output a line containing an integer, which means that the sum of all the numbers said by Dong Dong.

Sample Input 1 

3 13 3

Sample Output 1

17

Hint

The numbers mentioned by Dong Dong are 1, 7, 9, and 17.

Data scale and convention:

1 < n,k,T < 1,000,000;

Analysis: Find out the law and just do it. Take the question n = 3 as an example. Dongdong said 1 for the first time, 1 + (1 + 2 +3) = 7 for the second time, and 1 + (for the third time 1 + 2 + 3 +4 + 5 + 6)% k …… That is, the sum of the first n items with a tolerance of 1.

#include<iostream>
#include<bits/stdc++.h>
#include<string>
using namespace std;
int main()
{
    long long int  n,k,T;
    scanf("%lld %lld %lld", &n, &k,&T);
    long long int x=1,i = 1,j = n;
    long long int sum = 1;
    for(long long int xx = 1; xx < T; xx ++){
//        x = n*i+((n*i)*(n*i-1))/2 + 1;
//  		num=(num+(i+j)*n/2)%k;

        x = (x + (i+j) * n / 2)%k;
        sum += x;
        i += n;
        j += n;
    }
    printf("%lld\n",sum);
    return 0;
}

 

Guess you like

Origin blog.csdn.net/Mercury_Lc/article/details/107311181