HDU 5175 Misaki & #39; s Kiss again (XOR operation, formula deformation), hdu5175

Source: Internet
Author: User
Tags greatest common divisor

HDU 5175 Misaki's Kiss again (XOR operation, formula deformation), hdu5175


Misaki's Kiss again Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission (s): 201 Accepted Submission (s): 57

Problem Description After the Ferries Wheel, please friends hope to receive the Misaki's kiss again, so Misaki numbers them 1, 2 .. N −1, N , If someone's number is M and satisfied GCD (N, M) Equals N XOR M , He will be kissed again.

Please help Misaki to find all M (1 <= M <= N) .

Note that:
GCD (a, B) Means the greatest common divisor A And B .
A XOR B Means A Exclusive or B  
InputThere are multiple test cases.

For each testcase, contains a integets N (0 <N <= 1010)  
OutputFor each test case,
First line output Case # X :,
Second line output K Means the number of friends will get a kiss.
Third line contains K Number mean the friends 'number, sort them in ascending and separated by a space between two numbers
Sample Input
3515
 
Sample Output
Case #1:12Case #2:14Case #3:310 12 14HintIn the third sample, gcd(15,10)=5 and (15 xor 10)=5, gcd(15,12)=3 and (15 xor 12)=3,gcd(15,14)=1 and (15 xor 14)=1 
 
SourceValentine's Day Round

Question connection: http://acm.hdu.edu.cn/showproblem.php? Pid = 1, 5175

Find the M value that satisfies gcd (N, M) = NxorM (1 <= M <= N)

Question Analysis: Make M = N xor K, original formula: gcd (N, N xor K) = N xor (N xor K) = K, from this we can find that K is the approximate number of N, find all N's approximate numbers, and determine whether it meets the equation. Because it is an exclusive or operation, the result may be larger than the approximate number, for example, if 1xor2 = 3, a gcd (n, n) with an exclusive or 0 result exists )! = Nxorn, and an empty line is output when the value is 0. Otherwise, pe

#include <cstdio>#include <cmath>#define ll long longint const MAX = 1e5;ll fac[MAX], ans[MAX];ll gcd(ll a, ll b){    return b ? gcd(b, a % b) : a;  }int main(){    int ca = 1;    ll n;    while(scanf("%I64d", &n) != EOF)    {        int cnt1 = 0, cnt2 = 0;        ll tmp = sqrt(n);        for(ll i = 1; i <= tmp; i++)            if(n % i == 0)                fac[cnt1++] = i;        for(ll i = tmp; i >= 1; i--)            if(n % i == 0 && i != 1)                fac[cnt1++] = n / i;        for(int i = cnt1 - 1; i >= 0; i--)            if(fac[i] == gcd(n, n^fac[i]) && (n^fac[i]) != 0 && (n^fac[i]) <= n)                ans[cnt2++] = (n^fac[i]);        printf("Case #%d:\n%d\n", ca++, cnt2);        if(cnt2 == 0)            printf("\n");        for(int i = 0; i < cnt2; i++)        {            if(i != cnt2 - 1)                printf("%I64d ", ans[i]);            else                printf("%I64d\n", ans[i]);        }    }   }


 

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.