C - Jumps on a Circle(考数学应用)

There are p points on the circle numbered from 0 to (p - 1) in a way that from the point 0 you can move to the point 1, from the point 1 — to the point 2, and so on, and from the point (p - 1) you can move to the point 0.
A chip is initially placed at the point 0. It starts to jump around the circle, first moving 1 point forward, then 2 points forward, then 3 points forward and so on.
How many unique points (including the start one) will be visited by a chip after n moves?

Input

The input has two integers p and n (1 ≤ p ≤ 107, 0 ≤ n ≤ 1018) — the number of points on a circle and the number of moves.

output

Output a single integer — the number of unique points visited by a chip.

C - Jumps on a Circle(考数学应用)_第1张图片
题意:一个环从0到p-1,从0开始每次跳1,2,3,···,n-1,n步,问跳n步之后会有多少个位置访问过。

思路:弄一个vis数组,表示访问过的点,注意p为1e7,数组开大点(不然容易RE)

然后若n>2p,2p次后又一定回到点原点,然后2p+1,2p+2步的效果相当于1,2步(%p就知道了)
C - Jumps on a Circle(考数学应用)_第2张图片
C - Jumps on a Circle(考数学应用)_第3张图片

#include
using namespace std;
const int N = 1e7+5;
long long int vis[N];
int main()
{
    long long int p,n,sum = 0;
    cin>>p>>n;
    vis[0] = 1;
    long long now = 0;
    for(long long int i = 1;i<=min(n,2*p);i++)
    {
        now = (now+i)%p;
        vis[now] = 1;
    }
    for(int i = 0;i<p;i++)
        sum+=vis[i];
    cout<<sum<<endl;
    return 0;
}

你可能感兴趣的:(acm(运用到数学知识的题))