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 1110. Power

I get accept,but i don't why
Posted by aurora 5 Sep 2016 18:18
#include<iostream>
using namespace std;
int main()
{
    int N, M, Y;
    while (cin >> N >> M >> Y)
    {
        int f = 0,t=-1;
        for (int i = 0; i < M; i++)
        {
            int x = i;
            long long y = x;
            for (int j = 2; j < N + 1; j++)
            {
                y *= x;
                y %= M;
            }
            if (y== Y)
            {
                if (f > 0)
                    cout << ' ';
                cout << x ;
                f ++;
            }
        }
        if (f == 0)
            cout << t;
        cout << endl;
    }
    return 0;
}

why it can get accept,but this one get WA

#include<iostream>
using namespace std;
int main()
{
    int N, M, Y;
    while (cin >> N >> M >> Y)
    {
        int f = 0,t=-1;
        for (int i = 0; i < M; i++)
        {
            int x = i;
            long long y = x;
            for (int j = 2; j < N + 1; j++)
            {
                y *= x;
            }
            if (y%M== Y)
            {
                if (f > 0)
                    cout << ' ';
                cout << x ;
                f ++;
            }
        }
        if (f == 0)
            cout << t;
        cout << endl;
    }
    return 0;
}
Re: I get accept,but i don't why
Posted by ToadMonster 6 Sep 2016 13:17
Your second program assumes that all x^N are less than long long limit.
It isn't true. 10^999 requires about 999 digits. long long limit is about 19 digits.