int main()
{
    scanf("%d%d%s",&n,&k,str);
    c1[k-1]=c2[k]=1;
    for(int i=k;i<MAXN;i++) c1[i]=((c1[i-1]*i)%MASK*mul_inv(i-k+1))%MASK;
    for(int i=k+1;i<MAXN;i++) c2[i]=((c2[i-1]*i)%MASK*mul_inv(i-k))%MASK;
//  for(int i=0;i<100;i++) printf("%lld ",c2[i]);
//  printf("\n");
    sum[0]=str[0]-'0';
    for(int i=1;i<n;i++) sum[i]=sum[i-1]+str[i]-'0';
    long long off=1;
    for(int i=0;i<n;i++)
    {
        ans=(ans+(((getsum(n-i-2)*c1[n-i-2])%MASK+((str[n-i-1]-'0')*c2[n-i-1])%MASK)*off)%MASK)%MASK;
        off=(off*10)%MASK;
    }
    printf("%I64d\n",ans);
    return 0;
}
예제 #2
0
void key_generate(long int *d,long int *e,long int *n)
{
    long int PRIME_P,PRIME_Q,count,MODULUS,PHI;
    long int prime1_index,prime2_index;
    long int prime_array[1600];

count=generate_primes(prime_array);
//There are total 1575(which is value of 'count' variable at this PRIME_Point)
//primes between this range, And the program will randomly select any
//2-primes from those generated primes.

//randomize();     //This intializes seed for random numbers from clock.
                   //That's why 'time.h' is included. :)

prime1_index=prime2_index=2;
int low=1000;
while(prime1_index==prime2_index)
    {   srand(time(NULL));
	prime2_index = (rand() % (count-low) ) + low;
	prime1_index = rand() % low;
    }

PRIME_P=prime_array[prime1_index];
PRIME_Q=prime_array[prime2_index];

printf("PRIME_P:%10ld ( 0x%lx )\n",PRIME_P,PRIME_P);
printf("PRIME_Q:%10ld ( 0x%lx )\n",PRIME_Q,PRIME_Q);
//Here 2-primes numbers of same bit-length(15 bit) are choosen randomly

MODULUS=PRIME_P*PRIME_Q;   //This is our MODULUS for 'private' and 'public' key generation
PHI=(PRIME_P-1)*(PRIME_Q-1);  //This is 'Euler's Totient Function
printf("\nMODULUS:%10ld ( 0x%lx )\n",MODULUS,MODULUS);
printf("PHI(n) =%10ld ( 0x%lx )\n\nNOTE:phi(n) = phi(p)*phi(q) = (p-1)*(q-1)\n",PHI,PHI);

//Now, let public Key Exponent(e)=65537
//Thus, e is coprime to phi(n).(i.e. gcd(e,phi(n)) = 1)
//So, the public key is declared as: (e,n)
long int PUBLIC_KEY_EXPONENT=65537,PRIVATE_KEY_EXPONENT;
//FILE *fp;
//fp=fopen("public_key.txt","w+");
PRIVATE_KEY_EXPONENT=mul_inv(PUBLIC_KEY_EXPONENT,PHI);
printf("\nThe Public Key(e,n) is: ( %ld ( 0x%lx ), %ld ( 0x%lx ) )\n",PUBLIC_KEY_EXPONENT,PUBLIC_KEY_EXPONENT,MODULUS,MODULUS);
printf("\nThe Private Key(d,n) is: ( %ld ( 0x%lx ), %ld ( 0x%lx ) )\n",PRIVATE_KEY_EXPONENT,PRIVATE_KEY_EXPONENT,MODULUS,MODULUS);

//Putting Public Key for Public access.
//fprintf(fp,"%ld\n%ld",PUBLIC_KEY_EXPONENT,MODULUS);
//fclose(fp);
*e = PUBLIC_KEY_EXPONENT;
*d=PRIVATE_KEY_EXPONENT;  //Giving up private key as a
*n=MODULUS;               //Result of this function.
}
예제 #3
0
파일: test_mod_inv.c 프로젝트: Sindrus/RSA
int main(void) {
    printf("42 mod 2017\ninverse: %d\n", mul_inv(42, 2017));
    return 0;
}