我在这里得到了我的h / w:
#include <stdio.h>
#include <stdlib.h>
int main()
{
unsigned long int a,b,m, answer;
int i, k;
scanf("%ld", &a);
scanf("%ld", &b);
scanf("%ld", &m);
k = b >> 63;
if (k & 1)
answer = a * 2;
for(i=1;i<64;i++)
{
k = b >> (63-i);
if (k & 1)
answer = (((answer%m * 2)%m) + (a)%m)%m;
else
answer = (((answer%m * 2)%m)%m)%m;
}
printf("%ld\n", answer);
return 0;
}
我需要找到两个大数量的产品的模数,所以我在这里使用一些技巧。它适用于我的计算机上的所有测试,它在我的计算机编译器上进行了所有测试并且没有内存问题,但测试服务器上的valgrind会出现此错误:
==6557== Memcheck, a memory error detector
==6557== Copyright (C) 2002-2011, and GNU GPL'd, by Julian Seward et al.
==6557== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==6557== Command: /home/solution
==6557== Parent PID: 6556
==6557==
==6557== Use of uninitialised value of size 8
==6557== at 0x537044B: _itoa_word (in /lib64/libc-2.15.so)
==6557== by 0x5372B90: vfprintf (in /lib64/libc-2.15.so)
==6557== by 0x537AF68: printf (in /lib64/libc-2.15.so)
==6557== by 0x4007BB: main (mulmod.c:24)
==6557==
==6557== Conditional jump or move depends on uninitialised value(s)
==6557== at 0x5370455: _itoa_word (in /lib64/libc-2.15.so)
==6557== by 0x5372B90: vfprintf (in /lib64/libc-2.15.so)
==6557== by 0x537AF68: printf (in /lib64/libc-2.15.so)
==6557== by 0x4007BB: main (mulmod.c:24)
==6557==
==6557== Conditional jump or move depends on uninitialised value(s)
==6557== at 0x5372BEE: vfprintf (in /lib64/libc-2.15.so)
==6557== by 0x537AF68: printf (in /lib64/libc-2.15.so)
==6557== by 0x4007BB: main (mulmod.c:24)
==6557==
==6557== Conditional jump or move depends on uninitialised value(s)
==6557== at 0x53727A3: vfprintf (in /lib64/libc-2.15.so)
==6557== by 0x537AF68: printf (in /lib64/libc-2.15.so)
==6557== by 0x4007BB: main (mulmod.c:24)
==6557==
==6557== Conditional jump or move depends on uninitialised value(s)
==6557== at 0x537281D: vfprintf (in /lib64/libc-2.15.so)
==6557== by 0x537AF68: printf (in /lib64/libc-2.15.so)
==6557== by 0x4007BB: main (mulmod.c:24)
==6557==
==6557==
==6557== HEAP SUMMARY:
==6557== in use at exit: 0 bytes in 0 blocks
==6557== total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==6557==
==6557== All heap blocks were freed — no leaks are possible
==6557==
==6557== For counts of detected and suppressed errors, rerun with: -v
==6557== Use —track-origins=yes to see where uninitialised values come from
==6557== ERROR SUMMARY: 7 errors from 5 contexts (suppressed: 2 from 2)