Ural 1110 Power

Source: Internet
Author: User

1110. Powertime limit:0.5 Second
Memory limit:64 MB
You are given the whole numbers N, Mand Y. Write a program that'll find all whole numbers XIn the interval [0, M? 1] such that XN MoD M= Y. Inputthe input contains a single line with N, Mand Y(0 < N< 999, 1 < M< 999, 0 < Y< 999) separated with one space. Outputoutput all numbers XSeparated with space on one line. The numbers must is written in ascending order. If No such numbers exist then output? 1.Sample
input Output
2 6 4
2 4
problem Source:Bulgarian National Olympiad Day #1

Test Instructions: x is present, so that X's n%m equals y, and all x is present. There is no output negative one. water problem, run a fast power.
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include < Algorithm>using namespace Std;int m;int fast_mi (int a,int k) {    int res=1;    while (k)    {        if (k&1)            res=res*a%m;        a=a*a%m;        k>>=1;    }    return res;} int main () {    int n,y,i;    int a[1000];    while (Cin>>n>>m>>y)    {        int flag=0;        memset (A,0,sizeof (a));        int len=0;        for (i=0; i<=m-1; i++)        {            if (Fast_mi (i,n)%m==y)            {                flag=1;                a[len++]=i;            }        }        if (flag)        {for            (i=0; i<len; i++)            {                if (!i)                    cout<<a[i];                else                    cout<< "" <<a[i];            }            cout<<endl;        }        else            cout<<-1<<endl;    }    return 0;}

Ural 1110 Power

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.