Codeforces 424 C Magic Formulas

3752 ワード

テーマリンク:[ここにリンク内容を書きます](http://codeforces.com/contest/424/problem/C)タイトル:C.Magic Formulas(マジック・マトリクス)time limit per test 2 seconds memory limit per test 256 megabytes inputstandar input output
People in the Tomskya region like magic formurlas very much.You can see some of them below.
Imagine you are given a sequence of positive integer numbers p 1,p 2,…,pn.Lets write down some magic formlas:这里写图片描述 这里写图片描述
ヘレ、「mod」means the operation of taking the resideue after dividing.
The expression means apperation the bitwise xor(excluding"OR)operation to integers x and y.The given operation exists in moders programming langes.For example,in langage C+and Java it is presenter
People in the Tomskya region like magic formullas very much、but they don’t like to calculate them!The refore you are given the sequence p,calculate the value of Q.
Input The first line of the input contains the only integer n(1̵≦?nn?n).The next line contains n integers:p 1,̵p2,?̵…
Output The only line of output shout contain a single integer-the value of Q.
Sample test(s)input 3 1 2 3 output 3
上述の式によって最後の数値Qを解きます.時間が制限されていますので、暴力はACできません.テーマによって、Magic Formulasを提示します.行列を書いて、規則を見つけて、実現することができます.注意:異和演算は交換法則を満たすものであり、0異種またはXの値はXであり、元の値を入力時に異種を完成させることができます.その後の剰余演算はマトリックスに記入して、法則を見つけて、総個数のうちiの個数を判断します.
#include <iostream>
#include <cstdio>

using namespace std;

int p[1000005];
int main()
{
    int n;
    int Q;
    int i,j;
    while(scanf("%d",&n)!=EOF)
    {
        Q=0;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&p[i]);
            Q^=p[i];
        }
        //cout<<Q<<endl;
        p[0]=0;
        p[1]=0;
        for(int i=2;i<=n;i++)
        {
            p[i]=p[i-1]^(i-1);
        }
        for(int i=1;i<=n;i++)
        {
            int count=n/i;
            int yu=n%i;
            if(count&1)
            Q^=p[i];
            Q^=p[yu];
           // Q^=0;
            Q^=yu;
        }
        printf("%d
"
,Q); } return 0; }